2011-06-07 117 views
6

我想用ggplot创建一个显示方法比较数据的散点图。这些地块应该有原始数据,理想线和错误的拟合线。图例应显示理想线和拟合线的线型/线宽/线条颜色。ggplot散点图中的传奇故事

我可以得到大部分的我想要什么,但有这些问题与传说:

  • 的图例显示2行的每一行的类型,为什么?如何解决?

  • 我宁愿在传说中的矩形没有粉红色的背景(如果我没有指定填充颜色则rectanglebackground成为默认的灰色,这是我不喜欢任何更好)

示例代码:

set.seed(603) 
x.raw=rnorm(n=30, mean=50, sd=20) 
y.raw=x.raw+rnorm(n=30, mean=2, sd=2) 
x.raw=round(x.raw, 2); y.raw=round(y.raw, 2) 
df=data.frame(x=x.raw, y=y.raw) 

require(ggplot2, quietly=TRUE) 
theme_set(theme_bw()) 
xy.range=range(df$x, df$y) 

p=ggplot(df, aes(x=x, y=y)) + 
geom_point(shape=ifelse(nrow(df)>49, 1, 16)) + 
geom_smooth(method=lm, fill="red1", aes(colour="Fitted", linetype="Fitted")) + 
geom_abline(intercept=0, slope=1, aes(colour="Ideal", linetype="Ideal")) + 
scale_colour_manual(name="Lines", values=c("Ideal"="blue", "Fitted"="red")) + 
scale_linetype_manual(name="Lines", 
         values=c("Ideal"="solid", "Fitted"="twodash")) + 
scale_x_continuous(name="Control", limits=xy.range) + 
scale_y_continuous(name="Evaluation", limits=xy.range) + 
opts(title="Method Comparison") 
p 

我真的很感谢大家花时间回复。虽然有什么有效的逻辑,我不会在那里反复试验。我确实改变一下代码进行最后的:最后

  • 做出geom_point使点不被覆盖
  • 保持通话不断扩展,使X和Y轴的限制被迫成为同
  • 类似的说明,加入aspect.ratio = 1,现在理想线从角变为45°角人LA克利夫兰

最终代码到角:

ggplot(df, aes(x=x, y=y)) + 
    geom_smooth(method=lm, se=FALSE, size=1, aes(colour="Fitted", linetype="Fitted")) + 
    geom_smooth(method=lm, fill="red", colour="red", linetype="twodash", size=1) + 
    geom_line(data = data.frame(x=0, y=0), aes(colour = "Ideal", linetype = "Ideal"), size=1) + 
    #geom_abline(intercept=0, slope=1, aes(colour = "Ideal", linetype = "Ideal"), size=0) + 
    geom_abline(intercept=0, slope=1, colour = "blue", linetype = "solid", size=1) + 
    geom_point(shape=ifelse(nrow(df)>49, 1, 16)) + 
    scale_colour_manual(name="Lines", values=c("Ideal"="blue", "Fitted"="red")) + 
    scale_linetype_manual(name="Lines", values=c("Ideal"="solid", "Fitted"="twodash")) + 
    scale_x_continuous(name="Control", limits=xy.range) + 
    scale_y_continuous(name="Evaluation", limits=xy.range) + 
    opts(title="Method Comparison", aspect.ratio=1) + 
    theme_bw() 
+0

一号线是顺利,另一个是abline。至于如何解决这个问题,我想要做的就是不用任何关于实际情节的传说。然后伪造一些数据,并使用geom_line将其绘制成图标。但这只是一个解决方法。 – 2011-06-07 13:53:27

回答

5

正如@Iselzer在评论中指出的那样,这两行代码为ablinesmooth

为了得到传说中的背景有一个白色填充,你要欺骗ggplot如下:

  • 创建映射到颜色
  • 与填充一个geom_smooth层创建第二个,几乎是相同的,geom_smooth层但此时用白色填充,没有被映射到一个图例:

的代码:

p=ggplot(df, aes(x=x, y=y)) + 
    geom_point(shape=ifelse(nrow(df)>49, 1, 16)) + 
    geom_smooth(method=lm, fill="white", aes(colour="Fitted", linetype="Fitted")) + 
    geom_smooth(method=lm, fill="red") + 
    geom_abline(intercept=0, slope=1, aes(colour="Ideal", linetype="Ideal")) + 
    scale_colour_manual(name="Lines", values=c("Ideal"="blue", "Fitted"="red")) + 
    scale_linetype_manual(name="Lines", values=c("Ideal"="solid", "Fitted"="twodash")) + 
    opts(title="Method Comparison") + 
    labs(x="Control", y="Evaluation") + 
    theme_bw() 

还要注意,通过使用labs()创建标签,您可以简化代码。这意味着您不必重新创建秤。

enter image description here

0

这里是我采取的尤斯andrie的代码,而这两条线在图例

ggplot(df, aes(x=x, y=y)) + 
    geom_point(shape=ifelse(nrow(df)>49, 1, 16)) + 
    geom_smooth(method=lm, fill="white", aes(colour="Fitted", linetype="Fitted")) + 
    geom_smooth(method=lm, fill="red") + 
    geom_abline(intercept=0, slope=1, colour = "blue", linetype = "solid") + 
    geom_line(data = data.frame(x=0, y=0), aes(colour = "Ideal", linetype = "Ideal")) + 
    scale_colour_manual(name="Lines", values=c("Ideal"="blue", "Fitted"="red")) + 
    scale_linetype_manual(name="Lines", values=c("Ideal"="solid", "Fitted"="twodash")) + 
    opts(title="Method Comparison") + 
    labs(x="Control", y="Evaluation") + 
    theme_bw() 

我希望在传奇控制ggplot的新版本将避免这类黑客攻击的更好。

2

其实,还有就是要改变这种不添加时髦的解决办法的方式:

p + theme(legend.key = element_rect(color=NA, fill="white"))