about boxplots

28 views
Skip to first unread message

lily li

unread,
Jul 6, 2017, 6:37:55 PM7/6/17
to ggp...@googlegroups.com
Hi ggplot users,
I have a question about the boxplot. For example, one dataframe DF1 is like the following. There are just three columns: A, B, and C. I want to draw each column as one boxplot in a figure, so there will be three boxplots in one figure. The x-axis lable is 'A', 'B', and 'C'. 
If I have another dataframe DF2 that has the same structure. How to do the same for DF2, and then plot the panel for DF2 below panel DF1, so there will be just one x-axis? Thanks for your help.

DF1

        A  B  C
1     65 21 54
2     66 23 55
3     54 24 56
4     44 23 53
5     67 22 52
6     66 21 50
7     45 20 51
8     56 19 57
9     40 25 58
10   39 24 53



Roman Luštrik

unread,
Jul 6, 2017, 7:36:33 PM7/6/17
to lily li, ggp...@googlegroups.com
This is fairly easy to do, many solutions available online. Try searching something along the lines of "how to plot multiple graphs ggplot2 r".

Here's a working solution for two data.frames but can be extended to more.

library(tidyr)
library(ggplot2)

df1 <- data.frame(a = rnorm(30), b = rnorm(30, mean = 10), c = rnorm(30, mean = -5))
df2 <- data.frame(a = rnorm(30, mean = 5), b = rnorm(30, mean = 1), c = rnorm(30, mean = 2))

df1$origin <- "df1"
df2$origin <- "df2"

mdf <- do.call(rbind, list(df1, df2))

xx <- gather(mdf, key = vars, value = vals, -origin)

ggplot(xx, aes(x = vars, y = vals)) +
  theme_bw() +
  geom_boxplot() +
  facet_wrap(~origin, ncol = 1)


Cheers,
Roman

--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+unsubscribe@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
In God we trust, all others bring data.
Reply all
Reply to author
Forward
0 new messages