I need help maintaining a consistent ordering of bars across groups of the x-axis variable. Currently, within each group, the bars are displaying in ascending order of the y-axis value.
In the plot above, the X variable takes values Low or High. The Group variable also takes values Low or High. In this bar plot, I need the blue bar to come before the red bar for both X=low and X=high. Thanks in advance.
barplot1 <- structure(list(Y = c("1", "1.80", "2.80", "1.31"),
lb = c("1", "0.84","1.55", "0.75"), ub = c("1", "3.88", "5.04", "2.28"),
X = structure(c(1L, 2L, 1L, 2L), .Label = c("Low", "High"), class = "factor"),
Group = structure(c(1L, 1L, 2L, 2L), .Label = c("Low", "High"), class =
c("ordered", "factor"))), .Names = c("Y","lb", "ub", "X", "Group"), class =
"data.frame", row.names = c("Low X, Low Group",
"High X, Low Group", "Low X, High Group", "High X, High Group"))
barplot1$X <- factor(barplot1$X,levels = c("Low","High"))
barplot1$Group <- factor(barplot1$Group,levels = c("Low","High"),ordered=T)
example <-ggplot(barplot1, aes(x=X, y=Y, fill=Group)) +
theme_minimal() +
scale_fill_manual(values=c("#0073C2B2","#DC0000B2")) +
geom_bar(stat="identity", position = position_dodge()) +
geom_errorbar(aes(ymin = lb, ymax = ub), width=.2, position=position_dodge(.9)) +
ggtitle("Example") +
labs(x="X",y="Y") +
labs(fill = "Group",size=12) +
theme(plot.title = element_text(size=14,face="bold",hjust = 0.5),
axis.text=element_text(size=12),axis.title=element_text(size=14,face="bold"),
legend.text = element_text(size=12),legend.title = element_text(size=14),
plot.caption = element_text(size = 12))