这个问题建立在 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")
一切都很好,除了传说。我想要以下内容:深色位于左侧标题为“n = 5”的第一组中,“A”,“B”,“C”为三种深色,浅色为右侧,在右侧标题为“n=10”的第二组中,“A”、“B”、“C”代表三种浅色。有点像上面的链接 enter link description here。
我不知道如何调用箱线图两次来模拟那里的解决方案。
有没有办法做到这一点?如果问题不清楚,请随时告诉我。
再次感谢您的帮助!