# questionnaire stacked bar plot with R

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!

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

• 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! – ftonti Jan 23 '12 at 21:19
• 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') – Justin Jan 23 '12 at 21:29