我在ggplot中发现了一个相当令人困惑的功能,同时尝试在log10规模上注释段。以下代码生成如下图:
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)
这就是我所追求的:
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)
换句话说,我必须在x轴上log10转换段的端点,但不是开头。这种行为是否有合理的解释?我理解aes()
does the transformations ...但在这种情况下,x轴上的变换应该是均匀的(好吧,log10),对吗?
我正在努力:
R version 3.0.0 (2013-04-03)
Platform: x86_64-w64-mingw32/x64 (64-bit)
ggplot2_0.9.3.1
答案 0 :(得分:1)
发现这是scales()
的错误(不仅适用于scale_x_log10()
)与annotate()
一起使用且提供了xend
的值(xend
scale_x_log10()
3}}作为W.Chang的问题)。在这种情况下,"rect"
的转换仅在一个方向上进行 - 未取得log10的值,但计算功率。
annotate()
可以正常运行,例如,xmin
中使用xmax
并提供ggplot(dat,aes(x,y))+geom_line()+
scale_x_log10()+
annotate("rect",xmin=100,xmax=1000,ymin=log(10),ymax=log(200))
,geom_segment()
值。
data=NULL
此问题的解决方法是将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)
一起使用,并将所有其他值放在{{1}}内。
{{1}}