R语言 具有嵌套分组变量的多行y轴标签

kq0g1dla  于 2023-10-13  发布在  其他
关注(0)|答案(1)|浏览(99)

我希望两个不同嵌套分组变量的水平显示在图的y轴中的单独行上,而不是图例中。我现在有这样的代码:

data <- tibble::tribble(                       ~ term,   ~estimate, ~conf.low, ~conf.high,           ~group,           
"HH cultivated plots",                                         0.0163,  -0.0130,    0.0456, "Extensive Margin",
"# of farm assets (hoe, spade, axe), all HHs",                 0.356,    0.143,     0.569,  "Assets",          
"Number of plots",                                            -0.0539,  -0.158,     0.0498, "Intensive Margin",
"Farm size",                                                   0.208,   -0.0394,    0.456,  "Intensive Margin",
"# of HH members that cultivated plots, all HHs",              0.0541,  -0.0647,    0.173,  "Intensive Margin",
"Avg # of days HH members spent cultivating plots, all HHs",  -0.192,   -0.738,     0.354,  "Intensive Margin",
"Annual agricultural revenue",                                 0.329,    0.170,     0.487,  "Revenue")

ag_coef_plot <- data %>%
  
  ggplot(
    
    aes(x =  estimate, 
        y = term,
        color = group
    
  )) +
  
  geom_pointrange( # Confidence interval
    
    aes(
      xmin = conf.low, 
      xmax = conf.high
    ), 
    
    show.legend = FALSE,
    
  ) +
  
  scale_x_continuous(name = "") +
  
  geom_vline(xintercept = 0) +
  
  scale_y_discrete(name = "", 
                   limits = rev, 
                   expand = c(.05, 0)) + 
  
  theme_minimal() + 
  
  theme(
    
    plot.title          = element_text(size = 15, hjust = 0.5, face = "bold"),
    
    plot.title.position = "plot",
    
    plot.background     = element_rect(color = "black"), # For some reason this is necessary to ensure background color ISN'T black. Go figure
    
    axis.text.y         =  element_text(size = 15),
    
    axis.text.x         =  element_text(size = 15),
    
    legend.position     = "left",
    
    legend.title        = element_blank(),
    
    legend.text         = element_blank()
    
    
  ) +
  
  scale_color_brewer(palette = "Set2") 

ag_coef_plot

这段代码生成了这样的图:

但我想实现这样的东西,在完美的场景中,组标签与点的颜色相同:

我试过这篇文章的解决方案,但我不能让它工作:Multirow axis labels with nested grouping variables

ndasle7k

ndasle7k1#

分组变量之间的垂直线不对应于ggplot中的任何主题元素,但如果您喜欢简单的框,则可以使用ggplot和ggtext实现其余部分:

library(tidyverse)
library(ggtext)
library(RColorBrewer)

data %>%
  mutate(
    group = gsub(" ", "<br>", group),
    color =  brewer.pal(4, "Set2")[match(group, levels(factor(group)))],
    name = glue::glue("<i style='color:{color}'>{group}</i>"),
    name = fct_reorder(name, as.numeric(factor(group)))) %>%
  ggplot(aes(estimate, term, color = group)) +
  geom_pointrange(aes(xmin = conf.low, xmax = conf.high), show.legend = FALSE) +
  geom_vline(xintercept = 0) +
  labs(x = NULL) +
  scale_y_discrete(NULL, limits = rev) +
  scale_color_brewer(palette = "Set2") +
  facet_grid(name ~ ., scales = "free_y", switch = "y", space = "free_y") +
  theme_minimal(base_size = 15) + 
  theme(strip.placement     = "outside",
        strip.text.y.left   = element_markdown(size = 15, angle = 0,
                                               margin = margin(10, 10, 10, 10)),
        panel.spacing.y     = unit(0, "mm"),
        strip.background    = element_rect()
  )

相关问题