如何在 ggplot2 中自定义图例?

Posted

技术标签:

【中文标题】如何在 ggplot2 中自定义图例?【英文标题】:How to customize a legend in ggplot2? 【发布时间】:2021-08-27 20:16:32 【问题描述】:

我有以下数据框:

df <-  tribble(~ id,       ~ type,              ~ value,
               "1",  "Quarterly",  -185.44272504185938,
               "2",  "Quarterly",  2.3461400352210546,
               "3",  "Quarterly",  -3.3506362679401747,
               "4",  "Quarterly",  12.198021733063797,
               "5",  "Quarterly",  39.16805907764865,
               "6",  "Quarterly",  29.258292527180572,
               "7",  "Quarterly",  18.605504231676832,
               "8",  "Quarterly",  61.17282084445469,
               "9",  "Quarterly",  115.76795844352,
               "10", "Quarterly",  90.3473788015599,
               "1",   "Monthly",  473.42922462885326,
               "2",   "Monthly",  -10.579657811202168,
               "3",   "Monthly",  14.88866793035703,
               "4",   "Monthly",  0.9983391011546701,
               "5",   "Monthly",  -0.8838179658391709,
               "6",   "Monthly",  78.33644128716827,
               "7",   "Monthly",  92.38129375625499,
               "8",   "Monthly",  226.60868103438406,
               "9",   "Monthly",  295.9518745997166,
               "10",   "Monthly",  412.9794644618978)

df_aux <-  tribble(~ id,    ~ type,         ~ value,
                   "1", "Yearly", 287.9864995869939,
                   "2", "Yearly", -8.233517775981113,
                   "3", "Yearly", 11.538031662416856,
                   "4", "Yearly", 13.196360834218467,
                   "5", "Yearly", 38.28424111180948,
                   "6", "Yearly", 107.59473381434884,
                   "7", "Yearly", 110.98679798793182,
                   "8", "Yearly", 287.78150187883875,
                   "9", "Yearly", 411.7198330432366,
                   "10","Yearly", 503.3268432634577)

ggplot(df, aes(reorder(id, value), value, fill = type)) +
  geom_bar(position = "stack", stat = "identity", width = .6) +
  geom_point(data = df_aux, aes(id, value), shape = 23, color = "white", size = 3) +
  scale_fill_manual(values = c("#00a79d", "#9dd4cf", "#012169")) +
  scale_y_continuous(breaks = seq(-200, 700, by = 100),
                     limits = c(-200, 700)) +
  coord_flip() +
  labs(fill = "",
       x = "",
       y = "",
       title = "Some title",
       subtitle = "Some subtitle") +
  theme_minimal()

我在这里遇到的问题首先是类型的图例,它们看起来与所需的输出不同:

但是,我怎样才能改变 id 10 的颜色而不出现在图例上?似乎ggplot2不允许手动修改图例。

【问题讨论】:

【参考方案1】:

您可以使用key_glyph 指定图例形状,然后通过type 手动指定形状,就像您为fill 所做的那样。

ggplot(df, aes(reorder(id, value), value, shape = type, fill = type)) +
  geom_bar(position = "stack", stat = "identity", width = .6, key_glyph = "point") +
  geom_point(data = df_aux, aes(id, value), color = "white", size = 3) +
  scale_fill_manual(values = c("#00a79d", "#9dd4cf", "#012169"), name = "") +
  scale_shape_manual(values = c("Yearly" = 23, "Quarterly" = 22, "Monthly" = 22), name = "") +
  scale_y_continuous(breaks = seq(-200, 700, by = 100),
                     limits = c(-200, 700)) +
  coord_flip() +
  labs(fill = "",
       x = "",
       y = "",
       title = "Some title",
       subtitle = "Some subtitle") +
  theme_minimal()

编辑:如果您想单独显示 ID 10,我建议使用 ggnewscale 应用第二个填充比例。这涉及 ggplot2 的“标签外”使用,因此需要更多的争论才能使该点出现在该系列的顶部,并隐藏图例。

library(ggnewscale)
ggplot(df, aes(reorder(id, value), value, shape = type, fill = type)) +
  geom_bar(position = "stack", stat = "identity", width = .6, key_glyph = "point") +
  scale_fill_manual(values = c("#00a79d", "#9dd4cf", "#012169"), name = "") +
  scale_shape_manual(values = c("Yearly" = 23, "Quarterly" = 22, "Monthly" = 22), name = "") +
  geom_point(data = df_aux, aes(id, value), color = "white", size = 3) +
  new_scale_fill() +
  geom_bar(data = df[which(df$id == "10"),], aes(fill = type),
           position = "stack", stat = "identity", width = .6, key_glyph = "point") +
  scale_fill_manual(values = c("#68b6ea", "#9dc4dd"), name = "") +
  ...

【讨论】:

这对第一个问题真的很有帮助!我仍在努力改变与 ID 号 10l 对应的栏的颜色 已更新。还有一种方法可以更好地实现这一点,方法是创建特定于 ID 10 的新 types,并将它们隐藏在图例中。这将解决我遇到的问题,即由于新填充比例所需的绘图顺序,我的点位于 ID 10 的条形后面....

以上是关于如何在 ggplot2 中自定义图例?的主要内容,如果未能解决你的问题,请参考以下文章

如何在ggplot2的图例中斜体化一个类别

带有颜色和图例的简单 ggplot2 情况

lattice levelplot 或 ggplot2 map R 的自定义图例

在 ggplot2 中,使用两种图形类型时如何缩放图例?

R语言ggplot2可视化:在ggplot2中将图例(legend)移到图内自定义图例所处的位置自定义图例背景图例所处边框的颜色

在 ggplot2 中,如何将图例文本更改为百分比格式 [重复]