简体   繁体   English

如何自定义 ggplot2 中的图例?

[英]How to customize a legend in ggplot2?

I have the following dataframes:我有以下数据框:

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

The problem I am having here is in the first place the legends of type, they look different from the desired output:我在这里遇到的问题首先是类型的图例,它们看起来与所需的 output 不同:

在此处输入图像描述

But also, how can I change the colors from the id 10 without it appearing on the legend?而且,我怎样才能从 id 10 更改 colors 而不会出现在图例上? It seems ggplot2 does not allow to modify manually a legend.似乎 ggplot2 不允许手动修改图例。

You can specify the legend shape using key_glyph and then manually specify the shape by type the same way you have done for fill .您可以使用key_glyph指定图例形状,然后按照您对fill所做type相同方式手动指定形状。

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

在此处输入图像描述

EDIT: If you want to show ID 10 separately, I'd suggest using ggnewscale to apply a second fill scale.编辑:如果您想单独显示 ID 10,我建议使用ggnewscale应用第二个填充比例。 This is getting into "off-label" use of ggplot2, so it will take more wrangling to get the point to appear on top for that series, and to hide the legend.这涉及到 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 = "") +
  ...

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM