Do log10 segment annotations work differently for the end and start of a segment?

I found a rather confusing function in ggplot when trying to annotate segments on a log10 scale. The following code creates the graph below:

library(ggplot2) dat <- data.frame(x = x <- 1:1000, y = log(x)) ggplot(dat, aes(x = x, y = y)) + geom_line(size = 2) + scale_x_log10() + annotate("segment", x = 0, xend = log10(100), y = log(100), yend = log(100), linetype = 2) + annotate("segment", x = log10(100), xend = log10(100), y = 0, yend = log(100), linetype = 2) 

enter image description here

While this is what I get after:

 ggplot(dat, aes(x = x, y = y)) + geom_line(size = 2) + scale_x_log10() + annotate("segment", x = 0, xend = log10(100), y = log(100), yend = log(100), linetype = 2) + annotate("segment", x = 100, xend = log10(100), y = 0, yend = log(100), linetype = 2) 

enter image description here

In other words, I have to log10 convert the endpoint of the segment along the x axis, but not to the beginning. Does this behavior have a logical explanation? I understand that aes() does the conversion ... but in this case, the x-axis transforms should be the same (well, log10), right?

I am working on:

 R version 3.0.0 (2013-04-03) Platform: x86_64-w64-mingw32/x64 (64-bit) ggplot2_0.9.3.1 
+7
source share
1 answer

It was discovered that this is a scales() error (not only for scale_x_log10() ) when it is used with annotate() and xend value (this is already populated as a W.Chang release). In this case, xend conversion is performed in only one direction - log10 values ​​are not taken into account, but power is calculated.

scale_x_log10() works without problems if, for example, "rect" used in the values annotate() and xmin , xmax .

 ggplot(dat,aes(x,y))+geom_line()+ scale_x_log10()+ annotate("rect",xmin=100,xmax=1000,ymin=log(10),ymax=log(200)) 

enter image description here

A workaround for this problem would be to use geom_segment() with data=NULL and all other values ​​placed inside aes() .

 ggplot(dat, aes(x = x, y = y)) + geom_line(size = 2) + scale_x_log10() + geom_segment(data=NULL,aes(x = 100, xend = 100, y = 0, yend = log(100)), linetype = 2) 

enter image description here

+1
source

All Articles