Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R ggplot2:每个箱子中因子单级分数的条形图_R_Ggplot2_Histogram_Stacked - Fatal编程技术网

R ggplot2:每个箱子中因子单级分数的条形图

R ggplot2:每个箱子中因子单级分数的条形图,r,ggplot2,histogram,stacked,R,Ggplot2,Histogram,Stacked,例如,我们有通常的堆叠条形图: ggplot(diamonds, aes(x=color, fill=cut))+geom_bar(position="fill") 我想画同样的图,但只留下一个“切割”类型。例如“理想”(紫色)。所以,它应该类似于理想钻石在所有其他具有相同颜色的钻石中所占比例的直方图。我可以在ggplot中这样做吗?如果您预先汇总数据,这很简单: library("plyr") idl <- ddply(diamonds, .(color), summarize,

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

ggplot(diamonds, aes(x=color, fill=cut))+geom_bar(position="fill")


我想画同样的图,但只留下一个“切割”类型。例如“理想”(紫色)。所以,它应该类似于理想钻石在所有其他具有相同颜色的钻石中所占比例的直方图。我可以在ggplot中这样做吗?

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

library("plyr")

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

ggplot(idl, aes(x=color, y=idealpct)) + 
  geom_bar()
库(“plyr”)

idl是的,当然。谢谢你。我只是想知道,也许它是在ggplot2中实现的。我认为这是一件非常常见的事情