更改 ggplot2 中多面箱线图的图例,使其内部具有相似名称的组

这个问题建立在 enter link description here 的基础上,但在分面箱线图的背景下。

所以,我有以下代码:

set.seed(20210714)
dd <- data.frame(Method = rep(c("A","B","C"),each = 60),Pattern = rep(c("X","Y","Z"),times = 30),X1 = runif(180),Complexity = rep(c("High","Low"),times = 90),nsim = rep(rep(1:10,times = 9),each = 2),n = 10)
dd1 <-  data.frame(Method = rep(c("A",n = 5)
dd <- rbind(dd,dd1)


library(ggplot2)

# create dummy dataframe.
dummy.df <- dd
dummy.df[nrow(dd) + 1:2,"Pattern"] <- unique(dd$Pattern)[-3]
dummy.df[nrow(dd) + 1:2,"Method"] <- "ZZZ"
dummy.df[nrow(dd) + 1:2,"Complexity"] <- c("High","Low")

dummy.df$dummy <- interaction(dummy.df$Method,dummy.df$Pattern)             

ggplot(dummy.df,aes(x = dummy,y = X1,fill = Method)) +
      geom_boxplot(aes(fill = Method)) + 
  facet_grid(~Complexity) + 
    theme_light()  +
    theme(legend.position = 'bottom')  +
    guides(fill = guide_legend(nrow=1)) +
      geom_line(aes(x = dummy,group=interaction(Pattern,nsim)),size = 0.35,alpha = 0.35,colour = I("#525252"))  + 
    geom_point(aes(x = dummy,alpha = 0.25,colour = I("#525252"))  +
   scale_x_discrete(labels = c("","X","","Z","")) +
    xlab("Pattern") +
    scale_fill_brewer(breaks=c("A",type="qual",palette="Paired")

dummy.df <- dd
dummy.df[nrow(dd) + 1:2,dummy.df$Pattern)             
dummy.df$fill <- interaction(dummy.df$Method,dummy.df$n)
dummy.df$dummy <- interaction(dummy.df$fill,dummy.df$Pattern)
dummy.df$dummy <- factor(dummy.df$dummy,levels = levels(dummy.df$dummy)[-c(4,12,20,24)])

dummy.df$dummy[361:362] <- "A.10.Z" ## dummy variables to get rid of NAs


theme_set(theme_bw(base_size = 14))

ggplot(dummy.df,fill = fill)) +
    geom_boxplot(aes(fill = fill),lwd=0.1,outlier.size = 0.01)  +
    facet_grid(~Complexity) +
    theme(legend.position = 'bottom')  +
    guides(fill = guide_legend(nrow=1)) +
    geom_line(aes(x = dummy,nsim,n)),colour = I("#525252"))  +
    scale_x_discrete(labels = c("X",breaks = paste("A.10.",c("X",sep = ""),drop=FALSE) +
    xlab("Pattern") +
    scale_fill_brewer(breaks= levels(dummy.df$fill)[-c(4,8)],palette="Paired")

这会产生以下图。

更改 ggplot2 中多面箱线图的图例,使其内部具有相似名称的组

一切都很好,除了传说。我想要以下内容:深色位于左侧标题为“n = 5”的第一组中,“A”,“B”,“C”为三种深色,浅色为右侧,在右侧标题为“n=10”的第二组中,“A”、“B”、“C”代表三种浅色。有点像上面的链接 enter link description here

我不知道如何调用箱线图两次来模拟那里的解决方案。

有没有办法做到这一点?如果问题不清楚,请随时告诉我。

再次感谢您的帮助!

lzhyq 回答:更改 ggplot2 中多面箱线图的图例,使其内部具有相似名称的组

根据您之前的问题调整我的回答,可以这样实现:

library(ggplot2)

fill <- levels(dummy.df$fill)[-c(4,8)]
fill <- sort(fill)
labels <- gsub("\\.\\d+","",fill)
labels <- setNames(labels,fill)
colors <- scales::brewer_pal(type="qual",palette="Paired")(6)
colors <- setNames(colors,fill)

library(ggnewscale)

ggplot(dummy.df,aes(x = dummy,y = X1,fill = fill)) +
  geom_boxplot(aes(fill = fill),lwd=0.1,outlier.size = 0.01)  +
  scale_fill_manual(name = "n = 5",breaks= fill[grepl("5$",fill)],labels = labels[grepl("5$",values = colors,guide = guide_legend(title.position = "left",order = 1)) +
  new_scale_fill() +
  geom_boxplot(aes(fill = fill),outlier.size = 0.01)  +
  scale_fill_manual(name = "n = 10",breaks = fill[grepl("10$",labels = labels[grepl("10$",order = 2)) +
  facet_grid(~Complexity) +
  theme(legend.position = 'bottom')  +
  guides(fill = guide_legend(nrow=1)) +
  geom_line(aes(x = dummy,group=interaction(Pattern,nsim,n)),size = 0.35,alpha = 0.35,colour = I("#525252"))  + 
  geom_point(aes(x = dummy,alpha = 0.25,colour = I("#525252"))  +
  scale_x_discrete(labels = c("X","Y","Z"),breaks = paste("A.10.",c("X",sep = ""),drop=FALSE) +
  xlab("Pattern")
#> Warning: Removed 2 rows containing non-finite values (new_stat_boxplot).

本文链接:https://www.f2er.com/1668.html

大家都在问