McMahok
McMahok

Reputation: 360

Overlaying a Barplot onto another on ggplot

I'm trying and failing to make a simple barplot with ggplot 2 my data are

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

I've made the following plot

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)

Which gives

enter image description here

all I want to do now is add the Success data overlaid onto this barplot so that I would have the number of successful nests displayed on the bar (like a stacked barchart) but as far I can see this isn't possible with int class data. What am I missing here, I've tried to make a new bar chart and add it to Speciesplot but I can't get that to work either.

Upvotes: 1

Views: 205

Answers (2)

Tech Commodities
Tech Commodities

Reputation: 1959

You can add the "failures", change to long data and and then make a stacked barchart. This isn't fully formatted. Using "alpha" to differentiate between the successes and failures

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

enter image description here

Upvotes: 1

AnilGoyal
AnilGoyal

Reputation: 26218

You may adopt either of the following two strategies

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)

Created on 2021-08-19 by the reprex package (v2.0.1)

Upvotes: 2

Related Questions