首页 文章

ggplot - 多个箱图

提问于
浏览
4

我正在尝试使用因子混合创建这个数据集的箱形图(3个箱图组合):

daf <- read.table("http://pastebin.com/raw.php?i=xxYjmdgD", header=T, sep="\t")

这是样本的样子:

ia mix   Rs
1                                    Fluazinam   1 0.62
2                                    Fluazinam   1 0.76
3                                    Fluazinam   1 0.76
4                                    Fluazinam   1 0.52
5                                    Fluazinam   1 0.56
6                                    Fluazinam   1 0.20
7                                    Fluazinam   1 0.98
235 Carbendazim+Cresoxim-Metílico+Tebuconazole   3 0.65
236 Carbendazim+Cresoxim-Metílico+Tebuconazole   3 0.28
237 Carbendazim+Cresoxim-Metílico+Tebuconazole   3 0.41

这些是我失败的尝试!

library(ggplot2) 

qplot( Rs, ia, data=daf) + 
  facet_grid(mix ~ ., scales = "free", space = "free", labeller = label_both)

enter image description here

»当我添加 qplot( Rs, ia, data=daf, geom="boxplot") 它只是显示一条线,而不是框 .

ggplot(data=daf, aes(x=ia, y=Rs))+
  geom_boxplot(outlier.colour = "black", outlier.size = 2)  +  
  coord_flip() +  theme_bw() +
  scale_y_continuous(breaks=seq(0,1,by=0.25))+
  stat_summary(fun.y = mean, geom="point", shape = 4, size = 3, colour = "blue") +
  facet_grid(mix ~. , scales = "free", space="free", labeller = label_both)

enter image description here

»它将每个“ia”级别重复到每个“混合”级别

ggplot(data=daf, aes(x=ia, y=Rs))+
  geom_boxplot(outlier.colour = "black", outlier.size = 2)  +  
  layer(data = a, mapping = aes(x = ia, y= 0, label=a$Rs.median),
        geom = "text", color="NavyBlue", size=3.5) +
  coord_flip() +  theme_bw() +
  scale_y_continuous(breaks=seq(0,1,by=0.25))+
  stat_summary(fun.y = mean, geom="point", shape = 4, size = 3, colour = "blue")

enter image description here

最后,我想要三个图的组合:

从第一个图,facet.grid(不重复“ia”变量),从第二个,框,从第三个图中左边内边距的中值,如果可能的话,进入每个级别因子“混合”,用中值重新排序“ia”...

有人可以帮我吗?

提前致谢!

2 回答

  • 3

    geom_boxplot 假设分类变量位于x轴上 . coord_flipfacet_grid geom_boxplot 无法合作 . 一种解决方法是旋转文本 . 您可以在另一个程序中导出和旋转图像(或弄清楚如何拉出grid对象并旋转它) .

    a = ddply(daf, .(ia,mix), function(x) c(Rs=median(x$Rs, na.rm=TRUE)))
    
    ggplot( data=daf, aes(x=ia, y=Rs) ) + 
      geom_boxplot() +
      facet_wrap(~mix, scales="free_x") +
      stat_summary(fun.y = mean, geom="point", shape = 4, size = 3, colour = "blue") +
      theme(axis.text.x=element_text(angle = 90, hjust = 1, vjust=0.5)) +
      theme(axis.title.x=element_text(angle = 90, vjust=0.5)) +
      theme(axis.text.y=element_text(angle = 90, hjust=0.5)) +
      theme(strip.text=element_text(angle = 90, hjust=0.5)) +
      geom_text(data = a, mapping = aes(x = ia, y= 0.02, label=round(Rs,2)),
            color="NavyBlue", size=3.5, angle=90, hjust=1) +
      ylim(-0.03,1)
    

    enter image description here

  • 1

    我找到https://github.com/lionel-/ggstance并且认为我会做出另一个答案 .

    library(devtools)
    devtools::install_github("lionel-/ggstance")
    
    library(ggplot2)
    library(ggstance)
    daf <- read.table("http://pastebin.com/raw.php?i=xxYjmdgD", header=T, sep="\t")
    
    library(dplyr)
    a = daf %>% 
      group_by(ia, mix) %>%
      summarize(Rs=mean(Rs))
    
    ggplot(daf, aes(x=Rs, y=ia)) +
      geom_boxploth() +
      geom_point(data=a, shape = 4, size = 3, colour = "blue") +
      geom_text(data = a, mapping = aes(y = ia, x=0, label=round(Rs,2)),
                color="NavyBlue", size=3.5, hjust=0) +
      facet_grid(mix~., scales="free_y")
    

    enter image description here

相关问题