Solved – questionnaire stacked bar plot with R

data visualizationrsurvey

I have a file containing questionnaire data (categorical data, 4 possible values). I have imported this to R to get the corresponding data frame. Now I've done some messy things with the table function to obtain a new data frame which contains instead of the whole data listing only the frequency counts in right order of every variable, i.e. question in the questionnaire (maybe someone could tell me a good way to do this too).

What I would like to obtain a graphical visualisation of the frequencies as a stacked bar plot. That's what it should look like: for each question, for stacked bars, the size of each bar corresponding to the frequency count (the frequency data in the transformed data frame that I constructed is already ordered).
Is this possible?
Thanks in advance!

Best Answer

take a look at Hadley Wickam's ggplot2 package. With it, all the munging you did to get frequencies may have been wasted. See his examples on the page I linked. If you want to use your pre-generated frequencies, use stat='identity'.

Making up some sample data....

dat<-data.frame(sample(letters[1:4],1000,replace=T),question=rep(c('q1','q2','q3','q4'),each=250))
qplot(question,data=dat,geom='bar',fill=answer)