这是与上一个类似的问题here https://stackoverflow.com/questions/9563368/create-stacked-barplot-where-each-stack-is-scaled-to-sum-to-100。您可以使用position = "fill"
ggplot 中的参数将条形缩放至 100% 高度。这scale_y_continuous(labels = scales::percent)
命令将频率范围从 0-1 更改为 0-100%。
library(ggplot2)
x <- c("good", "good", "bad", "bad", "bad", "bad", "perfect", "perfect", "perfect")
y <- c("exercise1", "exercise2", "exercise3", "exercise1", "exercise2", "exercise3", "exercise1", "exercise2", "exercise3")
dt <- data.frame(x, y)
# Build plot
ggplot(dt, aes(x, fill = y)) +
geom_bar(position = "fill") +
scale_y_continuous(labels = scales::percent)