Why geom_abline does not honor scales_x_reverse?

ggplot2, r, visualization

Solution

So there answer is that this is just a bug. It is related to a problem in `geom_segment`. It works when the aestetics are specified inside `aes`, but not otherwise.

ggplot(data.frame(x=seq(1,10),y=seq(1,10))) + 
  geom_point(aes(x=x,y=y)) +
  scale_x_reverse() + 
  geom_segment(x = -10, xend=10, y=-10, yend=10, color='blue') +
  geom_segment(data = data.frame(x = -10, xend=10, y=-10, yend=10), 
               aes(x=x, y=y, xend=xend, yend=yend),
               color='red')

However, there seems to be a more general problem with using `geom_abline`, `geom_hline`, `geom_vline` together with scales. See here for more information.

Problem

I have been struggling with `geom_abline` commands when combined with `scale_x_reverse`. For instance, the following code creates a plot of 10 points following the identity. I can reverse the x axis with `scale_x_reverse`, but when I add an identity using `geom_abline` the red identity line is not reversed. ``` ggplot(data.frame(x=seq(1,10),y=seq(1,10))) + geom_point(aes(x=x,y=y)) + geom_abline(color="red") + scale_x_reverse(limits=c(10,-10)) + scale_y_continuous(limits=c(-10,10)) + ggtitle("I would expect the red line to be on top of the points") ``` It seems that `geom_abline` plots a line of `intercept=0, slope=1`, but does not honor the scale transformation. As a workaround I know that I can force the slope to do what I want: ``` ggplot(data.frame(x=seq(1,10),y=seq(1,10))) + geom_point(aes(x=x,y=y)) + geom_abline(intercept=0, slope=-1, color="red") + scale_x_reverse(limits=c(10,-10)) + scale_y_continuous(limits=c(-10,10)) + ggtitle("I would expect the red line to be on top of the points") ``` I find this `geom_abline` behaviour confusing and I don't understand the reasons why this was implemented this way. Given the good design of ggplot, I am sure that there must be a good logical reason, but I have been unable to understand it. Can anyone please explain it to me?

Original source