r - ggplot散点图中的图例问题

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

最佳答案

正如 @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

关于r - ggplot散点图中的图例问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6265371/

相关文章:

在 ggplot2 中重新着色图例文本背景

R ggplot突出显示季度数字表已取代数字

r - 如何在 ggplot 中独立定位两个图例

r - 在 R Markdown 中动态控制标签集的数量

r - 带有 ggplot 的多面热图,用于 X 的选定部分,并带有附加文本标签

r - 从 R 列中的字符串创建 txt 文件

python - 图例中带有线和点标记的两个图共享相同的标签

c++ - 在 Rcpp 中构建数据框

r - 如何使用 ggplot2 获取具有不同几何匹配的 2 个图形的网格

Python:如何在没有错误的情况下引用可选变量