I was doing some box plots with three variables in ggplot and struggled for a long time to get what i wanted because the data in my x axis was numeric and not factor. After reading documentation (http://www.sthda.com/english/wiki/ggplot2-box-plot-quick-start-guide-r-software-and-data-visualization) and other questions here (Modify x-axis labels in each facet and ggplot: arranging boxplots of multiple y-variables for each group of a continuous x) i understood that i had to transform my x axis data to factors.
> str(HT_2)
Classes ‘data.table’ and 'data.frame': 540 obs. of 4 variables:
$ T : int -1 -2 -3 -4 0 1 2 3 4 -1 ...
$ Month : Factor w/ 12 levels "1","2","3","4",..: 1 1 1 1 1 1 1 1 1 1 ...
$ Gauge : chr "Aconcagua" "Aconcagua" "Aconcagua" "Aconcagua" ...
$ Norm_Flow_m3_s: num 1.49 1.77 1.99 2.02 1.17 ...
- attr(*, ".internal.selfref")=<externalptr>
The following line of code solved my problem
ggplot(HT_2, aes(x=Month, y=Norm_Flow_m3_s,fill=Gauge)) + geom_boxplot()
But the same line fails when the x axis has int data
> str(HT_2)
Classes ‘data.table’ and 'data.frame': 540 obs. of 4 variables:
$ T : int -1 -2 -3 -4 0 1 2 3 4 -1 ...
$ Month : int 1 1 1 1 1 1 1 1 1 1 ...
$ Gauge : chr "Aconcagua" "Aconcagua" "Aconcagua" "Aconcagua" ...
$ Norm_Flow_m3_s: num 1.49 1.77 1.99 2.02 1.17 ...
- attr(*, ".internal.selfref")=<externalptr>
- attr(*, "sorted")= chr "Month"
Even if i try to group my x axis data
ggplot(HT_2, aes(x=Month, y=Norm_Flow_m3_s,fill=Gauge,group=Month)) + geom_boxplot()
Does anyone know why this happens? Or how could i include numeric data in a box plot?