让ggplot2直方图显示y轴上的逐级百分比 [英] Let ggplot2 histogram show classwise percentages on y axis

查看:2908
本文介绍了让ggplot2直方图显示y轴上的逐级百分比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  library(ggplot2)
data = diamonds [,c('carat','color')]
data = data [data $ color%in% c('D','E'),]

我想比较克拉的直方图跨越颜色D和E,并使用y轴上的分类百分比。我试过的解决方案如下:



解决方案1:

  ggplot(data = data,aes(carat,fill = color))+ geom_bar(aes(y = .. density ..),position ='dodge',binwidth = 0.5)+ ylab(Percentage)+ xlab 克拉)


这是不正确的,因为y轴显示了估计密度的高度。



解决方案2:

  ggplot(data = data,aes(carat,fill =颜色))+ geom_histogram(aes(y =(.. count ..)/ sum(.. count ..)),position ='dodge',binwidth = 0.5)+ ylab(Percentage)+ xlab(Carat )


这也不是我想要的,因为用于计算y轴比率的分母是总数D + E。



有没有办法显示用ggplot2的叠加直方图分类百分比?也就是说,我不希望在y轴上显示(bin中的obs数量)/ count(D + E),而是显示(bin中的obs数量)/ count(D)和(bin中的obs数量) / count(E)分别为两种颜色类别。感谢。

解决方案

您可以通过使用 ..组来按比例缩放它们。特殊变量以将 .. count .. 向量进行子集化。这是非常丑陋的,因为所有的点,但这里它去

  ggplot(aes(carat,fill = color) )+ 
geom_histogram(aes(y = c(.. count .. [.. group .. == 1] / sum(.. count .. [.. group .. == 1]),
..count .. [.. group .. == 2] / sum(.. count .. [.. group .. == 2]))* 100),
position ='dodge ',binwidth = 0.5)+
ylab(Percentage)+ xlab(Carat)


library(ggplot2)
data = diamonds[, c('carat', 'color')]
data = data[data$color %in% c('D', 'E'), ]

I would like to compare the histogram of carat across color D and E, and use the classwise percentage on the y-axis. The solutions I have tried are as follows:

Solution 1:

ggplot(data=data, aes(carat, fill=color)) +  geom_bar(aes(y=..density..), position='dodge', binwidth = 0.5) + ylab("Percentage") +xlab("Carat")

This is not quite right since the y-axis shows the height of the estimated density.

Solution 2:

 ggplot(data=data, aes(carat, fill=color)) +  geom_histogram(aes(y=(..count..)/sum(..count..)), position='dodge', binwidth = 0.5) + ylab("Percentage") +xlab("Carat")

This is also not I want, because the denominator used to calculate the ratio on the y-axis are the total count of D + E.

Is there a way to display the classwise percentages with ggplot2's stacked histogram? That is, instead of showing (# of obs in bin)/count(D+E) on y axis, I would like it to show (# of obs in bin)/count(D) and (# of obs in bin)/count(E) respectively for two color classes. Thanks.

解决方案

You can scale them by group by using the ..group.. special variable to subset the ..count.. vector. It is pretty ugly because of all the dots, but here it goes

ggplot(data, aes(carat, fill=color)) +
  geom_histogram(aes(y=c(..count..[..group..==1]/sum(..count..[..group..==1]),
                         ..count..[..group..==2]/sum(..count..[..group..==2]))*100),
                 position='dodge', binwidth=0.5) +
  ylab("Percentage") + xlab("Carat")

这篇关于让ggplot2直方图显示y轴上的逐级百分比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆