【问题标题】:R: Split metricsgraphics histogram by factorR:按因子拆分metricsgraphics直方图
【发布时间】:2015-11-20 10:57:11
【问题描述】:

我有一个看起来像下面这样的数据框:

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

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

【问题讨论】:

    标签: r metricsgraphicsjs


    【解决方案1】:

    我认为您必须制作每个情节并将其排列成网格。来自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)))
    

    【讨论】:

      猜你喜欢
      • 2016-03-06
      • 1970-01-01
      • 2013-03-24
      • 1970-01-01
      • 1970-01-01
      • 2017-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多