boxplot group_by month

--Hi,

i have a dataframe like this example below:

Month value
January 1.808501
January 1.846592
February 1.853238
February 1.863296
February 1.874263
February 1.880885
March 1.910228
March 1.914645
March 1.923587
March 1.924006
April 1.930859
April 1.934411
April 1.934766
April 1.938333
April 1.939394
July 1.940112
July 1.941505
December1.942514
December 1.945899
December 1.946359

i want to plot boxplot grouping by month (one boxplot per month), do i need to use a pivot table ?

thank you --

Like this?

library(ggplot2)

ggplot(data, aes(Month, Value, fill = Month)) + 
  geom_boxplot()

and if order matters:

data$Month <- factor(data$Month, levels = c('January', 'February', 'March', 'April', 
                                            'May', 'June', 'July', 'August', 'September', 
                                            'October', 'November', 'December'), ordered = TRUE)

ggplot(data, aes(Month, Value, fill = Month)) + 
  geom_boxplot()

1 Like

Thanks to Nate for his high quality answer;
just a mild assist from me; base R provides a built-in variable month.name which contains the names specified in the factor levels. This can help with reducing typing.

3 Likes

This a perfect case of" Yes it work but should it ?"

You are not showing the n observations for each month and that could bring a lot of confusion.. for example you have two points for January.. boxplots inherently hide distributions and assume that each group as about the same number of points..

In this case I would add raw dara points on top of it or use violin plot

Hi all,

thank you so much for yours help,
i have use month.name with factor levels for order months.
And also use geom_jitter to represent all the point by month per boxplot.

see you.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.