用户定义的重新订购箱图

时间:2014-05-25 17:22:47

标签: r ggplot2 boxplot

我正在制作这个数据的箱线图:

dput(fit3)
structure(list(Grupos = structure(c(3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L), .Label = c("TR", "TR2x45", "UT", "UT2x45"
), class = "factor"), ordenada = c(-11200.4727272736, -5999.3090909097, 
-6138.35454545502, -76381.2090909171, 0, -339.309090909113, -2376.61818181839, 
-25330.2272727299, -707.681818181909, -1611.90000000011, -3678.90909090945, 
-50373.0000000052, 0, 0, -4214.84545454581, -3423.97272727308, 
0, 0, -6027.19090909151, -9989.82727272836, -2206.87272727293, 
0, -6793.0000000008, -23997.4181818206, -24926.3363636386, -2349.28181818208, 
-820.572727272809, -33671.3636363669, -1160.70000000014, 0, -159329.727272744, 
-20348.481818184, -424.500000000049, -48302.9909090956, -29205.581818185, 
-1018.74545454556, -147929.054545469, -7812.65454545542, -18817.1363636383, 
-678.800000000052, 0, -22464.7090909112, -15843.9090909106, -5404.88181818238
), pendiente = c(18.0000000000014, 9.63636363636461, 9.86363636363712, 
122.681818181831, 0, 0.54545454545458, 3.81818181818215, 40.6818181818223, 
1.13636363636378, 2.59090909090926, 5.90909090909148, 80.9090909090992, 
0, 0, 6.77272727272785, 5.50000000000057, 0, 0, 9.68181818181914, 
16.0454545454563, 3.54545454545487, 0, 10.9090909090922, 38.5454545454584, 
40.0454545454582, 3.77272727272769, 1.31818181818195, 54.0909090909142, 
1.86363636363659, 0, 255.909090909118, 32.6818181818216, 0.68181818181826, 
77.5909090909165, 46.909090909096, 1.6363636363638, 237.636363636387, 
12.5454545454559, 30.2272727272758, 1.09090909090917, 0, 36.0909090909124, 
25.4545454545478, 8.68181818181908), r.squared = c(0.919634060520734, 
0.954630416312683, 0.845100502512542, 0.951645777648911, NaN, 
0.59999999999997, 0.963934426229504, 0.835706833594179, 0.694444444444498, 
0.91779661016945, 0.927552140504953, 0.959411828829618, NaN, 
NaN, 0.978880070546728, 0.936125319693121, NaN, NaN, 0.980315471045828, 
0.962528966476169, 0.856901408450706, NaN, 0.896358543417418, 
0.895321098632955, 0.971366890268327, 0.840121951219549, 0.793396226415111, 
0.95198720017211, 0.808173076923133, NaN, 0.980407864815474, 
0.952010975654702, 0.750000000000039, 0.933728442060346, 0.936844883094985, 
0.939130434782635, 0.993784082719898, 0.735431550492406, 0.957943419114485, 
0.847058823529391, NaN, 0.951314320205228, 0.974094551779841, 
0.957004197271801), adj.r = c(0.910704511689705, 0.949589351458537, 
0.827889447236158, 0.946273086276568, NaN, 0.555555555555522, 
0.959927140255005, 0.817452037326866, 0.660493827160554, 0.908662900188278, 
0.919502378338837, 0.954902032032908, NaN, NaN, 0.976533411718587, 
0.929028132992356, NaN, NaN, 0.978128301162031, 0.958365518306855, 
0.841001564945229, NaN, 0.884842826019353, 0.883690109592172, 
0.968185433631474, 0.822357723577276, 0.770440251572346, 0.946652444635678, 
0.786858974359037, NaN, 0.978230960906082, 0.946678861838558, 
0.722222222222266, 0.926364935622607, 0.929827647883317, 0.932367149758483, 
0.993093425244331, 0.706035056102673, 0.953270465682761, 0.830065359477101, 
NaN, 0.945904800228031, 0.971216168644268, 0.952226885857557)), .Names = c("Grupos", 
"ordenada", "pendiente", "r.squared", "adj.r"), row.names = c("UT1", 
"UT2", "UT3", "UT4", "UT5", "UT6", "UT7", "UT8", "UT9", "UT10", 
"TR1", "TR2", "TR3", "TR4", "TR5", "TR6", "TR7", "TR8", "TR9", 
"TR10", "UT1.1", "UT2.1", "UT3.1", "UT4.1", "UT5.1", "UT6.1", 
"UT7.1", "UT8.1", "UT9.1", "UT10.1", "UT11", "UT12", "TR1.1", 
"TR2.1", "TR3.1", "TR4.1", "TR5.1", "TR6.1", "TR7.1", "TR8.1", 
"TR9.1", "TR10.1", "TR11", "TR12"), class = "data.frame")

我用来做的代码就是:

mycolor<-c("red","darkorange","royalblue1","violetred2")
media<-tapply(fit3$pendiente,fit3$Grupos,mean)
boxplot(pendiente ~ Grupos,  data = fit3, main="Boxplot pendientes", col = mycolor))
points(media,pch=20,cex=3,col="black") 

我得到的结果是:

enter image description here

问题是我根据需要订购了Grupos变量,现在情节按字母顺序重新排序。我需要在x中为正确的顺序传递一个因子向量,以便了解它,但我一直在努力解决长度问题,因为unique(Grupos)不符合长度要求,factor(Grupos)按字母顺序重新排序。

我想要绘制的顺序(从左到右)是:

orden
[1] "UT"     "TR"     "UT2x45" "TR2x45"

我很欣赏使用基本图形的答案,但是如果格子或ggplot2解决这个并保持视觉效果他们很好(我使用基础图形,因为我更了解如何指定用户定义的颜色,就像我在图中所做的那样)

谢谢!

1 个答案:

答案 0 :(得分:2)

您可以按如下方式处理:

data <- split(fit3$pendiente, fit3$Grupos)
data <- data[c(3,1,4,2)]
boxplot(data, main="Boxplot pendientes", col = c('red', 'orange', 'violet', 'pink'))

在这里,我们手动拆分pendiente w.r.t. Grupos然后重新排序。

编辑:似乎绘图顺序与levels(fit3$Grupos)指示的一致; factor()函数在转换向量时自动对标签进行排序(例如,通过read.csv完成)。因此,我们可能会重新编码此变量,以便标签按所需顺序出现:

fit3$Grupos <- factor(as.character(fit3$Grupos), levels=c('UT', 'TR', 'UT2x45', 'TR2x45'))

然后以标准方式进行。