【问题标题】:Modyfing the Legend in ggplot2修改ggplot2中的图例
【发布时间】:2015-06-29 20:05:34
【问题描述】:

我在与 ggplot2 中的标签交互时遇到问题。

我有来自两个实验的两个数据集(温度与时间),但记录在不同的时间步长。我已经成功地合并了数据框,并使用 reshape2 库中的 melt 函数将它们长时间地绘制在同一个图中。 因此,初始数据帧如下所示:

> d1
    step   Temp
1  512.5 301.16
2  525.0 299.89
3  537.5 299.39
4  550.0 300.58
5  562.5 300.20
6  575.0 300.17
7  587.5 300.62
8  600.0 300.51
9  612.5 300.96
10 625.0 300.21
> d2
   step   Temp
1   520 299.19
2   540 300.39
3   560 299.67
4   580 299.43
5   600 299.78
6   620 300.74
7   640 301.03
8   660 300.39
9   680 300.54
10  700 300.25

我是这样组合的:

> mrgd <- merge(d1, d2, by = "step", all = T)
step Temp.x Temp.y
1  512.5 301.16     NA
2  520.0     NA 299.19
...

并将其放入 ggplot2 的长格式中:

> melt1 <- melt(mrgd3, id = "step")
> melt1
    step variable  value
1  512.5   Temp.x 301.16
2  520.0   Temp.x     NA
...

现在,我想例如做一个值分布的直方图。我是这样做的:

p <- ggplot(data = melt1, aes(x = value, color = variable, fill = variable)) + geom_histogram(alpha = 0.4)

我的问题是当我尝试修改此图的图例时,我不知道该怎么做!我遵循了 R Graphics Cookbook 书中的建议,但我没有运气。

我尝试过这样做,例如(更改图例的标签):

> p + scale_fill_discrete(labels = c("d1", "d2"))

但我只是创建一个“新”图例框,就像这样

甚至完全删除图例

> p + scale_fill_discrete(guide = F)

我刚刚得到这个

最后,这样做也无济于事

> p + scale_fill_discrete("")

同样,它只是添加了一个新的图例框

有人知道这里发生了什么吗?看起来我实际上是在修改另一个 Label 对象,如果这有任何意义的话。我已经查看了此站点中的其他相关问题,但我没有发现有人和我有同样的问题。

【问题讨论】:

  • @Henrik 我回滚了您的编辑,因为解决问题的最巧妙方法实际上是以更直接的方式处理数据(使用rbind),而不是摆弄标签传奇。总的来说,我认为,使用 ggplot2 的最佳方式是整理输入数据中的变量名称、因子级别等,而不是尝试更改数据格式中的内容。它使绘图代码更具可读性。

标签: r ggplot2 reshape2


【解决方案1】:

去掉aes(color = variable...),去掉属于aes(color = ...)的刻度。

ggplot(data = melt1, aes(x = value, fill = variable)) + 
  geom_histogram(alpha = 0.4) + 
  scale_fill_discrete(labels = c("d1", "d1")) # Change the labels for `fill` scale

第二个图包含aes(color = variable...)。在这种情况下,颜色将在直方图箱周围绘制彩色轮廓。您可以关闭比例,这样您就只有一个图例,即由fill 创建的图例

ggplot(data = melt1, aes(x = value, color = variable, fill = variable)) +
  geom_histogram(alpha = 0.4) +
  scale_fill_discrete(labels = c("d1", "d1")) +
  scale_color_discrete(guide = F) # Turn off the color (outline) scale

【讨论】:

    【解决方案2】:

    最直接的做法是根本不使用reshape2merge,而是使用rbind 您的数据框:

    dfNew <- rbind(data.frame(d1, Group = "d1"),
      data.frame(d2, Group = "d2"))
    ggplot(dfNew, aes(x = Temp, color = Group, fill = Group)) +
      geom_histogram(alpha = 0.4) +
      labs(fill = "", color = "")
    

    如果您想按组改变 alpha:

    ggplot(dfNew, aes(x = Temp, color = Group, fill = Group, alpha = Group)) +
      geom_histogram() +
      labs(fill = "", color = "") +
      scale_alpha_manual("", values = c(d1 = 0.4, d2 = 0.8))
    

    另请注意,geom_histogram 的默认 position"stacked"。除非您使用geom_histogram(position = identity),否则不会有条形重叠。

    【讨论】:

    • 那么我现在如何更改图例框? 'scale_color_discrete' 和 'scale_fill_discrete("")' 函数的行为与我之前发布的相同。
    • @j_eiros 为什么需要更改它们?我一般会设置输入到 ggplot 的数据,所以一开始是正确的。还是只是为了删除图例标题?我会修改上面的代码来做到这一点。
    • 谢谢。最后一个问题,您知道是否可以将 alpha 值映射到变量?假设我希望其中一个数据系列比另一个更不透明,我该怎么做?
    • @j_eiros alpha 可以在 aes 中设置,就像颜色和填充一样。请参阅编辑后的答案。
    猜你喜欢
    • 2013-03-05
    • 2012-08-21
    • 2023-03-12
    • 2016-06-23
    • 1970-01-01
    • 2020-05-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-23
    相关资源
    最近更新 更多