[英]R: Split metricsgraphics histogram by factor
I have a data frame that looks sort of like the following: 我有一个看起来像以下数据帧:
'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 ...
Now I would like to turn this into a histogram of GPA using the metricsgraphics package, but split the data by the factor 'admit'. 现在,我想使用metricsgraphics软件包将其转换为GPA的直方图,但将数据除以“允许”因子。 How is this done?
怎么做?
Using ggplot I can do something like the following: 使用ggplot,我可以执行以下操作:
ggplot(data, aes(gpa)) +
geom_histogram(aes(fill=admit, y=..density..),
position="dodge",
binwidth=0.1
)
but I'm looking at how to specifically do so using metricsgraphics. 但我正在研究如何使用metricsgraphics具体做到这一点。 I currently have
我目前有
mjs_plot(data, x = gpa) %>%
mjs_histogram(bins = 80)
but of course this doesn't split by the factor. 但这当然不会被因素分解。
I think you'll have to produce each plot and arrange it into a grid. 我认为您必须制作每个图并将其排列成网格。 From the package vignette :
从小插图包装中 :
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.