0
$\begingroup$

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!

$\endgroup$
1

1 Answer 1

2
$\begingroup$

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)
$\endgroup$
2
  • $\begingroup$ Thank you! I think that line should be dat<-data.frame(answer=sample(letters[1:4],1000,replace=T),question=rep(c('q1','q2','q3','q4'),each=250)) This example is very helpful. Just another stupid question: I have a data frame where each column has the name of a question and the entries are the answers given to that question listed by users (the user id's are the first column of the data frame). How do I get this data frame in something like the thing you generated? Thank you very much! $\endgroup$
    – ftonti
    Commented Jan 23, 2012 at 21:19
  • $\begingroup$ Yeah, copied the wrong line. to answer, use ?melt dat<-data.frame(userid=1:100,q1=sample(letters[1:4],100,replace=T),q2=sample(letters[1:4],100,replace=T)) dat.melt<-melt(dat,id.var='userid') $\endgroup$
    – Justin
    Commented Jan 23, 2012 at 21:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.