Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/77.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/13.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 镶嵌面_包裹:Y比例从0到1,基于绘制的变量_R_Ggplot2_Facet Wrap - Fatal编程技术网

R 镶嵌面_包裹:Y比例从0到1,基于绘制的变量

R 镶嵌面_包裹:Y比例从0到1,基于绘制的变量,r,ggplot2,facet-wrap,R,Ggplot2,Facet Wrap,在我的例子(见下文)中,是否有方法根据绘制的变量将镶嵌面包裹中的y轴从0缩放到1?好像.count../max(…count..)将在性别变量上分组。任何想法都将不胜感激 下面是我目前的尝试 ex[c("var1","var2", "gender")] %>% gather(-gender, key="var", value = "value") %>% ggplot(aes

在我的例子(见下文)中,是否有方法根据绘制的变量将镶嵌面包裹中的y轴从0缩放到1?好像
.count../max(…count..)
将在
性别
变量上分组。任何想法都将不胜感激

下面是我目前的尝试

ex[c("var1","var2", "gender")] %>%
  gather(-gender, key="var", value = "value") %>% 
  ggplot(aes(value, fill = gender)) +  geom_histogram(aes(y=..count.. / max(..count..)), stat="count") + 
  facet_wrap(~var + gender, scales = "free_x", ncol= 2)  +
  ylab("% in each group") 
导致

数据样本:

structure(list(row = 1:100, var1 = c(" <25", " <25", " 25-50", 
" 25-50", " 50-75", " <25", " 25-50", " 25-50", " 25-50", " 25-50", 
" 50-75", " 25-50", " 25-50", " 25-50", " <25", " 25-50", " 25-50", 
" 25-50", " 25-50", " 25-50", " 25-50", " 25-50", " 25-50", " 25-50", 
">75", " 25-50", " 25-50", " <25", " <25", " 25-50", " 25-50", 
" 25-50", " 50-75", " 50-75", " 25-50", " 25-50", " 50-75", " 25-50", 
" 25-50", " <25", " 25-50", " 25-50", " 25-50", " 25-50", " <25", 
" 25-50", " 25-50", " 25-50", " 25-50", " 25-50", " 25-50", " 25-50", 
" 25-50", " 50-75", " 50-75", " <25", " 25-50", " <25", " 50-75", 
" <25", " <25", " <25", " 25-50", " <25", " <25", " 25-50", " 50-75", 
" 25-50", " 25-50", " 25-50", " 25-50", " 25-50", " <25", " 25-50", 
" <25", " 25-50", " 25-50", " 25-50", " 25-50", " 25-50", " <25", 
" 50-75", " 25-50", " 25-50", " 25-50", " 25-50", " 25-50", " 50-75", 
" 25-50", " <25", " 25-50", " 25-50", " <25", " 25-50", " <25", 
" <25", " 25-50", " 25-50", " <25", " <25"), var2 = c(0L, 0L, 
0L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 
0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 1L, 1L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
1L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 
1L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L), gender = c("M", "M", "M", "M", "M", "M", "M", "F", "M", 
"M", "M", "M", "M", "M", "M", "M", "M", "M", "M", "M", "M", "M", 
"M", "M", "F", "M", "M", "M", "M", "M", "M", "M", "M", "M", "M", 
"M", "M", "M", "M", "M", "M", "M", "M", "M", "M", "M", "M", "M", 
"M", "M", "M", "M", "M", "M", "M", "M", "M", "M", "M", "M", "M", 
"M", "M", "M", "M", "M", "M", "M", "M", "M", "M", "M", "M", "M", 
"M", "M", "M", "M", "F", "M", "M", "M", "M", "M", "M", "M", "M", 
"F", "M", "M", "M", "M", "M", "M", "M", "M", "M", "M", "M", "M"
)), class = "data.frame", row.names = c(NA, -100L))

structure(list)(row=1:100,var1=c(“据我所知,没有现成的解决方案可以告诉
facet\u wrap
执行您试图实现的那种规范化。相反,您必须手动执行此操作

有(至少)两种方法可以实现这一点:

  • 简单的方法是预先计算(标准化)计数,并利用
    geom\u col
  • 更详细的方法是使用例如
    tapply
    gender
    计算最大计数。对于这种方法,我建议切换到
    geom\u bar
    ,而不是使用
    geom\u直方图
  • 两种方法如下所示:

    库(ggplot2)
    图书馆(tidyr)
    图书馆(dplyr)
    ex[c(“var1”、“var2”、“性别”)]%>%
    聚集(-gender,key=“var”,value=“value”)%%>%
    计数(性别、变量、值)%>%
    按性别划分的组别%>%
    突变(pct=n/max(n))%>%
    ggplot(aes(值、pct、填充=性别))+
    geom_col()+
    镶嵌面包裹(~var+gender,scales=“free\ux”,ncol=2)+
    ylab(“每组中的百分比”)
    

    ex[c(“var1”、“var2”、“性别”)]%>%
    聚集(-gender,key=“var”,value=“value”)%%>%
    ggplot(aes(值,填充=性别))+
    几何图形条(aes(y=…计数../tapply(…计数..,…填充..,函数(x)最大(x))[…填充..]),stat=“计数”)+
    镶嵌面包裹(~var+gender,scales=“free\ux”,ncol=2)+
    ylab(“每组中的百分比”)
    

    你能详细说明什么应该加到100%吗?也许你能给出一个预期的输出?是否有任何方法可以使每个类别(每个变量)中的列总和为1?我目前的解决方案是使用
    geom\u bar(aes(y=…count../tapply(…count..,…fill..,函数(x)sum(x))[…fill..),stat=“count”)
    分别针对每个变量+ylim(0,1)然后使用grid.arrange,但我认为应该有一个更平滑的解决方案?许多thanksHi@Randomcloud.Yep.grid.arrange是不需要的。一般来说,我建议通过预先计算的值使用我的第一种方法更容易处理。在那里,你可以简单地按var和性别对_进行分组。对于第二种方法,你必须做
    ggplot(aes)(value,fill=gender,group=interaction(var,gender))+geom_bar(aes(y=…count…./tapply(…count….,…group….,function(x)sum(x))[…group….]),stat=“count”)
    。这里我们首先映射
    交互(var,gender)
    on
    group
    ,然后可以在geom_bar或tapply中使用它来进行每个变量/性别的计算……在这两种情况下,您仍然需要ylim(0,1)来设置限制。