ss0208535 ss0208535 - 1 year ago 168
R Question

Using geom_abline() and ggplot

I am a beginner in

--it's been only 4 days since I have started experimenting with it. So, I apologize if this question sounds too basic. I'd appreciate any guidance--I've been struggling with this issue for about an hour.

I was experimenting with using
as below:

p <- ggplot(mpg, aes(cty, hwy)) + geom_point()
p + geom_abline() + facet_wrap(~cyl)

This works as in I can see a reference line in all four faceted graphs as below:

enter image description here

Later, I was using another related dataset
to see what happens to

p <- ggplot(mtcars, aes(wt, mpg)) + geom_point()
p + geom_abline() + facet_wrap(~cyl)

However, when I ran this command, I couldn't see
Quite surprisingly, I found similar version of above command in the help file, and it says "
is out of the range

While I know what "out of range" means, but how do I know whether in a particular dataset,
will be out of range? I can override it by forcing it to use a specific slope and an intercept, but then I'd consider this a bit of hacking--i.e. modifying the code after seeing the output. Is there any way I can know what happens behind the scenes for

Here's the graph I got without any
enter image description here

I'd appreciate any thoughts. I am really confused.

Answer Source

If you don't give any arguments, geom_abline() uses default values, intercept = 0 and slope = 1.

ggplot(mpg, aes(cty, hwy)) + geom_point() + geom_abline() + lims(x = c(0,35), y = c(0,50))
ggplot(mtcars, aes(wt, mpg)) + geom_point() + geom_abline() + lims(x = c(0,6), y = c(0,35))

enter image description hereenter image description here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download