I followed the answer on this question: Cluster stacked bargraph
Using the following code:
test <- data.frame(person=c("8.3. The number of people coming to me \n me, who
are who want pizza are too many \n,
and too aggressive.", "group 2", "group 3", "5", "6"),
value1=c(100,150,120,100,100), # Agree
value2=c(25,30,45,100,100) , # Neutral
value3=c(25,30,45,100,100), # Disagree
value4=c(100,120,150,100,100), # Agree
value5=c(10,12,15,100,100), # Neutral
value6=c(50,40,70,100,100)) # Disagree
library(reshape2) # for melt
melted <- melt(test, "person")
melted$cat <- ''
melted[melted$variable == 'value1' | melted$variable == 'value2' | melted$variable == 'value3',]$cat <- "GP"
melted[melted$variable == 'value4' | melted$variable == 'value5'| melted$variable == 'value6',]$cat <- "LW"
melted$gender <- ''
melted[melted$variable %in% sprintf("value%i",c(1,4)),]$gender <- "Agree"
melted[melted$variable %in% sprintf("value%i",c(2,5)),]$gender <- "Disagree"
melted[melted$variable %in% sprintf("value%i",c(3,6)),]$gender <- "Neutral"
p = ggplot(melted, aes(x = cat, y = value, fill = gender))
p + geom_bar(stat = 'identity', position = 'stack') + facet_grid(~ person) +
scale_fill_manual(values = c("orangered","green","dodgerblue2")) +
theme(panel.background = element_rect(fill = 'white'))
My question is:
How do I fix the order of the stacks so that neutral is between agree and Neutral? Is there anyway to make the graph horizontal so that I can have more wide space to fit the question? I also want the numbers to show inside each bar, for example, the left most red bar should have 100 written inside it.
I have used some of the liekert libraries in R and Python but I was not able to group the bars and also there were other issues. This ggplot graph is the closest I have come to creating the chart I need, so I would appreciate if your answer is fully reproducible with the numbers I gave.