Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing vector of axis ticks for an existing plot in ggplot2

Tags:

r

ggplot2

I would like to get the vector of values for axis ticks in an existing plot in ggplot. I know that a ggplot object is a list with 9 elements and I was wondering if somehow I could extract the values for the axis ticks from that list. For example if I produce this toy example:

library(ggplot2)
g=ggplot(data=mtcars,aes(hp,mpg))+geom_point()

enter image description here What I want are the vectors

 c(100,200,300)
 c(10,15,20,25,30,35)

for x ticks and y ticks respectively w

Is there a way to to this?

Many thanks!

like image 547
Sölvi Avatar asked Jul 04 '15 18:07

Sölvi


People also ask

How do I specify tick marks in ggplot2?

The default value of Y-axis tick marks using ggplot2 are taken by R using the provided data but we can set it by using scale_y_continuous function of ggplot2 package. For example, if we want to have values starting from 1 to 10 with a gap of 1 then we can use scale_y_continuous(breaks=seq(1,10,by=1)).

How do I add axis tick marks in R?

To hide or to show tick mark labels, the following graphical parameters can be used : xaxt : a character specifying the x axis type; possible values are either “s” (for showing the axis) or “n” ( for hiding the axis)

How do I change the y axis values in ggplot2?

Use scale_xx() functions It is also possible to use the functions scale_x_continuous() and scale_y_continuous() to change x and y axis limits, respectively.

How do I change axis labels in R?

Changing axis labels To alter the labels on the axis, add the code +labs(y= "y axis name", x = "x axis name") to your line of basic ggplot code. Note: You can also use +labs(title = "Title") which is equivalent to ggtitle .

How to remove axis labels and ticks in ggplot2 in R?

In this article, we will discuss how to remove axis labels and ticks in ggplot2 in R Programming Language. The axes labels and ticks can be removed in ggplot using the theme () method. This method is basically used to modify the non-data components of the made plot. It gives the plot a good graphical customized look.

How to set tick marks on the Y axis of boxplot?

The R code below set the position of tick marks on the y axis of the box plot. The function scale_y_continuous () and the argument breaks are used to choose where the tick marks appear : Tick mark labels can be formatted to be viewed as percents, dollars or scientific notation.

How to set Axis breaks of a ggplot2 plot on the Y-axis?

The following code illustrates how to set the axis breaks of a ggplot2 plot on the y-axis. For this, we can basically use the same code as in Example 1. We simply need to replace the scale_x_continuous function by the scale_y_continuous function:

How to set axis ticks for discrete and continuous axes in R?

Set axis ticks for discrete and continuous axes. x or y axis can be discrete or continuous. In each of these two cases, the functions to be used for setting axis ticks are different. The functions scale_x_discrete() and scale_y_discrete() are used to customize discrete x and y axis, respectively.


1 Answers

Those values can be found in

ggbld <- ggplot_build(g)
ggbld$panel$ranges[[1]]$x.major_source
#[1] 100 200 300

and

ggbld$panel$ranges[[1]]$y.major_source
#[1] 10 15 20 25 30 35

They can also be found stored as characters here:

ggbld$panel$ranges[[1]]$x.labels
#[1] "100" "200" "300"
ggbld$panel$ranges[[1]]$y.labels
#[1] "10" "15" "20" "25" "30" "35"

Update:

The above doesn't work with ggplot2_3.0.0, but that information can be still be found using:

ggbld <- ggplot_build(g)
ggbld$layout$coord$labels(ggbld$layout$panel_params)[[1]]$x.major_source
ggbld$layout$coord$labels(ggbld$layout$panel_params)[[1]]$x.labels
like image 142
Jota Avatar answered Oct 29 '22 19:10

Jota