2013-04-25 9 views
7

Znalazłem dość zagmatwaną funkcję w ggplot podczas próbowania przypisywania segmentów w skali log10. Poniższy kod tworzy poniższy wykres:Adnotacja segmentu w skali log10 działa inaczej dla końca i początku segmentu?

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

niniejsza jest co jestem po:

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

Innymi słowy, muszę log10 przekształcić punkt końcowy segmentu na osi X, ale nie na początku. Czy to zachowanie ma logiczne wytłumaczenie? Rozumiem, że aes() does the transformations ... ale w tym przypadku transformacje na osi X powinny być jednolite (dobrze, log10), prawda?

pracuję nad:

R version 3.0.0 (2013-04-03) 
Platform: x86_64-w64-mingw32/x64 (64-bit) 
ggplot2_0.9.3.1 

Odpowiedz

1

Stwierdzili, że jest to problem z scales() (nie tylko dla scale_x_log10()), gdy jest używany z annotate() i xend wartość jest (jest already filled jak wystawienia przez W .Chang). W tym przypadku transformacja xend odbywa się tylko w jednym kierunku - log10 wartości nie jest podejmowane, ale moc jest obliczana.

scale_x_log10() działa bez problemów, jeśli na przykład, "rect" jest stosowany w annotate() i xmin, xmax wartości są świadczone.

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

Obejście tego problemu byłoby użycie geom_segment() z data=NULL i wszystkich innych wartości umieszczone wewnątrz 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

Powiązane problemy