使用 facet_nested_wrap GGPLOT 将因子绘制到多行

plotting factors to multiple rows with facet_nested_wrap GGPLOT

我想就 facet_nested_wrap 一个论点寻求您的帮助。

让我先描述一下我的数据。

我正在使用 TRACE 贿赂风险矩阵及其 4 个域和 9 个子域。

“机会”有 3 个子域:“交互”、“期望”、“杠杆”。

“威慑”有 2 个子域:“劝阻”、“执法”。

“透明度”有 2 个子域:“流程”、“兴趣”。

“监督”有 2 个子域:“新闻自由”、“公民社会”。

函数 facet_nested_wrap 可以在包 ggh4x 中找到,可以从 GitHub 使用

安装
devtools::install_github('teunbrand/ggh4x')

我做了下图:

library(dplyr)
library(ggh4x)

df <- data.frame(

scope = c("GPFG","GPFG","GPFG","GPFG","GPFG","GPFG","GPFG","GPFG","GPFG",
"ABP", "ABP", "ABP", "ABP", "ABP", "ABP", "ABP", "ABP", "ABP"),

value = c(18.69760, 25.15200, 30.54616, 34.35660, 28.47889, 18.44293,   
16.99592, 19.10984, 22.32335, 20.00916, 28.66086, 30.22386, 37.56469, 31.69484, 20.42255, 18.80105, 23.36370, 25.05658),

domain = c("Opportunity", "Opportunity", "Opportunity", "Deterrence",
"Deterrence", "Transparency", "Transparency", "Oversight", "Oversight", "Opportunity", "Opportunity", "Opportunity", "Deterrence", "Deterrence", "Transparency", "Transparency", "Oversight", "Oversight"),

subdomain = c("Interaction", "Expectation", "Leverage", "Dissuasion", "Enforcement", "Processes", "Interests", "Free press", "Civil society", "Interaction", "Expectation", "Leverage", "Dissuasion", "Enforcement", "Processes", "Interests", "Free press", "Civil society")
) 

df %>%
  ggplot(aes(x= as.numeric(value), y= reorder(domain, -value), fill = scope)) + 
  geom_col(colour = "black", width= 0.6, 
           position = position_dodge(width= 0.8)) + 
  theme_bw() + 
  coord_flip() + 
  geom_text(aes(label = round(value, 1)), vjust=-0.5, color="black", 
            size=3.5, position = position_dodge(0.8)) + 
  labs(title = "Weighted risk scores per portfolio",
       x = "",
       y = "", 
       fill = NULL) + 
  theme(legend.position = "bottom",
        axis.text.y = element_text(size = 12),
        plot.title = element_text(hjust = 0.5, size = 22),
        axis.ticks.x = element_blank(),
        axis.text.x = element_blank(),
        strip.text.x = element_text(size = 14),
        legend.text = element_text(size = 16)) + 
  facet_nested_wrap(vars(domain, subdomain), scales = "free") + 
  expand_limits(x=c(0, 50))

绘图时,我希望将 4 个域的刻面除以 4 行。现在我只有 3 个,域在某种程度上被“削减”了一半(域“监督”与“机会”一起位于第一行)。

我想将第一个域“Opportunity”绘制为第一行,将第二个域“Oversight”绘制在第二行,依此类推。问题是“机会”有3个子域,而其余只有2个。

我查看了流血参数,但无法正常工作:( https://rdrr.io/github/teunbrand/ggh4x/man/facet_nested_wrap.html

有人可以帮助我吗? 提前谢谢你

facet_nested_wrap() 函数主要重组条带,这意味着如果您无法在 facet_wrap() 中正确获得布局,那么在 facet_nested_wrap() 中也不会正确。这也在阅读的文档的详细部分中:'This function only merges strips in the same row or column as they appear through regular facet_wrap() layout behaviour'。也就是说,一种补救措施可能是用 geom_blank().

的虚拟变量填充 2 面板域的第三面板
library(dplyr)
library(ggh4x)

df <- data.frame(
  
  scope = c("GPFG","GPFG","GPFG","GPFG","GPFG","GPFG","GPFG","GPFG","GPFG",
            "ABP", "ABP", "ABP", "ABP", "ABP", "ABP", "ABP", "ABP", "ABP"),
  
  value = c(18.69760, 25.15200, 30.54616, 34.35660, 28.47889, 18.44293,   
            16.99592, 19.10984, 22.32335, 20.00916, 28.66086, 30.22386, 37.56469, 31.69484, 20.42255, 18.80105, 23.36370, 25.05658),
  
  domain = c("Opportunity", "Opportunity", "Opportunity", "Deterrence",
             "Deterrence", "Transparency", "Transparency", "Oversight", "Oversight", "Opportunity", "Opportunity", "Opportunity", "Deterrence", "Deterrence", "Transparency", "Transparency", "Oversight", "Oversight"),
  
  subdomain = c("Interaction", "Expectation", "Leverage", "Dissuasion", "Enforcement", "Processes", "Interests", "Free press", "Civil society", "Interaction", "Expectation", "Leverage", "Dissuasion", "Enforcement", "Processes", "Interests", "Free press", "Civil society")
) 

df %>%
  ggplot(aes(x= as.numeric(value), y= reorder(domain, -value), fill = scope)) + 
  geom_col(colour = "black", width= 0.6, 
           position = position_dodge(width= 0.8)) + 
  geom_blank(
    data = data.frame(
      domain = c("Deterrence", "Oversight", "Transparency"), 
      subdomain = "z"
    ),
    inherit.aes = FALSE
  ) +
  theme_bw() + 
  coord_flip() + 
  geom_text(aes(label = round(value, 1)), vjust=-0.5, color="black", 
            size=3.5, position = position_dodge(0.8)) + 
  labs(title = "Weighted risk scores per portfolio",
       x = "",
       y = "", 
       fill = NULL) + 
  theme(legend.position = "bottom",
        axis.text.y = element_text(size = 12),
        plot.title = element_text(hjust = 0.5, size = 22),
        axis.ticks.x = element_blank(),
        axis.text.x = element_blank(),
        strip.text.x = element_text(size = 14),
        legend.text = element_text(size = 16)) + 
  facet_nested_wrap(vars(domain, subdomain), scales = "free",
                    ncol = 3) + 
  expand_limits(x=c(0, 50))

reprex package (v1.0.0)

于 2021-02-18 创建

附带说明:'bleed' 参数是关于条带以及它们如何合并的,但也许文档在这方面可以更清楚。