如何根据ggplot2的连续变量绘制因子并对其中一个因子进行排序

时间:2014-03-05 08:57:16

标签: r ggplot2

我正在尝试使用ggplot2来可视化我的数据帧。我的数据框中的每一行代表一个具有三个值的人:人的角色,人的村庄以及该村与共同起源的距离。我想想象每个村庄的角色(如箱形图),但我也希望村庄不是按字母顺序绘制,而是根据距离(从最近到最远)绘制。我现在所拥有的只有:

qplot(h[,2],h[,1], xlab="village", ylab="role")

我不明白如何告诉qplot()制作箱形图以及如何根据距离订购村庄。我还希望将距离显示为顶部x轴。任何帮助都超过欢迎!

我的数据框是这样的:

h<-structure(list(role = structure(c(4L, 4L, 4L, 4L, 6L, 6L, 4L, 
4L, 4L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 
3L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 2L, 1L, 1L, 2L, 2L, 
2L, 3L, 3L, 3L, 3L, 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, 3L, 4L, 4L, 4L, 4L, 3L, 5L, 5L, 5L, 5L, 5L, 3L, 3L, 
3L, 4L, 4L, 4L, 4L, 4L, 3L, 3L, 4L, 4L, 3L, 3L, 3L, 3L, 6L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 5L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 6L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 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, 3L, 3L, 6L), .Label = c("1", "2", "3", "4", "5", 
"6"), class = "factor"), village = structure(c(9L, 9L, 9L, 9L, 
15L, 15L, 15L, 15L, 15L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
5L, 5L, 5L, 5L, 13L, 13L, 13L, 13L, 13L, 13L, 11L, 11L, 11L, 
11L, 11L, 11L, 11L, 11L, 11L, 11L, 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, 10L, 10L, 10L, 10L, 10L, 10L, 14L, 14L, 14L, 
14L, 14L, 2L, 7L, 7L, 7L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 
16L, 4L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 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, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 3L, 3L, 3L), .Label = c("a", 
"b", "c", "d", "e", "f", "g", 
"h", "i", "l", "m", "n", "o", 
"p", "q", "r"), class = "factor"), distance = c(409, 
409, 409, 409, 365, 365, 365, 365, 365, 351, 351, 351, 351, 351, 
351, 351, 351, 351, 351, 351, 351, 351, 436, 436, 436, 436, 436, 
436, 439, 439, 439, 439, 439, 439, 439, 439, 439, 439, 434, 434, 
434, 434, 434, 434, 434, 434, 434, 434, 434, 434, 434, 434, 434, 
434, 434, 434, 434, 434, 434, 434, 434, 434, 434, 434, 434, 434, 
434, 434, 434, 434, 434, 434, 434, 434, 466, 466, 466, 466, 466, 
466, 492, 492, 492, 492, 492, 447, 448, 448, 448, 431, 431, 431, 
431, 431, 431, 431, 431, 449, 449, 449, 449, 449, 471, 471, 471, 
471, 471, 471, 471, 471, 471, 471, 471, 471, 471, 471, 471, 443, 
443, 443, 443, 443, 443, 443, 443, 443, 443, 443, 443, 443, 443, 
443, 443, 443, 443, 443, 443, 443, 443, 443, 443, 443, 443, 443, 
443, 443, 443, 443, 443, 443, 443, 443, 443, 443, 443, 443, 443, 
416, 416, 416, 416, 416, 416, 416, 416, 416, 416, 416, 416, 416, 
416, 416, 416, 416, 416, 416, 416, 416, 416, 416, 416, 416, 416, 
416, 416, 416, 416, 416, 416, 416, 416, 416, 416, 416, 416, 416, 
416, 416, 416, 416, 416, 416, 416, 416, 416, 416, 416, 416, 416, 
416, 416, 416, 416, 416, 416, 416, 416, 416, 423, 423, 423)), .Names = c("role", 
"village", "distance"), row.names = c(NA, -221L), class = "data.frame")

1 个答案:

答案 0 :(得分:1)

library(plyr)
txt <- ddply(h, .(village), summarize, role = max(as.numeric(role)), distance = distance[1])
ggplot(h, aes(x = reorder(village, distance, max), y = role)) + geom_boxplot(aes(group = village)) + geom_text(data = txt, aes(village, role, label = distance), vjust = -1)

所以基本上你用geom_text添加文字。