ggplot散点图中的传奇故障

Kar*_*ron 6 r legend ggplot2

我想使用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
Run Code Online (Sandbox Code Playgroud)

我非常感谢你们所有人花时间回复.虽然有一个逻辑可行,但我不会在试验和错误的情况下到达那里.我确实为最终更改了一些代码:

  • 最后使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() 
Run Code Online (Sandbox Code Playgroud)

And*_*rie 5

正如@Iselzer在评论中指出的那样,这两行是for 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() 
Run Code Online (Sandbox Code Playgroud)

另请注意,您可以使用labs()创建标签来简化代码.这意味着您不必重新创建比例.

在此输入图像描述