ggplot散点图中的传奇故障

时间:2011-06-07 12:47:05

标签: r ggplot2 legend

我想使用ggplot创建一个显示方法比较数据的散点图。这些图应该包含原始数据,理想线和带错误的拟合线。图例应显示理想线和拟合线的线型/线宽/线颜色。

我可以获得我想要的大部分内容,但传说中存在以下问题:

  • 图例为每种线型显示2行,为什么?,如何修复?

  • 我不希望在图例矩形中没有粉红色背景(如果我没有指定填充颜色,那么矩形背景就会变成默认的灰色,我不喜欢它了)

示例代码:

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°角落在克利夫兰

最终代码:

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() 

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:2)

实际上,有一种方法可以在不添加时髦的解决方法的情况下进行更改:

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

答案 2 :(得分:0)

这是我在没有传奇

中的两行的情况下使用urie 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中的图例控制能更好地避免这种黑客攻击。

相关问题