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.
Asked
Active
Viewed 2,087 times
-2
-
1Instead of adding an image of your data, can you provide a reproducible example of your data ? https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – dc37 Jan 08 '20 at 15:19
-
1In addition to a reproducible example, would you be able to explain a bit more clearly what you want? My guess is one bar per year, with the total bar being total victims, stacked by injuries and fatalities, but injuries and fatalities add to more than total victims in your data. – Jaccar Jan 08 '20 at 15:24
-
Does this answer your question? [Plotting a stacked bar plot?](https://stackoverflow.com/questions/12592041/plotting-a-stacked-bar-plot) – phalteman Jan 08 '20 at 15:57
1 Answers
1
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))

dc37
- 15,840
- 4
- 15
- 32
-
`geom_bar(stat = "identity", position = "fill")` might be closer to OP goal. – Jon Spring Jan 08 '20 at 16:11
-
-