I want to know if there is a way for defining the order of appearance per group for the side by side barplot in ggplot. My dataset is
> df V1 V2 V3 1 True Joint 0.18 2 True Individual 0.82 3 Estimate Joint 0.37 4 Estimate Individual 0.63
The following line of code
library(ggplot2)
ggplot(df, aes(x = V2, y = V3, fill = V1)) +
geom_col(position = position_dodge2()) +
scale_fill_manual(values=c("steelblue","coral2")) +
guides(fill = guide_legend(reverse = T)) +
scale_x_discrete(limits = unique(df$V2)) +
theme(legend.title = element_blank()) +
xlab("Structure") + ylab("Variance") +
ggtitle("Variance Explained by Joint and Individual structure")
gives me this graph side-by-side-barplot.
I want the coral-colored bar to be on the left and the blue bar on the right for each group. I have tried reordering the dataset with lines of code such as
df = arrange(df, df$V1)
df = df[order(df$V1, decreasing = F),]
df = df[c(1,3,2,4),]
df = df[c(1,4,3,2),]
but they did not work.