我在ggplot中的一个小细节上遇到了麻烦。如您所见,第一个子组(第8个)在红色条之前具有蓝色条,而其他子组则具有相反的方向……我想不出一种使它们保持一致的方法。有什么想法吗?
这是我的代码:
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 (%)")