【问题标题】:Add legend manually to ggplot2 does not work [duplicate]手动将图例添加到 ggplot2 不起作用 [重复]
【发布时间】:2020-04-21 15:55:15
【问题描述】:

我不仅搜索了 stackoverflow,还搜索了许多其他网站。不幸的是,我还没有找到任何帮助。我将尽可能明确地解释我的问题。由于这是我在 stackoverflow 上的第一个问题,请温柔一点,我是 R 的初学者。我的目标是手动将图例添加到我已经创建的 ggplot2 对象中。

这是我正在使用的数据集:

structure(list(Values = 0:5, Count = c(213L, 128L, 37L, 18L, 
3L, 1L), rel_freq = c(0.5325, 0.32, 0.0925, 0.045, 0.0075, 0.0025
), pois_distr = c(0.505352031744286, 0.344902761665475, 0.117698067418343, 
0.0267763103376731, 0.00456870795136548, 0.000623628635361388
)), class = "data.frame", row.names = c(NA, -6L))

看起来像

  Values Count rel_freq   pois_distr
1      0   213   0.5325 0.5053520317
2      1   128   0.3200 0.3449027617
3      2    37   0.0925 0.1176980674
4      3    18   0.0450 0.0267763103
5      4     3   0.0075 0.0045687080
6      5     1   0.0025 0.0006236286

接下来,我已经成功创建了一个可以正常的ggplot,代码是:

cols <- c('Beob. Häufigkeiten' = 'lightblue', 'Theor. Häufigkeiten' = 'darkblue')
plot_yeast1 <- ggplot(data.frame(data1_plot), aes(x=Values)) + 
  geom_col(aes(y=rel_freq, fill = 'Beob. Häufigkeiten'), col = 'lightblue4', alpha = 0.8) + 
  geom_point(aes(y=pois_distr, colour = 'Theor. Häufigkeiten'), alpha = 0.9, size = 4) +
  scale_fill_manual(name = 'Legende', values = cols) +
  scale_colour_manual(name ='Legende', values = cols) + 
  scale_y_continuous(breaks = seq(0, 0.6, 0.05)) +
  labs(title = 'Gegenüberstellung der beobachteten Häufigkeiten mit den theoretischen \nHäufigkeiten aus dem geschätzten Poissonmodell', x = 'Auftretende Fehler von Hefezellen', y = 'Relative Häufigkeit', subtitle = 'Konzentration 1') +
  theme_bw()
plot_yeast1

输出是:

我的目标是将情节右侧的两个手动创建的图例合并为一个。我已经尝试跳过图例的第二个标题,然后看起来像

但宽阔的空间是丑陋的,必须有可能将这两个图例合并为一个,两个条目靠近在一起。 我已经在上面9个多小时了,搜索了很多帖子,都没有解决我的问题。 如果有任何不清楚的地方,请告诉我。正如我已经写过的,这是第一次提出问题。 谢谢

【问题讨论】:

  • 顺便提一些反馈:一个措辞非常优美的问题。感谢您以易于导入和使用的格式提供详细信息、代码和数据。非常感谢这些细节,肯定会给你最好的机会得到一个有用的答案!

标签: r ggplot2 legend


【解决方案1】:

如果主要是在视觉上从两者中创建“一个”图例,这种方法可能会有所帮助 - 详情请参阅 cmets 至 theme(...) - 最后调用:

cols <- c('Beob. Häufigkeiten' = 'lightblue', 'Theor. Häufigkeiten' = 'darkblue')
plot_yeast1 <- ggplot(data.frame(data1_plot), aes(x=Values)) + 
  geom_col(aes(y=rel_freq, fill = 'Beob. Häufigkeiten'), col = 'lightblue4', alpha = 0.8) + 
  geom_point(aes(y=pois_distr, colour = 'Theor. Häufigkeiten'), alpha = 0.9, size = 4) +
  scale_fill_manual(name = 'Legende', values = cols) +
  scale_colour_manual(name ='', values = cols) + 
  scale_y_continuous(breaks = seq(0, 0.6, 0.05)) +
  labs(title = 'Gegenüberstellung der beobachteten Häufigkeiten mit den theoretischen \nHäufigkeiten aus dem geschätzten Poissonmodell', x = 'Auftretende Fehler von Hefezellen', y = 'Relative Häufigkeit', subtitle = 'Konzentration 1') +
  theme_bw() +
  theme(legend.box.background = element_rect(colour = "grey", fill = "white"), # create a box around all legends
        legend.box.margin = margin(0.1, 0.1, 0.1, 0.1, "cm"),                  # specify the margin of that box
        legend.background = element_blank(),                                   # remove boxes around legends (redundant here, as theme_bw() seems to do that already)
        legend.spacing = unit(-0.5, "cm"),                                     # move legends closer together
        legend.margin = margin(0, 0.2, 0, 0.2, "cm"))                          # specify margins of each legend: top and bottom 0 to move them closer
plot_yeast1

【讨论】:

  • 也许也可以添加输出来提高质量,这样人们一眼就不需要运行代码?
  • 非常感谢!
【解决方案2】:

您可以像这样将图例与theme(legend.margin) 一起移动

cols <- c('Beob. Häufigkeiten' = 'lightblue', 'Theor. Häufigkeiten' = 'darkblue')
plot_yeast1 <- ggplot(data.frame(data1_plot), aes(x=Values)) + 
  geom_col(aes(y=rel_freq, fill = 'Beob. Häufigkeiten'), col = 'lightblue4', alpha = 0.8) + 
  geom_point(aes(y=pois_distr, colour = 'Theor. Häufigkeiten'), alpha = 0.9, size = 4) +
  scale_fill_manual(name = 'Legende', values = cols) +
  scale_colour_manual(name =NULL, values = cols) + 
  scale_y_continuous(breaks = seq(0, 0.6, 0.05)) +
  labs(title = 'Gegenüberstellung der beobachteten Häufigkeiten mit den theoretischen \nHäufigkeiten aus dem geschätzten Poissonmodell', x = 'Auftretende Fehler von Hefezellen', y = 'Relative Häufigkeit', subtitle = 'Konzentration 1') +
  theme_bw() +
  theme(
    # legend.position = c(.95, .95),
    # legend.justification = c("right", "top"),
    legend.box.just = "right",
    legend.margin = margin(-10)
  )
plot_yeast1

【讨论】:

  • 非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-16
  • 2014-11-20
  • 1970-01-01
  • 1970-01-01
  • 2017-07-22
  • 2022-01-01
相关资源
最近更新 更多