0

I am having trouble finding a solution to plot multiple boxplots in one graph with boxplot in R.

I have a dataset containing the soil humidity of 48 sites measured in 5 dates. Sites are classified in four classes: A, C, M, P (12 sites of each one).

I saved my data in as a .csv file with 6 columns.

Column one contains the class of the sites: A, C, M or P.

Columns two through 6 contains the measurements (febr2017, mar2017, jun2017, sep2017 and feb2018).

I would like to plot a boxplot of the soil humidity of all these 5 dates against the site's classes.

My code so far is:

boxplot(feb2017~class, humidity, na.action=NULL, main="Soil humidity", xlab="Class", ylab="Humidity (%)", col=(c("yellowgreen")))

However, this only shows soil humidity of february against the classes. I have tried to use the parameter "add=TRUE" in the rest of the plots but it shows all the dates overlapped.

My question is: How to show the rest of the dates in one graph with some dodge position?

Any help will be much appreciated.

neilfws
  • 32,751
  • 5
  • 50
  • 63
María Paz
  • 3
  • 1
  • 2
  • 1
    It is very difficult to help you like this. Can you please provide sample data and an expected output? – Seymour Apr 09 '18 at 22:39

1 Answers1

1

This is a case where the tidyverse provides a good solution.

Some fake data:

set.seed(111)
df1 <- data.frame(class = sample(c("A", "C", "M", "P"), 100, replace = TRUE),
                  feb2017 = sample(0:100, 100, replace = TRUE), 
                  mar2017 = sample(0:100, 100, replace = TRUE), 
                  jun2017 = sample(0:100, 100, replace = TRUE), 
                  sep2017 = sample(0:100, 100, replace = TRUE), 
                  feb2018 = sample(0:100, 100, replace = TRUE))

The trick is to gather the dates into one column and values for those dates into another. You also need to set the factor levels to get the dates in the correct order.

You could indicate site class by facets or by colour. Both shown below, I think colour works better.

library(tidyverse)
df1 %>% 
  gather(Date, Humidity, -class) %>% 
  mutate(Date = factor(Date, levels = c("feb2017", "mar2017", "jun2017",
                                        "sep2017", "feb2018"))) %>% 
  ggplot(aes(Date, Humidity)) + 
    geom_boxplot() + 
    facet_grid(~class)

enter image description here

df1 %>% 
  gather(Date, Humidity, -class) %>% 
  mutate(Date = factor(Date, levels = c("feb2017", "mar2017", "jun2017",
                                        "sep2017", "feb2018"))) %>% 
  ggplot(aes(Date, Humidity)) + 
    geom_boxplot(aes(fill = class))

enter image description here

neilfws
  • 32,751
  • 5
  • 50
  • 63