ggplot散点图中的图例问题

6
我希望使用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°角延伸至角落,效果与Cleveland相近

最终代码:

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

一行是用于平滑,另一行是用于abline。关于如何修复图例,我会在实际绘图中不使用任何图例。然后伪造一些数据,并使用geom_line来绘制带有图例的图形。但这只是一个解决方法。 - Luciano Selzer
3个回答

5
如@Iselzer在评论中指出的那样,这两行是用于“abline”和“smooth”的。
要使图例背景填充为白色,您需要像以下方式一样欺骗“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

2
实际上,有一种方法可以在不添加奇怪的解决方案的情况下更改这个问题:
p + theme(legend.key = element_rect(color=NA, fill="white"))

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中,更好的图例控制能够避免这种hack。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接