0 votes
1 view
in R Programming by (5.3k points)

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 (25.3k points)

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

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...