Home > OS >  How to plot multiple boxplots with a single variable each on ggplot2?
How to plot multiple boxplots with a single variable each on ggplot2?

Time:09-17

I have a dataset looks like this:

01/02/2013 02/02/2013 03/02/2013 04/02/2013
    1          2          3          3   
    2          1          6          7 
    3          3          4          2
    4          1          1          8

I want to make a graph with n boxplots according to the number of the columns in my dataset, where each boxplot only contains one variable which is its corresponding column. So in this case, there would be 4 boxplots.

I used boxplot() function and it worked for my data, however I want to use geom_jitter() from ggplot2 to beautify my plots. And ggplot2 requires both x and y axes where I don't really have with my dataset.

This is what I want for my plot:

enter image description here

CodePudding user response:

  1. Bring your data in long format with pivot_longer from tidyr package (is in tidyverse)

  2. use ggplot from ggplot2 package (is also in tidyverse)

  3. geom_boxplot and geom_jitter if needed.

library(tidyverse)
df %>% 
    mutate(id = row_number()) %>% 
    pivot_longer(
        cols = starts_with("X"),
        names_to = "names",
        values_to = "values"
    ) %>% 
    ggplot(aes(x=names, y=values, fill=names)) 
    geom_boxplot()  
    geom_jitter(aes(y=values))

enter image description here

  • Related