I have the following sample data with three different cost-types and a year-column:
library(tidyverse)
# Sample data
costsA <- sample(100:200,30, replace=T)
costsB <- sample(100:140,30, replace=T)
costsC <- sample(20:20,30, replace=T)
year <- sample(c("2000", "2010", "2030"), 30, replace=T)
df <- data.frame(costsA, costsB, costsC, year)
My goal is to plot these costs in a stacked barplot, so that I can compare the mean-costs between the three year-categories. In order to do so I aggregated the values:
df %>% group_by(year) %>%
summarise(n=n(),
meanA = mean(costsA),
meanB = mean(costsB),
meanC = mean(costsC)) %>%
ggplot( ... ) + geom_bar()
But how can I plot the graph now? In the x-axis there should be the years and in the y-axis the stacked costs.
df %>% group_by(year) %>% summarise(n=n(), meanA = mean(costsA), meanB = mean(costsB), meanC = mean(costsC)) %>% gather("key", "value", - c(year, n)) %>% ggplot(aes(x = year, y = value, group = key, fill = key)) + geom_bar(stat = "identity")
– amatsuo_net