How to Make a Stacked Bar Chart in Ggplot2? [Duplicate]

I'm new to R language and I've some data which are formatted like that: I would like to get an ggplot with stacked bar like this: But I've no idea how doing it. I try a lot of things, but nothing work.I would like to get as a biggest bar the "Total victims", and put the other stacked bar with injured/fatalities.

3

You need to reshape your data in a longer format in order to plot them using ggplot2. You can achieve this by using the pivot_longer function of tidyr package:

library(tidyr)
library(dplyr)
library(ggplot2)

df %>% pivot_longer(.,-year, names_to = "Variable", values_to = "Value") %>%
  ggplot(aes(x = year, y = Value, fill = Variable))+
  geom_bar(stat = "identity", position = "fill")+
  scale_x_continuous(breaks = 2005:2017)+
  ylab("Victims")

If you want to specify a specific order of your stack, you can change the order of levels of the factor variable:

library(tidyr)
library(dplyr)
library(ggplot2)

df %>% pivot_longer(.,-year, names_to = "Variable", values_to = "Value") %>%
  mutate(Variable = factor(Variable, levels = c("injured", "total", "fatal")))%>%
  ggplot(aes(x = year, y = Value, fill = Variable))+
  geom_bar(stat = "identity", position = "fill")+
  scale_x_continuous(breaks = 2005:2017)+
  ylab("Victims")

Does it answer your question ?

Data

structure(list(year = 2017:2005, fatal = c(113, 173, 210, 41, 
76, 99, 29, 9, 57, 28, 65, 24, 21), injured = c(558, 258, 179, 
60, 32, 111, 37, 5, 46, 30, 61, 16, 13), total = c(670, 418, 
360, 93, 101, 204, 65, 17, 100, 57, 123, 38, 31)), class = "data.frame", row.names = c(NA, 
-13L))
3
Alexander Ross

Alexander Ross

Gaming, Esports & Interactive Media Writer

Alexander Ross has covered the video game industry for a decade, writing deep dives on game design, esports tournaments, VR developments, and gaming culture.

Share this article
Twitter Facebook Pinterest