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()
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!
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)).
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)
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.
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 .
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.
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.
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:
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.
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"
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With