Im following the example given in this post for creating a grouped and stacked bar-chart: How to produce stacked bars within grouped barchart in R
From that post:
library(reshape2)
library(dplyr)
library(ggplot2)
test <- data.frame(person=c("A", "B", "C", "D", "E"),
value1=c(100,150,120,80,150),
value2=c(25,30,45,30,30) ,
value3=c(100,120,150,150,200))
melted <- melt(test, "person")
melted$cat <- ''
melted[melted$variable == 'value1',]$cat <- "first"
melted[melted$variable != 'value1',]$cat <- "second"
ggplot(melted, aes(x = cat, y = value, fill = variable)) +
geom_bar(stat = 'identity', position = 'stack') +
facet_grid(~ person)
As it is above, the plot orders value2 on top of value3.
What I am trying to do is change the order of the stacked portion,ie; I'd like to place value3 on top of value2.
Ive tried manually changing the order of the variable:
melted2 <- melted %>%
arrange(desc(variable))
ggplot(melted2, aes(x = cat, y = value, fill = variable)) +
geom_bar(stat = 'identity', position = 'stack') +
facet_grid(~ person)
But the plot output looks identical to the first one. Essentially, the reordering of the input data does not accomplish the task.
Thank you in advance!