简体   繁体   中英

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'. How is this done?

Using ggplot I can do something like the following:

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. 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)))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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