简体   繁体   中英

Multiple boxplots in ggplot2

I have three vectors for each I would like to make side-to-side boxplots in ggplot2. Each vector contains observations from three separate samples so ideally I would like to identify each boxplot. I know of course how to accomplish that with the simple boxplot command but in ggplot2, it seems to be more complicated, at least for a newbie such as myself.

Could you please tell me whether there is a painless way to proceed here?

Thank you.

library(ggplot2)
library(reshape2)

# re-create your samples via runif (though I should have set.seed first)

obs_1 <- runif(100)
obs_2 <- runif(100)
obs_3 <- runif(100)

# you need a data frame, but you can do it on the fly
# this makes 3 columns from each of your samples
# then uses melt to do wide to long (which is what geom_boxplot needs

gg <- ggplot(melt(data.frame(obs_1, obs_2, obs_3)), aes(x=variable, y=value))
gg <- gg + geom_boxplot()
gg

在此处输入图片说明

You should really make a proper data frame, do the melt and rename column as needed. This was just to show a quick example.

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