简体   繁体   中英

ggplot - multiple boxplots

I'm trying to create boxplots with this dataset faceting by factor mix (3 boxplots combined):

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

This is what the sample looks like:

                                            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

These are my failed attempts!

library(ggplot2) 

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

在此输入图像描述

» When I add the qplot( Rs, ia, data=daf, geom="boxplot") It simply appear a line, not the box.

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) 

在此输入图像描述

» It repeats every "ia" level into each "mix" level

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") 

在此输入图像描述

Finally I'd like a combination of the three plots:

from the first plot, the facet.grid(without repeating "ia" variables), from the second one, the boxes, and from the third one the median values in the left inside margin, and if it could be possible, into each level of factor "mix", reordering the "ia" by median values...

Could someone help me with this??

Thanks in advance!

geom_boxplot assumes the categorical variables are on the x-axis. coord_flip doesn't work in combination with facet_grid + geom_boxplot . One workaround is to rotate the text. You can export and rotate the image in another program (or figure out how to pull out the grid object and rotate it).

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)

在此输入图像描述

I found https://github.com/lionel-/ggstance and thought I'd make an alternative answer.

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")

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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