改变Latitice中绘制水平的顺序

时间:2011-07-18 11:39:52

标签: r plot lattice

我正在尝试获得一个具有正在绘制的水平的特定顺序的箱线图。 使用以下数据和代码我生成箱图,但我需要它的顺序是6,12,15,18。 我已尝试使用with()函数进行了许多操作,但无法使其正常工作。

enter image description here

library(lattice)
rate<-structure(list(Temp = c(6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 12L, 
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 
15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 
15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 
15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 
15L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 
18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 
18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 
18L, 18L), Rep = c(1L, 1L, 1L, 1L, 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, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L,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 2L, 6L, 6L, 6L, 6L, 5L, 5L, 5L, 5L, 4L, 4L, 
4L, 4L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L), .Label = c("B", "MBCT", 
"MBT", "MSCT", "MST", "S"), class = "factor"), Week = c(1L, 2L, 
6L, 8L, 1L, 2L, 6L, 8L, 1L, 2L, 6L, 8L, 1L, 2L, 6L, 8L, 1L, 2L, 
6L, 8L, 1L, 2L, 6L, 8L, 1L, 2L, 6L, 8L, 1L, 2L, 6L, 8L, 1L, 2L, 
6L, 8L, 1L, 2L, 6L, 8L, 1L, 2L, 6L, 8L, 1L, 2L, 6L, 8L, 1L, 2L, 
6L, 8L, 1L, 2L, 6L, 8L, 1L, 2L, 6L, 8L, 1L, 2L, 6L, 8L, 1L, 2L, 
6L, 8L, 1L, 2L, 6L, 8L, 1L, 2L, 6L, 8L, 1L, 2L, 6L, 8L, 1L, 2L, 
6L, 8L, 1L, 2L, 6L, 8L, 1L, 2L, 6L, 8L, 1L, 2L, 6L, 8L, 1L, 2L, 
6L, 8L, 1L, 2L, 6L, 8L, 1L, 2L, 6L, 8L, 1L, 2L, 6L, 8L, 1L, 2L, 
6L, 8L, 1L, 2L, 6L, 8L, 1L, 2L, 6L, 8L, 1L, 2L, 6L, 8L, 1L, 2L, 
6L, 8L, 1L, 2L, 6L, 8L, 1L, 2L, 6L, 8L, 1L, 2L, 6L, 8L, 1L, 2L, 
6L, 8L, 1L, 2L, 6L, 8L, 1L, 2L, 6L, 8L, 1L, 2L, 6L, 8L), Weight = c(1.756, 
1.756, 1.756, 1.756, 0.92, 0.92, 0.92, 0.92, 1.201, 1.201, 1.201, 
1.201, 2.601, 2.601, 2.601, 2.601, 2.057, 2.057, 2.057, 2.057, 
0.784, 0.784, 0.784, 0.784, 0.663, 0.663, 0.663, 0.663, 1.272, 
1.272, 1.272, 1.272, 3.389, 3.389, 3.389, 3.389, 1.433, 1.433, 
1.433, 1.433, 3.822, 3.822, 3.822, 3.822, 1.55, 1.55, 1.55, 1.55, 
1.198, 1.198, 1.198, 1.198, 1.029, 1.029, 1.029, 1.029, 1.113, 
1.113, 1.113, 1.113, 0.261, 0.261, 0.261, 0.261, 0.639, 0.639, 
0.639, 0.639, 0.749, 0.749, 0.749, 0.749, 1.083, 1.083, 1.083, 
1.083, 1.429, 1.429, 1.429, 1.429, 3.083, 3.083, 3.083, 3.083, 
1.061, 1.061, 1.061, 1.061, 1.154, 1.154, 1.154, 1.154, 1.691, 
1.691, 1.691, 1.691, 1.185, 1.185, 1.185, 1.185, 0.552, 0.552, 
0.552, 0.552, 1.507, 1.507, 1.507, 1.507, 1.175, 1.175, 1.175, 
1.175, 1.773, 1.773, 1.773, 1.773, 1.712, 1.712, 1.712, 1.712, 
3.784, 3.784, 3.784, 3.784, 0.715, 0.715, 0.715, 0.715, 1.271, 
1.271, 1.271, 1.271, 0.788, 0.788, 0.788, 0.788, 1.72, 1.72, 
1.72, 1.72, 0.571, 0.571, 0.571, 0.571, 1, 1, 1, 1, 1.037, 1.037, 
1.037, 1.037, 1.656, 1.656, 1.656, 1.656, 2.083, 2.083, 2.083, 
2.083), Rate = c(0.387, 0.116, -0.141, 0.184, 0.785, 0.151, -0.69, 
0.16, 0.477, 0.368, -0.544, 0.49, 0.152, 0.183, -0.137, 0.259, 
0.239, 0.292, 0.018, 0.411, 0.322, 0.073, -0.148, 0.287, 0.214, 
0.21, -0.579, 0.419, 0.23, 0.271, 0.685, 0.426, 0.248, 0.125, 
0.053, 0.176, 0.465, 0.107, 0.02, 0.339, 0.261, 0.327, 0.279, 
0.424, 0.308, 0.223, 0.287, 0.383, 0.306, 0.24, 0.258, 0.253, 
0.437, 0.315, 0.275, 0.481, 0.372, 0.306, 0.267, 0.449, 0.727, 
0.441, 0.624, 1.262, 0.334, 0.447, 0.548, 0.654, 0.846, 0.661, 
0.66, 0.734, 0.191, 0.316, 0.551, 0.581, 0.332, 0.403, 0.509, 
0.603, 0.411, 0.683, 0.427, 0.516, 0.498, 0.674, 0.371, 0.326, 
0.288, 0.435, 0.297, 0.435, 0.165, 0.387, 0.212, 0.345, 0.334, 
0.664, 0.526, 0.338, 0.094, 0.066, 0.39, 0.525, 0.215, 0.431, 
0.151, 0.361, 0.153, 0.297, 0.127, 0.339, 0.292, 0.434, 0.411, 
0.442, 0.25, 0.607, 0.369, 0.567, 0.189, 0.39, 0.372, 0.333, 
0.339, 0.327, 0.449, 0.224, 0.086, 0.242, 0.465, 0.374, -0.063, 
-0.006, 0.364, 0.308, 0.069, 0.223, 0.397, 0.264, 0.478, 0.345, 
0.582, 0.36, 0.426, 0.403, 0.583, 0.544, 0.57, 0.567, 0.388, 
0.531, 0.111, 0.125, 0.366, 0.266, 0.26, 0.315, 0.387, 0.549)), .Names = c("Temp", 
"Rep", "Ind", "Week", "Weight", "Rate"), class = "data.frame", row.names = c(NA, 
-160L))
rate$Temp <- as.character(rate$Temp)
rate$Week <- as.character(rate$Week)
rate$Rep <- as.character(rate$Rep)
rate$Weight<- as.character(rate$Weight)
bwplot(Rate~Temp, rate,
       main="Boxplot for data over all weeks by temperature"
       )

1 个答案:

答案 0 :(得分:4)

这可以用与your question from a month ago相同的方式解决。您需要设置因子的级别顺序。我通常建议你使用因子,除非你有充分的理由使用字符。

rate$Temp <- as.factor(rate$Temp)
levels(rate$Temp) <- c("6", "12", "15", "18")