Home > database >  How to set the order for the fill variable in ggplot2 for R
How to set the order for the fill variable in ggplot2 for R

Time:12-22

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 enter image description here

  • Related