I´m trying to graph a series of 11 variables from 5 groups in a stacked barplot. This is my data and code
inf_source <- structure(list(inf_episode = c("First", "Second",
"Third", "Fourth", "Fifth"), Pneumonia = c(16,6,2,0,0),
IAI = c(6,2,0,0,0), SSTI = c(7,0,2,0,0), Bacteremia = c(4,14,8,6,2),
Sinusitis =c(14,10,0,0,0), Other =c(8,7,7,4,0),
UTI=c(14,13,1,0,0), CDI=c(0,0,3,0,2), Dental=c(1,3,1,0,0),
Colitis=c(2,2,1,0,0), CLABSI=c(0,0,3,0,0)),
.Names = c("inf_episode", "Pneumonia", "IAI", "SSTI",
"Bacteremia", "Sinusitis", "Other", "UTI", "CDI", "Dental", "Colitis", "CLABSI"),
class = "data.frame", row.names = c(NA, 5L))
inf_source$inf_episode <- factor(inf_source$inf_episode,
levels = c("First", "Second", "Third", "Fourth", "Fifth"))
inf_source$inf_episode <- reorder(inf_source$inf_episode, rowSums(inf_source[-1]))
md <- melt(inf_source, id=(c("inf_episode")))
ggplot(data = md, aes(x = reorder(inf_episode, value, sum), y = value, fill = variable)) +
geom_col()
The graph I got is this:
The bars start with "Fifth" and end with "First", and I cannot change them to start with "First" and finish with "Fifth". Also, I would like to add the number of time each variable appears in every group, and if possible, the stack size to reflect the number. Any ideas?
I have tried reordering the groups but it´s not working.