I have a data set with the following data(say):
n=50
df = data.frame(id =c(seq(1,n),seq(1,n)), pre_post = c(rep(0,n),rep(1,n)), q1 = sample(1:5,2*n, replace = TRUE), q2 = sample(1:5,2*n, replace = TRUE),q3 = sample(1:5,2*n, replace = TRUE),q4 = sample(1:5,2*n, replace = TRUE))
df$pre_post = as.factor(df$pre_post)
df$q1 = as.factor(df$q1)
df$q2 = as.factor(df$q2)
df$q3 = as.factor(df$q3)
df$q4 = as.factor(df$q4)
head(df)
I want a graph such that all the questions should be in x axis and stacks should be number of people who have responded as 1, 2, ...5 for pre and post.
How to achieve this?
I have 10 such questions and I need to plot it in a single graph.
Typically wanted to compare the frequency of each factor for each question in pre and post.
What i have done?
melted = melt(df, id.vars = c('id','pre_post'))
ggplot(melted, aes(x = pre_post, y =id , fill = value)) +
geom_bar(stat = 'identity', position = 'stack') + facet_grid(~variable)
And this gave me the following plot. But this graph seems to be not correct. Where am I wrong?