Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boxplot with geom_boxplot for many years

Tags:

r

ggplot2

I want to make nice boxplot with ggplot2. NB: the airquality data from ggplot2 can illustrated what I want to do but in my own data I have an additional column for year(1900:2000).

I make simple boxplot with this command:

      tapply(data$Temp, substr(data$Month, 1,3),na.rm=TRUE, summary) #data=airquality
      boxplot(Temp~Month, data=data, na.action = NULL, main="1900-2000")

It have this graphic: Graph1

But I when try with ggplot2 with this command:

     ggplot(data, aes(Month, Temp),facet= Month~.) + geom_boxplot()

It get this graphic Graph2

In the same plot I want to view the corresponding Value and boxplot for each month like graphic1

like image 876
NVega Avatar asked May 30 '26 06:05

NVega


1 Answers

Because Month is a continuous variable you will need to 'factorize' this variable to have seperate boxplots:

ggplot(airquality, aes(factor(Month), Temp)) + geom_boxplot()

alternatively you can use the group aesthetic:

ggplot(airquality, aes(Month, Temp, group = Month)) + geom_boxplot()
like image 194
Johan Avatar answered Jun 01 '26 00:06

Johan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!