1
$\begingroup$

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

enter image description here

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

$\endgroup$
2
  • 1
    $\begingroup$ 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. $\endgroup$
    – acvill
    May 4 at 21:19
  • $\begingroup$ 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 $\endgroup$
    – Mark
    May 5 at 10:57

1 Answer 1

2
$\begingroup$

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

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.