Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in R Programming by (5.3k points)
edited by

I have an object from ggplot2, say myPlot, how can I identify the ranges for the x and y axes?

It doesn't seem to be a simple multiple of the data values' range, because one can rescale plots, modify axes' ranges, and so on.  findFn (from sos) and Google don't seem to be turning up relevant results, other than how to set the axes' ranges.

1 Answer

0 votes
by
edited by

To extract the plot axes ranges for a ggplot2 object, you can use the following:

For the plot “p” given below:

p <- ggplot(data = mpg,aes(hwy, cty)) +

  geom_point()+

  facet_grid(. ~ manufacturer) + 

  coord_cartesian(xlim = c(20,40))+

  theme(strip.text.x = element_text(size = 10, colour = "black", angle = 90))

To extract x and y ranges:

 p$coordinates$limits

$x

[1] 20 40

$y

NULL

Related questions

Browse Categories

...