如何计算因素,并根据qqplot2的连续变量排列其中一个因素 [英] how to boxplot factors and order one of the factors according to a continuos variable with qqplot2

查看:96
本文介绍了如何计算因素,并根据qqplot2的连续变量排列其中一个因素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用ggplot2来显示我的数据框。我的数据框中的每一行代表一个具有三种价值观的人:人的角色,人的村庄和该村庄从共同起源的距离。我想为每个村庄想象角色(比如方块图),但我也希望村庄不是按字母顺序绘制,而是根据距离(从最近到最远)绘制。我现在所拥有的是:

pre code> qplot(h [,2],h [,1],xlab =village ,ylab =role)

我不知道如何告诉qplot()以及如何根据距离来定购村庄。我也想显示一个顶部x轴的距离。

我的数据框是这样的:

 <$ 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, ,
2L,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,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, 3L,3L,6L),.Label = c(1,2,3,4,5,
6),class =factor结构(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,12L,10L,10L,10L,10L,10L,10L,10L,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,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,6 L,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)。标签= c(a,
b,c, d,e,f,g,
h,i,l,m,n,o,
p ,q,r),class =factor),距离= c 351,351,351,351,351,351,351,351,436,436,436,436,436,
436,439,439,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,434,466,466,466, 466,466,
466,492,492,492,492,492,447,448,448,448,431,431,431,
431,431,431,431,431,431,439, 449,449,449,449,471,471,471,
471,471,471,471,471,471, 471,471,473,443,444,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,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,414,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)),.Names = c(role,
village,distance),row.names = c(NA,-221L),class =data.frame)


解决方案

  library(plyr)
txt< - ddply(h,。(村庄),总结,角色=最大(as.numeric(角色)),距离= distance [1])$ ​​b $ b ggplot(h,aes(x = reorder村庄,距离,最大),y =角色))+ geom_boxplot(aes(group = village))+ geom _text(data = txt,aes(village,role,label = distance),vjust = -1)

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


I am trying to use ggplot2 to visualize my dataframe. Each row in my data frame represents a person with three values: the role of the person, the village of the person and the distance of that village from a common origin. I'd like to visualize roles (like box plots) for each villages, but I'd also like villages to be plotted not in alphabetical order, but according to distance (from the closest to the farthest). All I have right now is:

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

I do not understand how to tell qplot() to make box plots and how to order villages according to distance. I also would like to show as a top x-axis the distances. Any help more than welcome!

My data frame is like this:

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")

解决方案

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)

So basically you add text with geom_text.

这篇关于如何计算因素,并根据qqplot2的连续变量排列其中一个因素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆