I would like to make a stacked bart chart showing percentages of the variables SARREN, SCED and SPROP across 4 case studies and have also a column for the totals across the 4 cases.
I modified the database with pivot longer to have one column with the three variables and managed to create a stacked bar chart with counts, but when including position = “fill” I get the message: Ignoring unknown aesthetics: position.
Any help would be much appreciated. I would also like to add labels to each stacked bar with percentages for each variable, but did not find a solution yet. I wold also be very thankful if anyone could give me a hint on this respect.
This is an extract of my ownership database:
structure(list(SPROP = c(100L, 0L, 0L, 70L, 6L, 0L, 0L, 0L, 10L,
45L, 100L), SARREN = c(0L, 100L, 100L, 20L, 94L, 100L, 100L,
100L, 90L, 55L, 0L), SCED = c(0L, 0L, 0L, 10L, 0L, 0L, 0L, 0L,
0L, 0L, 0L), CASE = c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L)), row.names = 10:20, class = "data.frame")
And this is my code so far:
ownership2 <- ownership %>%
pivot_longer(names_to = "variable", values_to = "value",-CASE)%>%
group_by(variable) %>%
ownership3 <- ownership2 %>%
mutate(case1 = as.factor(CASE))
# total count
ownership3 %>%
ggplot(aes(x=case1))+
geom_bar(aes(fill=factor(variable))) +
geom_bar(aes(x="Total", fill=factor(variable)))