在ggplot上将条形图叠加到另一个上

Posted

技术标签:

【中文标题】在ggplot上将条形图叠加到另一个上【英文标题】:Overlaying a Barplot onto another on ggplot 【发布时间】:2021-10-21 00:14:09 【问题描述】:

我正在尝试使用 ggplot 2 制作简单的条形图,但未能成功 我的数据是

dput(Success)
structure(list(Species = c("b", "c", "g", "g, b", "m"), n = c(586L, 
5L, 293L, 4L, 8L), Success = c(412L, 5L, 186L, 4L, 6L)), row.names = c(NA, 
-5L), class = "data.frame")

我做了以下情节

Speciesplot<-ggplot(Success, aes(Species, n, fill = Species)) + geom_bar(stat = "identity") +
  scale_x_discrete(labels = c("Blue tit", "Coal tit", "Great tit", "Mixed Broods (G,B)", "Marsh tit")) +
  scale_y_continuous(breaks = seq(0, 600, by = 50)) +
   scale_fill_manual(values=c("dodgerblue", "gray", "chartreuse4", "red", "lightgoldenrod"))+
  theme(element_blank())+
  ggtitle("Number of nests by species")+
  ylab("Number of nests")+
  theme(legend.position = "none")+
  geom_text(aes(label=n), position=position_dodge(width=0.9), vjust=-0.25)

这给了

我现在要做的就是将Success 数据添加到此条形图上 这样我就可以在条形图上显示成功嵌套的数量(如堆叠的条形图),但据我所知,int 类数据是不可能的。我在这里缺少什么,我尝试制作一个新的条形图并将其添加到 Speciesplot 但我也无法让它工作。

【问题讨论】:

【参考方案1】:

您可以采用以下两种策略中的任何一种

Success <- structure(list(Species = c("b", "c", "g", "g, b", "m"), n = c(586L, 
                                                                         5L, 293L, 4L, 8L), Success = c(412L, 5L, 186L, 4L, 6L)), row.names = c(NA, 
                                                                                                                                                -5L), class = "data.frame")



library(tidyverse)
# First Method

Success %>%
  pivot_longer(!Species) %>%
  ggplot(aes(x = Species, y = value, fill = name)) +
  geom_col(position = 'dodge') +
  scale_x_discrete(labels = c("Blue tit", "Coal tit", "Great tit", "Mixed Broods (G,B)", "Marsh tit"))

#Second (alternative) method
Success %>%
  ggplot() + 
  geom_col(aes(x = Species, y = n, fill = Species), position = 'identity') +
  scale_x_discrete(labels = c("Blue tit", "Coal tit", "Great tit", "Mixed Broods (G,B)", "Marsh tit")) +
  scale_y_continuous(breaks = seq(0, 600, by = 50)) +
  scale_fill_manual(values=c("dodgerblue", "gray", "chartreuse4", "red", "lightgoldenrod"))+
  theme(element_blank())+
  ggtitle("Number of nests by species")+
  ylab("Number of nests")+
  theme(legend.position = "none") +
  geom_text(aes(x = Species, y = n, label=n), position=position_dodge(width=0.9), vjust=-0.25) +
  geom_col(aes(x = Species, y = Success), position = 'identity', width = 0.7, alpha = 0.6) +
  geom_text(aes(x = Species, y = Success, label=Success), position=position_dodge(width=0.9), vjust=1)

由reprex package (v2.0.1) 于 2021-08-19 创建

【讨论】:

【参考方案2】:

您可以添加“失败”,更改为长数据,然后制作堆叠条形图。这不是完全格式化的。使用“alpha”来区分成功和失败

Success <- Success %>% 
  mutate(failure = n - Success) %>% 
  select(-n) %>% 
  pivot_longer(-Species) %>% 
  mutate(name = as_factor(name))


ggplot(Success, aes(x = Species, y = value, fill = Species, alpha = name)) + 
  geom_bar(stat = "identity") +
  scale_x_discrete(labels = c("Blue tit", "Coal tit", "Great tit", "Mixed Broods (G,B)", "Marsh tit")) +
  scale_y_continuous(breaks = seq(0, 600, by = 50)) +
  scale_fill_manual(values=c("dodgerblue", "gray", "chartreuse4", "red", "lightgoldenrod")) +
  scale_alpha_manual(values = c(1, 0.5)) +
  theme(element_blank()) +
  labs(title = "Number of nests by species", y = "Number of nests") +
  geom_text(aes(label = if_else(name == "Success", value, NULL)), vjust = -0.25) +
guides(alpha = "none", fill = "none")

【讨论】:

以上是关于在ggplot上将条形图叠加到另一个上的主要内容,如果未能解决你的问题,请参考以下文章

条形图上条形图上方 ggplot 汇总统计量的注释

使用 ggplot 在分组条形图上定位标签

如何在ggplot2中并排条形图上居中标签

R语言使用ggplot2可视化堆叠条形图,并在堆叠条形图上显示数据值实战

ggplot:条形图面板

在R ggplot2中将条形图绘制到地图上