ggplot2:每个区间中单个因子级别的分数的条形图

yer*_*rba 3 r histogram stacked ggplot2

例如,我们有通常的堆积条形图:

ggplot(diamonds, aes(x=color, fill=cut))+geom_bar(position="fill")
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

我想制作相同的情节,但只留下一个"剪切"类型.例如"理想"(紫色).因此,它应该是类似于所有其他具有相同颜色的钻石中理想钻石的分数的直方图.我可以在ggplot中这样做吗?

Bri*_*ggs 5

如果您预先汇总数据,则很简单:

library("plyr")

idl <- ddply(diamonds, .(color), summarize, 
             idealpct = sum(cut=="Ideal")/length(cut))

ggplot(idl, aes(x=color, y=idealpct)) + 
  geom_bar()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述