如何在分组条形图中的多个面板中包装冗长的标签

How to wrap lengthy labels in multiple panels in grouped bar charts

此问题涉及如何在聚类 column/bar 图表中包装冗长的标签,以使标签出现在多个面板的多行(或多行)上。考虑以下数据

df  <- data.frame(group=c("Treated very satisfied", "Treated very satisfied",
                           "Treated not satisfied","Treated not satisfied",
                           "Untreated very satisfied","Untreated very satisfied", 
                            "Untreated not satisfied","Untreated not satisfied"), 
                  cost=c("low","high","low","high","low","high","low","high"),     
                  treatment=c("treated","treated","treated","treated",
                            "untreated","untreated","untreated","untreated") ,
                  value=c(2.3,5.7,4.0,3.1,9.4,3.1,2.0,-1.6)) 

我们使用组作为 x 轴,值作为 y 轴。从该数据集中,使用以下代码生成后续图表

#REORDER
df$group <- factor(df$group, levels = c("Treated very satisfied", 
                            "Treated not satisfied",
                            "Untreated very satisfied",
                            "Untreated not satisfied"))

ggplot(data=df,aes(y = value, x = group, fill = cost)) +
geom_bar(stat="identity",position='stack') + ylab("Y label") +
theme(legend.direction = "horizontal",legend.position = "bottom",
    legend.spacing.x = unit(0.1, 'cm'))+
theme(legend.title=element_blank())+
geom_text(aes(label = ifelse(value !=0, value, "")), 
         position = position_stack(vjust=0.5))+
facet_grid( ~ treatment)    

在 x 轴上,我希望在面板 1 中看到 "Treated very satisfied" 和 "Treated not satisfied",并且
"Untreated very satisfied" and "Untreated not satisfied" in panel 2.如图所示,这些文字重叠,所以看不清楚。我正在寻找一种方法 format/wrap 文本(不旋转),每个文本沿 x 轴分成多行(即每行三行),例如,Treated\n very\n 满足两个面板中的所有标签。

我考虑过几次解决这个问题的尝试,例如使用包 "stringr" 中的函数 "str_wrap"(来自 Auto wrapping of labels via labeller=label_wrap in ggplot2)。但是,由于图中的 panels/clusters,这不起作用。我将不胜感激任何帮助。

首先,我建议在 facet_grid 中使用 scales = "free_x" 以仅显示每个面板中使用的类别。第二。使用链接 post 中的解决方案对我来说包装标签效果很好。

library(ggplot2)
library(stringr)

df  <- data.frame(group=c("Treated very satisfied", "Treated very satisfied",
                          "Treated not satisfied","Treated not satisfied",
                          "Untreated very satisfied","Untreated very satisfied", 
                          "Untreated not satisfied","Untreated not satisfied"), 
                  cost=c("low","high","low","high","low","high","low","high"),     
                  treatment=c("treated","treated","treated","treated",
                              "untreated","untreated","untreated","untreated") ,
                  value=c(2.3,5.7,4.0,3.1,9.4,3.1,2.0,-1.6))

#REORDER
df$group <- factor(df$group, levels = c("Treated very satisfied", 
                                        "Treated not satisfied",
                                        "Untreated very satisfied",
                                        "Untreated not satisfied"))

ggplot(data=df,aes(y = value, x = group, fill = cost)) +
  geom_bar(stat="identity",position='stack') + ylab("Y label") +
  theme(legend.direction = "horizontal",legend.position = "bottom",
        legend.spacing.x = unit(0.1, 'cm'))+
  theme(legend.title=element_blank())+
  geom_text(aes(label = ifelse(value !=0, value, "")), 
            position = position_stack(vjust=0.5))+
  scale_x_discrete(labels = function(x) str_wrap(x, width = 10)) +
  facet_grid( ~ treatment, scales = "free_x")    

reprex package (v0.3.0)

于 2020 年 6 月 8 日创建