I spent several hours searching for reordering a grouped bar chart on stackoverflow.com but I did not find the answer I was looking for. I want to change the order of the bars and legend to print 'before', 'during' and 'after' in that order but R keeps printing the factors alphabetically like this 'after' before' during' and don't know how to fix it.
If someone can help me, I will be thankful!
This is the order R displays which I want to change.
Here is my code:
df <- data.frame(time = c("after","before","during","before","after","before","during","after","after","during", "during","after","before","during","before"),
variable = c("granny", "red del", "other", "golden", "star", "granny", "red del", "other", "golden", "star", "granny", "red del", "other", "golden", "star"),
value = c(34,66,7,12,99,35,66,82,55,48, 27,55,82,42,26))
df$time = as.factor(df$time)
df$variable = as.factor(df$variable)
ggplot(df, aes(x = variable, y = value, fill = time)) +
geom_bar(position='dodge', stat='identity') +
theme_minimal() +
labs(x=' ', y=' ', title=' ') +
theme(plot.title = element_text(hjust=0.5, size=20, face='bold')) +
theme_bw() +
theme(axis.line = element_line(color='black'),
plot.background = element_blank(),
panel.grid.minor = element_blank(),
panel.grid.major = element_blank())