繁体   English   中英

R:按因子拆分指标图形直方图

[英]R: Split metricsgraphics histogram by factor

我有一个看起来像以下数据帧:

'data.frame':   400 obs. of  4 variables:
 $ admit: Factor w/ 2 levels "rejected","accepted": 1 2 2 2 1 2 2 1 2 1 ...
 $ gpa  : num  3.61 3.67 4 3.19 2.93 3 2.98 3.08 3.39 3.92 ...

现在,我想使用metricsgraphics软件包将其转换为GPA的直方图,但将数据除以“允许”因子。 怎么做?

使用ggplot,我可以执行以下操作:

ggplot(data, aes(gpa)) + 
geom_histogram(aes(fill=admit, y=..density..),
           position="dodge",
           binwidth=0.1
          )

但我正在研究如何使用metricsgraphics具体做到这一点。 我目前有

mjs_plot(data, x = gpa) %>%
     mjs_histogram(bins = 80)

但这当然不会被因素分解。

我认为您必须制作每个图并将其排列成网格。 从小插图包装中

moar_plots <- lapply(1:7, function(x) {
  mjs_plot(rbeta(10000, x, x), width="250px", height="250px", linked=TRUE) %>%
    mjs_histogram(bar_margin=2) %>%
    mjs_labs(x_label=sprintf("Plot %d", x))
})

mjs_grid(moar_plots, nrow=4, ncol=3, widths=c(rep(0.33, 3)))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM