使用ggplot的时间序列boxplot,无需重新排序我的数据集

时间:2017-03-11 00:17:02

标签: r ggplot2 boxplot

我有这种类型的数据集:

> 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

我总是看到2列中ggplot的数据框:值和标签如下:

> 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

那么,如果不重新排序我的数据框,是否有办法以boxplot()函数的方式构建一个箱形图?

谢谢大家。

2 个答案:

答案 0 :(得分:2)

问题是ggplot2会自动按字母顺序排序字符串,并使用这样的顺序构建其图形。 解决方案是将有问题的列转换为有序因子:

data2$month <- factor(data2$month, levels=c("January", "February",..))

然后,制作情节

答案 1 :(得分:2)

我认为ggplot无法在不转换数据的情况下生成所需的箱线图。这几个月必须在一个列中。 转型很简单:

require(reshape2)
data2 <- melt(data,variable.name = 'month', value.name = 'value')

月份将自动分解,订单不会更改(与列顺序相同)。现在你可以轻松地使用ggplot进行绘图。