0

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")
Carl
  • 4,232
  • 2
  • 12
  • 24
user299791
  • 2,021
  • 3
  • 31
  • 57

1 Answers1

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)

So basically you add text with geom_text.

danas.zuokas
  • 4,551
  • 4
  • 29
  • 39
  • super amazing! btw, it would be great to add a x-axis on top which lists the distances, would that be possible? axis(3...) does not work with ggplot2 – user299791 Mar 05 '14 at 09:28