Reputation: 448
I have this type of dataset:
> head(data)
January February March April May June
V1 137.9173 113.57923 104.62843 68.68833 1.933005 26.5402298
V2 158.3457 103.53317 141.21841 61.78238 14.270943 9.0918608
V3 141.4854 99.30824 83.77083 13.95556 9.854910 29.6569729
V4 255.2354 149.29211 89.61694 30.99911 39.676975 17.5725574
V5 118.9752 180.70465 126.28256 58.79493 18.042225 17.6935253
V6 163.9151 117.41238 117.55447 21.79455 8.409595 0.5114895
July August September October November December
V1 1.504106 12.076564 12.24011 73.17817 88.58139 60.99152
V2 11.249913 5.185025 58.95524 96.62216 18.67379 84.01288
V3 4.398000 35.410599 11.00119 77.53560 39.07005 145.29512
V4 24.361744 44.198326 26.29123 48.34600 29.77278 97.02626
V5 30.001137 16.488718 26.84374 84.10559 71.60467 78.03426
V6 4.034544 20.972374 38.19715 35.83235 28.04593 151.85103
I've always seen data frames for ggplot out of 2 columns: the values and the labels like this:
> head(data2)
value month
1 -1.1138797 January
2 7.1686868 January
3 16.8264991 January
4 0.2936878 January
5 25.5293354 January
6 12.0044658 January
So, without reordering my data frame is there a way to build a boxplot the same way the boxplot()
function does?
Thank you all.
Upvotes: 0
Views: 116
Reputation: 613
I think ggplot cannot produce the boxplot you desired without transforming your data first. The months have to be in a single column. The transformation is simple:
require(reshape2)
data2 <- melt(data,variable.name = 'month', value.name = 'value')
The months will be automatically factorized and the order will not be changed (same as the column order). Now you can plot with ggplot easily.
Upvotes: 2
Reputation: 470
The problem is that ggplot2 automatically orders the strings alphabetically, and uses such order to construct its graphics. The solution is to convert the column in question to an ordered factor:
data2$month <- factor(data2$month, levels=c("January", "February",..))
and then, produce the plot
Upvotes: 2