First summarise and transform your data:
library(dplyr)
d2 <- d %>%
group_by(groupchange, Symscore3) %>%
summarise(count = n()) %>%
mutate(perc = count/sum(count))
Then you can plot it:
ggplot(d2, aes(x = factor(groupchange), y = perc*100, fill = factor(Symscore3))) +
geom_bar(stat="identity", width = 0.7) +
labs(x = "Groupchange", y = "percent", fill = "Symscore") +
theme_minimal(base_size = 14)
this gives:
Alternatively, you can use the percent
function from the scales
package:
brks <- c(0, 0.25, 0.5, 0.75, 1)
ggplot(d2, aes(x = factor(groupchange), y = perc, fill = factor(Symscore3))) +
geom_bar(stat="identity", width = 0.7) +
scale_y_continuous(breaks = brks, labels = scales::percent(brks)) +
labs(x = "Groupchange", y = NULL, fill = "Symscore") +
theme_minimal(base_size = 14)
which gives:
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…