代码之家  ›  专栏  ›  技术社区  ›  a_todd12

对facet_grid的特定行中的系数进行排序

  •  0
  • a_todd12  · 技术社区  · 3 年前

    我正在使用 facet_grid() 以显示2x2个不同组合的模型类型,用于种族群体和项目参与程度。

    通过使用 scales = "free" 我能够为每一行分离出y轴,并且只显示相关系数。但是,我如何在每个面板行中指定模型/变量顺序?通常,我会做一些类似的事情:

    model_order <- c("White", "Black", "Hispanic")
    

    然后将其传递给 scale_x_discrete() 。(然后依次为High、Medium和Low)。

    但在这种情况下,这似乎不起作用,因为使用 scales=“免费” .是否有控制订单的变通方法?

    密码

    mylabels <- c("1" = "Linear",
                           "2" = "Logit",
                           "3" = "Race",
                           "4" = "Level")
    
    ggplot(dx, aes(x = var, y = coef,
           ymin = ci_lower, ymax = ci_upper)) +
      geom_point(size = 2) +
      geom_errorbar(width = 0.1,
                    size = 1) +
      facet_grid(effect~model,
                 scales = "free",
                 labeller = as_labeller(mylabels)) + 
      scale_y_continuous(breaks = seq(-3, 3, by = 1)) +
      coord_flip() +
      theme_bw(base_size = 15) +
      theme(legend.position = "none")
    

    数据

    structure(list(var = c("White", "Black", "Hispanic", "White", 
    "Black", "Hispanic", "High", "Medium", "Low", "High", "Medium", 
    "Low"), coef = c(1.64, 1.2, 0.4, 1.45, 0.17, 0.6, 1.04, 0.05, 
    -0.74, -0.99, -0.45, -0.3045), ci_lower = c(1.3, 0.86, 0.06, 
    1.11, -0.17, 0.26, 0.7, -0.29, -1.08, -1.33, -0.79, -0.6445), 
        ci_upper = c(1.98, 1.54, 0.74, 1.79, 0.51, 0.94, 1.38, 0.39, 
        -0.4, -0.65, -0.11, 0.0355), model = c(1, 1, 1, 2, 2, 2, 
        1, 1, 1, 2, 2, 2), effect = c(3, 3, 3, 3, 3, 3, 4, 4, 4, 
        4, 4, 4)), class = c("spec_tbl_df", "tbl_df", "tbl", "data.frame"
    ), row.names = c(NA, -12L), spec = structure(list(cols = list(
        var = structure(list(), class = c("collector_character", 
        "collector")), coef = structure(list(), class = c("collector_double", 
        "collector")), ci_lower = structure(list(), class = c("collector_double", 
        "collector")), ci_upper = structure(list(), class = c("collector_double", 
        "collector")), model = structure(list(), class = c("collector_double", 
        "collector")), effect = structure(list(), class = c("collector_double", 
        "collector"))), default = structure(list(), class = c("collector_guess", 
    "collector")), skip = 1L), class = "col_spec"))
    
    1 回复  |  直到 3 年前
        1
  •  1
  •   Vinícius Félix    3 年前

    您可以将变量定义为因子,然后对其级别进行重新排序:

    library(dplyr)
    library(ggplot2)
    
    mylabels <- c("1" = "Linear",
                  "2" = "Logit",
                  "3" = "Race",
                  "4" = "Level")
    dx %>% 
      mutate(var = forcats::fct_relevel(var,"High","Medium")) %>%
      ggplot(aes(x = var, y = coef,
                     ymin = ci_lower, ymax = ci_upper)) +
      geom_point(size = 2) +
      geom_errorbar(width = 0.1,
                    size = 1) +
      facet_grid(effect~model,
                 scales = "free",
                 labeller = as_labeller(mylabels)) + 
      scale_y_continuous(breaks = seq(-3, 3, by = 1)) +
      coord_flip() +
      theme_bw(base_size = 15) +
      theme(legend.position = "none")
    
    推荐文章