Question

Apologies that this example isn't that great but it does highlight the point.

mtcars$tran <- factor(mtcars$am, labels=c("Man","Aut"))
ggplot(mtcars, aes(x=hp, y= mpg, group=tran)) + geom_smooth(aes(colour=tran))
ggplot(mtcars, aes(x=hp, y= mpg, group=tran)) + geom_point(aes(colour=tran)) 
ggplot(mtcars, aes(x=hp, y= mpg, group=tran)) + geom_point(aes(colour=tran)) + geom_smooth(aes(colour=tran))
ggplot(mtcars, aes(x=hp, y= mpg, group=tran)) + geom_smooth(aes(colour=tran)) + scale_y_continuous(limits=c(12,60))

What I want to do is draw a smoothed curve but 'zoom in' by restricting the scale of the y axis. However it seems ggplot excludes any data outside the scale limits when working out what the smoothed curves are. Yes, this seems logical, but how would I see what I want to see? In my real data, the (raw) y values range between 5 and 14, but the smoothed curves lie entirely between 7 and 9. So there is a lot of empty space at the top. When I set it to c(7,9) it no longer uses points outside of that range to calculate the smooth curve, and hence I get a different curve.

I can't provide by data but you can see this in this example. See how in the last plot, two points are dropped off and the last half of the 'Man' curve is not that same as it was in the original graph.

Question

If scale_y_continuous limits the data values that are used to construct the smoothed curve (logical), how can you draw the curve using all the data and then 'zoom in' on it (wrt the y axis).

Let me know if that's not clear.

Thanks

Was it helpful?

Solution

Use coord_cartesian instead of scale_y_continuous

ggplot(mtcars, aes(x=hp, y= mpg, group=tran)) + 
geom_smooth(aes(colour=tran))  +
coord_cartesian(ylim = c(12,60))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top