在 ggplot 上将一个条形图叠加到另一个条形图上
Overlaying a Barplot onto another on ggplot
我正在尝试使用 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
class 数据是不可能的。我在这里错过了什么,我试图制作一个新的条形图并将其添加到 Speciesplot
但我也无法让它工作。
您可以采用以下两种策略之一
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 创建
您可以添加“失败”,更改为长数据,然后制作堆叠条形图。这不是完全格式化的。使用“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 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
class 数据是不可能的。我在这里错过了什么,我试图制作一个新的条形图并将其添加到 Speciesplot
但我也无法让它工作。
您可以采用以下两种策略之一
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 创建您可以添加“失败”,更改为长数据,然后制作堆叠条形图。这不是完全格式化的。使用“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")