Reputation: 11
I'm having trouble with a tiny detail here in the ggplot. As you can see, the first subgroup (8th) has blue bar before the red bar, while the others have the other way around... I can't figure out a way to get them to be consistent. Any thoughts?
Here is my code:
library(ggplot2)
library(reshape2)
grade <- factor(c("8th","10th","12th"), levels = c("8th","10th","12th"))
alc.py <- c("37", "38", "41")
alcpy.st <- c("23", "42", "58" )
alcohol.py <- data.frame(grade, alc.py, alcpy.st)
alcohol.py <- melt(alcohol.py, id.vars = "grade")
ggplot(alcohol.py, aes(x=grade, y=value, fill=variable)) +
geom_bar(stat = "identity", position = position_dodge()) +
theme_minimal() +
xlab("Past Year Alchol Use") +
ylab("Percentage of use (%)")
Upvotes: 1
Views: 155
Reputation: 76402
Just add another aesthetic, group by variable
.
ggplot(alcohol.py, aes(x=grade, y=value, fill=variable, group=variable)) +
geom_bar(stat = "identity", position = position_dodge()) +
theme_minimal() +
xlab("Past Year Alcohol Use") +
ylab("Percentage of use (%)")
Upvotes: 1