使用facet_grid时使用..count ..的R ggplot2 [英] R ggplot2 using ..count.. when using facet_grid

查看:40
本文介绍了使用facet_grid时使用..count ..的R ggplot2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Ubuntu中使用R Studio,并带有标准的更新版R和ggplot2

I am using R studio in Ubuntu, with standard updated R and ggplot2

我尝试在ggplot中创建直方图,并按组将数据分开.

I try to create a histogram in ggplot, and to separate the data by groups.

我需要绘图的y轴来说明子面网格划分的子组中每个bin的频率.

I need the plot's y axis to say the frequency of each bin in the subgroup that was split by the facet grid.

例如,如果我在数据中有两个条目

for example if i have two entries in the data

a group
1 1
2 2

我需要使用facet_grid按组划分,然后显示a代表1的条形,这是组1中示例的100%,反之亦然.

I need to use facet_grid to split by group, and then to show that a has one bar for 1 that is 100% percent of the examples in group 1 and vice versa.

我发现执行此操作的方法是使用(..count ..)/sum(.. count)但是sum(.. count ..)会计算整个数据帧中的频率,并且会给我不想要的结果,

I found out that the way to do it, is using (..count..)/sum(..count) but sum(..count..) will count the frequency of that been in the entire data frame and will give me unwanted results,

我找不到深入使用..count ..

I can't find good documentation for deep using of ..count..

有关特殊ggplot变量的问题

关于..count ..的另一个问题.

文档中没有非常全面的内容,

There is nothing very comprehensive in the docs,

这是我正在使用的示例代码

This is the example code i am using

df <- data.frame(a = 1:10, b = 1:10, group = c(rep(1,5),rep(2,5)))
p<-ggplot(df) + geom_histogram(aes(x = a, y = (..count..)/sum(..count..))) +  
   facet_grid(group ~ .)

您可以看到y轴将包含0.1作为最大值,例如,我希望表明1个值中的100%位于组1中.等等

You can see that the y axis will contain 0.1 as the highest value, i would like it to show that 100% percent of the 1 values are in group 1 for example. etc.

感谢Jimbou给出的答案和对适用于离散数据的精心构建的遍历的引用,请注意,我在这里遇到的真正问题将需要使用连续数据,并且将多个值组合在一起的垃圾箱,此外,没有有关如何使用..count ..函数执行此操作的适当文档,因此,我认为这对于找到解决方案而不是使用漫游功能很重要

Thanks to Jimbou for the answer and reference to a well built walk around that is suitable for discrete data, pls note that the real problem i am having here will need to use continuous data, and bins that group more than one value, furthermore, there is no proper documentation about how to do this with the ..count.. function and therefor I believe this is important to find a solution and not to use walk around

推荐答案

经过了很多次尝试,并给出了非常好的指导,我发现,在Jimbou和Shayaa的答案之间进行了一些添加和融合,并添加了一些代码,效果很好.

After a lot of playing around, and very good directions you all gave, i found that with a little addition and blend between Jimbou's and Shayaa's answers, and some added code this works beautifully.

t <- data %>% group_by(group,member,v_rate) %>% tally %>% mutate(f = n/sum(n))

将获取数据并按组,成员,v_rate分组,并将每个组的计数除以总和(组中的相对频率)

will take the data and will group by group, member, v_rate, and will add count of each group divided by the sum (relative frequency in the group)

我们要使用ggplot2创建直方图,并将这些值用作直方图的权重函数,否则全部都是徒劳的,

than we want to create the histogram with ggplot2 and use those values as the weight function of the histogram, otherwise it was all for vain,

 p <- ggplot(t, aes(x = v_rate, weight = f)) + geom_histogram() + facet_grid(group ~ member)

效果很好.

这篇关于使用facet_grid时使用..count ..的R ggplot2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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