views:

58

answers:

1

I'm wondering if the following is possible.

I have a survey with 100+ questions, all categorical, collected in 4 locations. All questions are labeled q1, q2 and so on. Let's say there are 100 for simplicity.

I can visually compare the results of a particular question across locations with:

library (lattice);
histogram(~ q75 | location, data = survey, layout=c(1,4));

or, with ggplot2,

library (ggplot2);
qplot(q75, data=survey) + facet_grid(location ~ .);

This gives 4 histograms, aligned vertically, for one question.

I'm wondering if there is a programmatic way to generate the histograms for all 100 questions, so left most I have the stack of 4 histograms for q1, then to the right the stack of 4 histograms for q2 and so on. Granted, it's going to be a long line, but this is for visual inspection only and for spotting the initial areas to investigate further. Scrolling to the right is fine with me, I have a wide-screen monitor so I'll get a decent number of histograms fitting in at one time.

That the questions are labeled with 'q' + a counter is good. What I don't know is

  • how to make this kind of plot with lattice (or ggplot2?) it's a bi-dimensional lattice.
  • how to feed such programmatically-generated field names into these commands.

Suggestions are appreciated. I'm a programmer, but not in R where I'm a newbie.

+2  A: 

The trick is to get the data in the correct format. You want a data frame with 3 columns: the question, the location and the score. (The reshape package may assist you in manipulating your dataset.)

n_questions <- 100
n_responses <- c(North = 89, East = 37, South = 57, West = 71)
n_locations <- length(n_responses)
total_responses <- sum(n_responses)

survey <- data.frame(
  question = unlist(lapply(n_responses, function(x) rep(seq_len(n_questions), each = x))),      
  location = rep(c("North", "East", "South", "West"), times = n_questions * n_responses),
  score = sample(n_questions, n_questions * total_responses, replace = TRUE)
)

After that, drawing the histograms are easy.

lattice:

library(lattice)
library(latticeExtra)
useOuterStrips(histogram(~ score | question * location, data = survey))

ggplot2:

library(ggplot2)
ggplot(survey, aes(score)) + geom_histogram() + facet_grid(location ~ question)
Richie Cotton