7

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!

Sölvi
  • 500
  • 5
  • 17

2 Answers2

12

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
Jota
  • 17,281
  • 7
  • 63
  • 93
  • Wow,thank you for a quick and a great response. So the ggplot_build function both plots the plot and returns a list of info about the plot? – Sölvi Jul 04 '15 at 18:42
  • 1
    `ggplot_build` according to its help page: "...is the powerhouse that converts the plot specification into something that's ready to be rendered on screen." It doesn't plot the data, but it does return a list of info about the plot object. – Jota Jul 04 '15 at 18:48
  • 2
    @Jota Apparently, the ggplot structure has changed somewhat. To get the panel ranges, I had to use `ggplot_build(g)$layout$panel_ranges[[1]]$x.major_source`. The above commands return NULL. – Michael S Taylor Sep 17 '17 at 11:23
  • This doesn't work for ggplot2_3.0.0 anymore, any ideas? – zx8754 Aug 10 '18 at 11:45
  • 2
    @zx8754 Hm, maybe there's a better way, but you can use `ggplot_build(g)$layout$coord$labels(ggplot_build(g)$layout$panel_params)[[1]]$x.major_source` or `ggplot_build(g)$layout$coord$labels(ggplot_build(g)$layout$panel_params)[[1]]$x.labels` – Jota Aug 12 '18 at 02:19
0

To follow up on the comments on @Jota's answer, the methods mentioned appear to have changed. Using one of the following no longer appears to work:

ggplot_build(g)$layout$panel_ranges[[1]]$x.major_source
ggbld <- ggplot_build(g)
ggbld$layout$coord$labels(ggbld$layout$panel_params)[[1]]$x.labels
ggbld$layout$coord$labels(ggbld$layout$panel_params)[[1]]$x.major_source

Using of ggplot2 version 3.5.5, I can extract the major tick marks, as per the original question, with:

(g1 <- ggplot_build(g)$layout$panel_params[[1]]$x$breaks)
 

giving

[1]  NA 100 200 300  NA

which we can subset with g1[which(!is.na(g1))].

In case of interest, the minor tick marks (visible as thin lines on the plot but not labelled on the axis) can be extracted with:

ggplot_build(g)$layout$panel_params[[1]]$x$minor_breaks

giving

[1]  50 100 150 200 250 300
dardisco
  • 5,086
  • 2
  • 39
  • 54