使用 sub-categories 移动 multi-row(多面)ggplot2 的文本

Moving text of multi-row (faceted) ggplot2 with sub-categories

关于如何将变量中级别的文本对齐到更接近变量名称的任何建议?我正在使用包 GGally 和 broom.mixed 绘制模型系数,它会根据我感兴趣的协变量自动合并分面。我发现 strip.text.y.left(element.text = hjust = 1) 让我最接近,但我试图消除在标签之间奖励 space 以便为实际图表腾出更多空间。

require(broom.helpers)
require(broom.mixed)
require(GGally)

# model4 is my glmer model
ggcoef_model(model4, conf.int = TRUE, include = c("Openness","Slope^2","Distance to trails and rec","Dom.Veg",
                                                  "Northness","Burn","Distance to roads"), intercept = FALSE, 
             add_reference_rows = FALSE, show_p_values = FALSE, signif_stars = FALSE, stripped_rows=FALSE, 
             point_size=3, errorbar_height = 0.2) + 
  xlab("Coefficients") +
  theme(plot.title = element_text(hjust = 0.5, face="bold", size = 24), strip.text.y = element_text(size = 17),
        strip.text.y.left = element_text(hjust = 1), strip.placement = "outside",
        axis.title.x = element_text(size=17, vjust = 0.3), legend.position = "right", 
        axis.text=element_text(size=15.5, hjust = 1), 
        legend.text = element_text(size=14))

理想情况下,我认为放置在内部的标签看起来最好,但这可能会导致 Dom.Veg 类别出现问题,因为主要变量标题应位于级别之前。另一个不错的选择是在外部放置 left-aligned 变量名(因此删除 strip.text.y.left(element.text = hjust = 1) 行,并使类别更接近变量名。这甚至可能吗?

我认为直接标注可能是这里的答案:

p <- ggcoef_model(model4, conf.int = TRUE, 
             include = c("Openness","Slope^2","Distance to trails and rec","Dom.Veg",
                         "Northness","Burn","Distance to roads"), 
             intercept = FALSE, 
             add_reference_rows = FALSE, 
             show_p_values = FALSE, 
             signif_stars = FALSE, 
             stripped_rows=FALSE, 
             point_size=3, errorbar_height = 0.2) + 
  xlab("Coefficients") +
  theme(plot.title = element_text(hjust = 0.5, face="bold", size = 24), 
        strip.text.y = element_text(size = 17),
        strip.text.y.left = element_text(hjust = 1), 
        strip.placement = "outside",
        axis.title.x = element_text(size=17, vjust = 0.3), 
        legend.position = "right", 
        axis.text=element_text(size=15.5, hjust = 1), 
        legend.text = element_text(size=14))

p + theme(axis.text.y = element_blank()) + 
  geom_text(aes(label = label), 
            data = p$data[p$data$var_class == "factor",], 
            nudge_y = 0.4, color = "black")

注意,我这里没有你的数据集,所以不得不创建一个类似的数据集(这比实际回答问题要难得多!)

set.seed(1)

df <- data.frame(Openness = runif(1000),
           `Slope^2` = runif(1000),
            `Distance to trails and rec` = runif(1000),
            `Northness` = runif(1000),
            `Burn` = runif(1000),
            `Distance to roads` = runif(1000),
            `Dom.Veg` = factor(c(rep(c("NADA", "Aspen", "PJ", "Oak/Shrub",
                                       "Ponderosa", "Mixed Con.",
                                       "Wet meadow/pasture"), each = 142),
                                 rep("Ponderosa", 6)), 
                                 levels = c("NADA", "Aspen", "PJ", "Oak/Shrub",
                                       "Ponderosa", "Mixed Con.",
                                       "Wet meadow/pasture"
                                 )), check.names = FALSE)

df$outcome <- with(df, Openness * 0.2 +
            `Slope^2` * -0.15 +
            `Distance to trails and rec` * -0.9 +
            `Northness` * -0.1 +
            `Burn` * 0.5 +
            `Distance to roads` * -0.6 +
            rnorm(1000) + c(0, 2.1, -1, -0.5, -1, -0.1, 1.2)[as.numeric(Dom.Veg)]
    )

model4 <- glm(outcome ~ Openness + `Slope^2` + `Distance to trails and rec` + 
                Northness + Burn + `Distance to roads` + `Dom.Veg`, data = df)