# How to build advanced boxplots from datasets which columns contain only numbers?

I have got a fold-change dataset and I simply would like to plot something like this:

The problem is that I have only numbers in my columns and when I use a boxplot() function I can plot only boxes but not dots, jitter, etc.

I use this code to build a df and then proceed to a boxplot

A <- c(-1, 25, 3, 4, 50)
B <- c(23, -41, 32, 68, 26)
C <- c(63, 71, 22, -8, 2)
D <- c(16, -49, 2, 56, 6)
df <- data.frame(A, B, C, D)
print (df)
boxplot(df)


How can I plot something that looks like the above picture? Thanks

• You want ggplot2, specifically the geom_boxplot and geom_dotplot functions. Check out the cheat sheet (warning: PDF download) to see the full functionality of the package. May 4 at 21:19
• Thank you. The problem is that I don't have geom data - if you check the dataset I have proposed - there is no "y" data, I have only 4 columns (A,B,C,D) and rows with numbers. It is ok for default boxplot() while I cannot add geom, can you specify what should the ggplot2 script look like for the above-mentioned dataset? Thanks
– Mark
May 5 at 10:57

To coerce your data into a form interpretable by ggplot2, you can use the gather function from tidyr.

A <- c(-1, 25, 3, 4, 50)
B <- c(23, -41, 32, 68, 26)
C <- c(63, 71, 22, -8, 2)
D <- c(16, -49, 2, 56, 6)
df <- data.frame(A, B, C, D)

library(tidyverse)
as_tibble(df) %>%
gather %>%
ggplot +
geom_boxplot(mapping = aes(x = key, y = value)) +
geom_dotplot(mapping = aes(x = key, y = value),
binaxis = "y", stackdir = "center") +
theme_classic()