1
votes

I have been using ggplot2 with geom_boxplot to plot multiple boxplots in one graph. The data looks something like thst below.

Month   Rainfall

1         45
1         12
1         14
2         65
2         45
2         78
3         10
3         35
3         92
.         .
.         .
.         .

So by using boxplot I want to see the boxplot for the values of Rainfall for each group (1,2,3...). The result I am getting is weird and order seems messed up. Any help?

ggplot(data=edit3)+geom_boxplot(aes(x=Month, y=Rainfall))

Note: edit3 is the dataframe with the values of Rainfall and Month.

enter image description here

dput(head(edit3[,c("Month","Rainfall")],9))
structure(list(Month = c("1", "1", "1", "1", "1", "1", "1", "1", 
"2"), Rainfall = c(NA, 135.6, 34.2, 39.4, 134.6, 234.6, 69.6, 
92.8, NA)), row.names = c(NA, -9L), class = c("tbl_df", "tbl", 
"data.frame"))
1
The Months are ordered lexicographically, most likely because that column is a character or a factor. Convert it to a numeric column.joran
Not sure what the best duplicate would be, maybe this or, possibly thisjoran
I just tried this using edit3$Month <- as.integer(edit3$Month) and the result is only one boxplot of the total values of Rainfall. Any suggestion?akis
Not without an actual reproducible example to work with. But I can tell you with 100% certainty that the ordering you're seeing is because that column was originally either a character or factor. If it was a factor, then as.integer won't necessarily convert it the way you want. Se my second link.joran
Ah, there you go, you can do with integer x axis, you just also have to specify group = Month.joran

1 Answers

1
votes

Because your months are like factors, you just need to reorder the factors. Here I used the forcats package for this.

library(dplyr)
library(forcats)

edit31_1 <- edit3 %>% 
  dplyr::mutate(Month = forcats::fct_inorder(Month))

ggplot2::ggplot(edit31_1) +
  geom_boxplot(aes(x = Month, y = Rainfall))

enter image description here


  • Fictitious data used:
library(ggplot2)

set.seed(1)
edit3 <- data.frame(Month = as.factor(rep(paste(seq(1, 12, 1)), 3)),
                    Rainfall = rnorm(n = 36, mean = 60, sd = 30))

ggplot2::ggplot(edit3) +
  geom_boxplot(aes(x = Month, y = Rainfall))

enter image description here