转换不会传播ggplot2中的线段 [英] Transformation doesn't transorm line segments in ggplot2

查看:161
本文介绍了转换不会传播ggplot2中的线段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用一些线段来标注剧情。 x轴最好通过日志转换显示。我使用 ggplot2 来处理转换,这也意味着我不应该转换到我的线段的位置。但是当我应用一个转换时,线段消失了(好 - 由于转换,它们不再适合绘图窗口)。关于如何让他们跟随转型的任何建议?



最小的例子:

  library(ggplot2)
## Base plot:
B < - ggplot(data = data.frame(X = 10 ^(1:10),Y = 1:10 ),
aes(x = X,y = Y))+ geom_point()
##生成分段:
S1 < - geom_segment(x = 1000,xend = 1000,
y = 3,yend = 5)
S2 < - geom_segment(x = 20,xend = 2.5e9,
y = 8,yend = 7)
##生成转换:
T < - scale_x_continuous(trans =log)

比较以下内容:

  B#基本图
B + T#基本图,转换
B + S1 + S2#基本,未转换,细分
B + S1 + S2 + T#应该用细分:短缺

我知道我可以改变细分的位置,但我真的更想找到更多<$ c $

解决方案:

 

S3 < - geom_segment(x = log(1000),xend = log(1000),
y = 3,yend = 5)
S4 < - geom_segment(x = log (20),xend = log(2.5e9),
y = 8,yend = 7)
B + S1 + S2
B + S3 + S4 + T#

谢谢!

解决方案

不确定我展示的情节是否符合你的期望。但如果是,下面的解释是有效的。



ggplot2 转换是在美学。数据在绘图之前先进行转换(或者进行任何类型的拟合,例如:geom_smooth等等都是在转换后的数据上完成的)。

所以,如果你希望将日志转换反映到您的细分受众群中,您必须将 aes 包装为:

  S1和S2;  -  geom_segment(AES(X = 1000,xend的= 1000,Y = 3,YEND = 5))
S2< - geom_segment(AES(X = 20, xend = 2.5e9,y = 8,yend = 7))

顺便说一句,应该是 log10,NOT log

  T < -  scale_x_continuous(trans =log10) 

现在,如果您绘制 B + S1 + S2 + T



ggplot2_aes



更进一步:将 B + S1 + S2 + T S1 S2 用我的修改:

  ggplot_build(B + S1 + S2)$ data#和
ggplot_build(B + S1 + S2 + T)$ data

可以看到美学相应地得到改变。


I am trying to annotate a plot using some line segments. The x-axis is best displayed by a log transformation. I am using ggplot2 which handles transformations, which also means I shouldn't have to transform to location of my line segments. But when I apply a transformation, the line segments disappear (well - they don't "fit" into the plot window any more, due to the transformation). Any suggestions on how to get them to "follow" the transformation?

Minimal example:

library(ggplot2)
## Base plot:
B <- ggplot(data = data.frame(X = 10^(1:10), Y = 1:10),
            aes(x = X, y = Y)) + geom_point()
## Generate segments: 
S1 <- geom_segment(x = 1000, xend = 1000,
                   y = 3, yend = 5)
S2 <- geom_segment(x = 20, xend = 2.5e9,
                   y = 8, yend = 7)
## Generate transformation:
T <- scale_x_continuous(trans = "log")

Compare the following:

B               # Basic plot
B + T           # Basic plot, transformed
B + S1 + S2     # Basic, untransformed, with segments
B + S1 + S2 + T # Should be transformed with segments: segments missing

I know I could just transform the locations of the segments, but I'd really rather find a more ggplot2-style solution!

Hack solution:

S3 <- geom_segment(x = log(1000), xend = log(1000),
                   y = 3, yend = 5)
S4 <- geom_segment(x = log(20), xend = log(2.5e9),
                   y = 8, yend = 7)
B + S1 + S2
B + S3 + S4 + T #Fine, but not elegant.

Thanks!

解决方案

Not sure if the plot I've shown is what you expect. But if it is, the explanation below is valid.

In ggplot2 transformations are performed on aesthetics. And the data is transformed first before plotting (or doing anything of the sort of fitting, ex: geom_smooth etc.. is done on transformed data).

So, if you want the log transformation to be reflected on your segment, you'll have to wrap around with aes as:

S1 <- geom_segment(aes(x=1000, xend=1000, y=3, yend=5))
S2 <- geom_segment(aes(x=20, xend=2.5e9, y=8, yend=7))

And by the way, your transformation should be log10, NOT log:

T <- scale_x_continuous(trans = "log10")

Now, if you plot B + S1 + S2 + T:

One step further: Compare your B+S1+S2+T and the one with S1 and S2 modified with mine using:

ggplot_build(B+S1+S2)$data # and 
ggplot_build(B+S1+S2+T)$data

to see that the aesthetics get transformed accordingly.

这篇关于转换不会传播ggplot2中的线段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆