繁体   English   中英

调整分组箱线图中的图例和颜色 ggplot2

[英]Adjust legend and colours in grouped boxplots ggplot2

我正在尝试使用ggplot制作grouped boxplots箱线图。 我的代码生成了分组箱线图。 但是,我想在箱线图上改进很多地方:

在此处输入图像描述

1) 删除由scale_fill_manual引入的第二个图例

2) 在我的代码中保留 colors,将箱线图更改为如下设计:

在此处输入图像描述

用我的代码生成的箱线图确实具有上述透明度,但胡须穿过箱子(看起来不太好)。 median应该像上面的示例一样可见,并且胡须不应穿过盒子。

到目前为止,这是我尝试过的。 双图例是一个问题,穿过箱线图的胡须是另一个问题。

示例数据和代码:

    library(ggplot2)
    ## create some data.frames: this results in a list of four dfs
    createDF <- quote(data.frame(id=sample(c("NN", "SS","H"), 100, rep=T),
                                 heavy=runif(100),
                                 heavier=runif(100),
                                 heaviest=runif(100)))
    dfs <- lapply(1:4, function(i) eval(createDF))
    
    ## join and shape them
    library(reshape2)
    dat <- do.call(rbind, dfs)
    dat$dfid <- paste("df", rep(1:4, times=sapply(dfs, nrow)))
    dat <- melt(dat, id.vars=c("id", "dfid"))
    
    #-----plot
    
    ggplot(dat, aes(x = factor(id, level = c('H', 'NN', 'NS')), y=value, group=interaction(variable, id), fill=variable, colour=variable)) +
      stat_boxplot(geom ='errorbar', width = 0.6,lwd = 0.9,position = position_dodge2(preserve = "single"))+
      geom_boxplot(aes(fill=variable, colour=variable),width = 0.6,lwd = 0.9,outlier.shape = NA,outlier.colour = NULL, coef = 0, 
                   position = position_dodge2(preserve = "single"),alpha = 0.6,fatten = 2) +
      coord_cartesian(ylim = c(0, 1))+
      scale_y_continuous(limits = c(0, 1), breaks = seq(0, 1, by = .25),expand = c(0, 0)) + 
      facet_grid(.~dfid)+ #reorder facets
      scale_fill_manual(values=c("#4daf4a","#984ea3","#ffff99","#ff7f00","#e41a1c"),name="")+ #  scale_fill_manual(values = c('1' = "black", '2' = "white", '3' = "darkred", '4' = "lightblue"))
      scale_color_manual(name = "scenario", values = c("#4daf4a","#984ea3","#ffff99","#ff7f00","#e41a1c"))+
      labs(y = "Values", x = "")+
      theme_bw()+
      theme(strip.text.x = element_text(size = 14, colour = "black", angle = 0, face = "bold"),
            axis.text = element_text(size = 12, color = "black"),
            text = element_text(size = 18),
            axis.text.x = element_text(angle = 0, hjust = 0.5, size = 9, colour = "black", face = "bold"),
            legend.direction = "vertical",
            legend.position = "right",
            panel.background = element_rect(fill = "grey70"),
            panel.border = element_rect(colour = "black", fill=NA, size=1))+
      theme(legend.direction ="vertical",legend.position = c(0.5, .79))+
      guides(fill=guide_legend(nrow=1))+ theme(legend.title = element_blank())+theme(legend.text = element_text(colour="black", size = 10, face = "bold"))+
      theme(legend.margin=margin(-10, 0, 0, 0))+
      theme(panel.grid.major = element_line(colour="white", size = 0.2),
            panel.grid.minor = element_blank())
    #theme(plot.margin = unit(c(0,0.0,0,0.2), "lines"))

这个设计怎么样:虽然第一个问题很简单:要删除第二个图例,只需在scale_fill_manualscale_color_manual中添加guide = "none"

第二个问题是通过使用position_dodge(1)来实现的 position 箱线图和 colors 设计可能是一种解决方法:

ggplot(dat, aes(x = factor(id, level = c('H', 'NN', 'NS')), y=value, group=interaction(variable, id), fill=variable, colour=variable)) +
  stat_boxplot(geom ='errorbar',lwd = 0.9,position = position_dodge(1))+
  geom_boxplot(aes(fill=variable), colour="grey80",width = 0.6,lwd = 0.9,outlier.shape = NA,outlier.colour = NULL, coef = 0, 
               position = position_dodge(1)) +
  coord_cartesian(ylim = c(0, 1))+
  scale_y_continuous(limits = c(0, 1), breaks = seq(0, 1, by = .25),expand = c(0, 0)) + 
  facet_grid(.~dfid)+ #reorder facets
  scale_fill_manual(values=c("#4daf4a","#984ea3","#ffff99","#ff7f00","#e41a1c"),name="", guide = "none")+ #  scale_fill_manual(values = c('1' = "black", '2' = "white", '3' = "darkred", '4' = "lightblue"))
  scale_color_manual(name = "scenario", values = c("#4daf4a","#984ea3","#ffff99","#ff7f00","#e41a1c"), guide = "none")+
  labs(y = "Values", x = "")+
  theme_bw()+
  theme(strip.text.x = element_text(size = 14, colour = "black", angle = 0, face = "bold"),
        axis.text = element_text(size = 12, color = "black"),
        text = element_text(size = 18),
        axis.text.x = element_text(angle = 0, hjust = 0.5, size = 9, colour = "black", face = "bold"),
        legend.direction = "vertical",
        legend.position = "right",
        panel.background = element_rect(fill = "grey70"),
        panel.border = element_rect(colour = "black", fill=NA, size=1))+
  theme(legend.direction ="vertical",legend.position = c(0.5, .79))+
  guides(fill=guide_legend(nrow=1))+ theme(legend.title = element_blank())+theme(legend.text = element_text(colour="black", size = 10, face = "bold"))+
  theme(legend.margin=margin(-10, 0, 0, 0))+
  theme(panel.grid.major = element_line(colour="white", size = 0.2),
        panel.grid.minor = element_blank())

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM