如何在 R 中为使用百分比而不是计数作为 y 轴的数据框创建条形图?

How do you create a bar graph for a data frame in R that uses percentages as the y-axis instead of a count?

如果我有这样的数据(但更大):

ID    Rating
12    Good
12    Good
16    Good
16    Bad
16    Very Bad
34    Very Good
38    Very Bad
52    Bad

我需要做什么才能让绘图显示每种类型的计数百分比。基本上,对于每种类型的评级,图表应该看起来像 x 轴上的 4 个条,y 轴应该是评级出现时间的百分比。例如,上面的数据框将有 4 个条形,非常差和差为 25%,好为 37.5%,非常好为 12.5%。我真的很想在 ggplot2 中得到答案,但是,因为我真的根本找不到这个,所以 R 中的任何东西都可以。

library(ggplot2)

# create some data
DT <- data.frame(ID=1:10,Rating=sample(c("Very Good","Good","Bad","Very Bad"),20,replace=TRUE))

ggplot(DT, aes(factor(Rating))) + geom_bar()

参考:ggplot2 docs

用于显示基数 barplots 的比例,实际比例显示为横条上的文本:

tmp.table <- prop.table(table(dat$Rating))
with(dat, barplot(tmp.table, xlab= "Rating", ylab="proportion", ylim=c(0,.40)))
text(x = c(0.75, 2, 3.1, 4.25), y = tmp.table + .01, labels=paste(tmp.table*100,"%"))

结果

数据

dat <- read.csv(text="Rating
Good
Good
Good
Bad
Very Bad
Very Good
Very Bad
Bad")

这是我找到的最佳答案:

# create data
data <- data.frame(ID = as.factor(c(12,12,16,16,16,34,38,52)),
               Rating = c("Good","Good","Good","Bad","Very Bad","Very Good","Very Bad","Bad"))

# get summary table of Rating
t <- table(data$Rating)
# get percentage list
percent <- as.vector(t)/nrow(data)

# plot
library(ggplot2)
ggplot(data = data,aes(x=Rating)) + 
    geom_bar(aes(y = (..count..)/sum(..count..))) +
    ylab("Percentage") +
    ylim(0,0.4)