ggplot 总结箱线图

ggplot summarizing boxplot

遗憾的是我的 ggplot 有问题。

这是我的数据框的一个子集:

Name <- c('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16', '17','18','19','20','21','22','23','24','25','26','27','28','29','30','31','32')
Gruppe <-c('A','A','B','B','C', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C',  'C',  'C', 'C','A','A','B','B','C', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C',  'C',  'C', 'C')
Group <-c('A','A','B','B','CA','CA','GE','GE','SA','SA','ST','ST','STR','STR','WA','WA')
Location <-c('CO','UF','CO','UF','CO','UF','CO','UF','CO','UF','CO','UF','CO','UF','CO','UF','CO','UF','CO','UF','CO','UF','CO','UF','CO','UF','CO','UF','CO','UF','CO','UF')
Value <-c(3.5,6.6,1.9,9.2,5.2,4.4,6.8,3.2,2.7,7.1,4.3,9.8,4,8.3,5,6.1,3,8.4,4.8,9.1,1.4,4,8.9,3.6,4,8.4,6.1,2.5,4.5,9.3,6.7,4.6)

data <- data.frame(Name, Gruppe, Group, Location, Value)

我想用根据“组”分隔的 ggplot 绘制它。 我的代码:

ggplot(data, aes(x=Location, y=Value, fill=Group)) +
  geom_boxplot()+
  scale_color_brewer(palette="Paired")+
  theme_classic()+
  scale_fill_manual(values=c("chartreuse3", "yellow2",
                             "firebrick3", "cyan4","darkgoldenrod2","darkorange4","darkgreen","deeppink3","darksalmon"))

但是,我还想在“Gruppe”中添加“C”作为应该出现在图例中的附加箱线图,总结“CA”-“WA”中的“Groups”。有什么办法吗?最好在不改变数据集本身的情况下,它的原始形式非常大。这个箱线图应该在“A”和“B”旁边并且看起来相同,即宽度相同。

示例图片(看起来很抱歉): enter image description here

谢谢大家的帮助,如果有什么遗漏我会尽力解释的。

这仍然会(临时)重复数据,如果您想避免这种情况,我看到的唯一方法是预先计算基本数据集和 Gruppe == "C" 子集的箱线图美学。

geom_boxplot(
  data = . %>% union_all(., filter(., Gruppe == "C") %>% mutate(Group = "C"))
)

根据您的草图,这对您有很大帮助,这可能是一个解决方案。 数据以长格式准备,过滤掉 Group AB,因为它们在 Gruppe 中重复。 grp 组合变量“Group”和“Gruppe”的值被制成一个因子,因此它们在分面时以正确的顺序绘制。 为 GroupGruppe ids 准备了一个单独的标签数据框,以便 ids 可以位于箱线图的最大值处。 为视觉效果编辑图形的输入参数,以确保标签有 space 并且箱线图的宽度相同。

library(ggplot2)
library(dplyr)
library(tidyr)
library(stringr)

data1 <- 
  data %>% 
  pivot_longer(cols = c(Gruppe, Group), names_to = "grp", values_to = "grp_id") %>% 
  filter(grp != "Group" | !grp_id %in% c("A", "B")) %>% 
  mutate(grp = factor(grp, levels = c("Gruppe", "Group")))

lab_group <- 
  data1 %>%
  group_by(Location, grp_id) %>% 
  filter(Value == max(Value))


ggplot(data1, aes(x = grp_id, y = Value, fill = grp_id)) +
  geom_boxplot() +
  geom_text(data = lab_group, aes(label = grp_id), vjust = -0.2, hjust = -0.2 )+
  scale_x_discrete(expand = expansion(add = c(0.5, 0.7)))+
  scale_y_continuous(expand = expansion(mult = c(0.02, 0.08)))+
  scale_color_brewer(palette = "Paired")+
  scale_fill_manual(values=c("chartreuse3", "yellow2",
                             "firebrick3", "cyan4","darkgoldenrod2","darkorange4",
                             "darkgreen","deeppink3","darksalmon", "red", "green"))+
  facet_grid(Location~grp,
             space = "free_x",
            scales = "free_x")+
  theme_bw()+
  theme(legend.position = "none",
        axis.text.x = element_blank(),
        axis.ticks.x = element_blank(),
        axis.title.x = element_blank())

reprex package (v2.0.0)

于 2021-09-16 创建

@Peter 哦,哇,你做到了!太感谢了!我很乐意按原样使用您的解决方案,但在原始数据集中我有六个位置。使用小平面包裹会有点矫枉过正。我设法稍微更改了您的代码:

ggplot(data1, aes(x = Location, y = PH1, fill = grp_id)) +
geom_boxplot() +
theme_classic()+
scale_x_discrete(expand = expansion(add = c(0.5, 0.7)))+
scale_y_continuous(expand = expansion(mult = c(0.02, 0.08)))+
scale_color_brewer(palette = "Paired")+
scale_fill_manual(values=c("chartreuse3", "yellow2",
                         "firebrick3", "cyan4","darkgoldenrod2","darkorange4",
                         "darkgreen","deeppink3","darksalmon", "red", "green"))