如何使用R中的网格根据条形图中的组变量更改条形的顺序?

时间:2015-07-23 05:30:01

标签: r lattice

我正在使用R中的格子制作条形图,其中我有不同状态的灌溉来源的4年不同的数据。使用我的代码,条形图很好但我希望首先绘制对应于1996年的条形图,然后绘制对应于2001年等的条形图,以便显示通过管井灌溉的增加区域。但是,我无法更改订单。这是我的数据和R代码。非常感谢你的帮助。

# sample data
irr_atlas <- structure(list(state = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L), .Label = c("ANDHRA PRADESH", 
"KARNATAKA", "MADHYA PRADESH", "RAJASTHAN"), class = "factor"), 
st_code = c(28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 
28L, 28L, 28L, 28L, 28L, 28L, 28L, 29L, 29L, 29L, 29L, 29L, 
29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 23L, 
23L, 23L, 23L, 23L, 23L, 23L, 23L, 23L, 23L, 23L, 23L, 23L, 
23L, 23L, 23L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 
8L, 8L, 8L, 8L, 8L), year = c(1996L, 1996L, 1996L, 1996L, 
2001L, 2001L, 2001L, 2001L, 2006L, 2006L, 2006L, 2006L, 2011L, 
2011L, 2011L, 2011L, 1996L, 1996L, 1996L, 1996L, 2001L, 2001L, 
2001L, 2001L, 2006L, 2006L, 2006L, 2006L, 2011L, 2011L, 2011L, 
2011L, 1996L, 1996L, 1996L, 1996L, 2001L, 2001L, 2001L, 2001L, 
2006L, 2006L, 2006L, 2006L, 2011L, 2011L, 2011L, 2011L, 1996L, 
1996L, 1996L, 1996L, 2001L, 2001L, 2001L, 2001L, 2006L, 2006L, 
2006L, 2006L, 2011L, 2011L, 2011L, 2011L), irr_area = c(1.84066, 
0.942819, 0.82886, 0.853502, 1.54922, 0.825659, 0.542492, 
1.53412, 1.72969, 0.70271, 0.637221, 1.53894, 1.99893, 0.678425, 
0.819829, 1.70708, 0.921594, 0.231669, 0.316999, 0.358529, 
0.91339, 0.207157, 0.426549, 0.481061, 0.921255, 0.18192, 
0.426145, 0.547193, 0.930802, 0.148065, 0.377149, 1.51843, 
1.59425, 0.112145, 2.67683, 0.540054, 1.48056, 0.030502, 
1.63696, 0.563948, 1.12595, 0.058667, 2.46494, 1.15004, 1.10444, 
0.157069, 2.64378, 2.14177, 1.55814, 0.106623, 2.71347, 0.644683, 
1.35746, 0.030586, 2.41845, 0.935234, 1.76933, 0.054374, 
2.46197, 1.76918, 1.62587, 0.050299, 2.14737, 2.82708),irr_source =             structure(c(1L,2L, 4L, 3L, 1L, 2L, 4L, 3L, 1L, 2L, 4L, 3L, 1L, 2L, 4L, 3L, 
1L, 2L, 4L, 3L, 1L, 2L, 4L, 3L, 1L, 2L, 4L, 3L, 1L, 2L, 4L, 
3L, 1L, 2L, 4L, 3L, 1L, 2L, 4L, 3L, 1L, 2L, 4L, 3L, 1L, 2L, 
4L, 3L, 1L, 2L, 4L, 3L, 1L, 2L, 4L, 3L, 1L, 2L, 4L, 3L, 1L, 
2L, 4L, 3L), .Label = c("Canal", "Tank", "Tube", "Well"), class =    "factor")), .Names = c("state","st_code", "year", "irr_area", "irr_source"),   class = "data.frame", row.names = c(NA, -64L))    

情节代码......

library(lattice)
barchart(~irr_area | factor(state) + factor(irr_source),
    group=year, data=irr_atlas, auto.key=list(space="right"))

1 个答案:

答案 0 :(得分:1)

如上所述,R图形中的组的排序通常由因子变量的排序决定。因此,您可以使用factor及其levels参数重新排序您的因素。

library(lattice)
barchart(~irr_area | factor(state) + factor(irr_source),
         group=factor(year, levels=sort(unique(year), decreasing=T)),  # change the order of years
         data=irr_atlas, auto.key=list(space="right"))

您可以通过更改decreasing=F

将其切换回来