My dataframe:
Variable <- sample(-9:10)
Levels<-rep(c("N", "A", "L","B", "O", "C", "U", "R", "E", "Y" ),times=2)
ID<-rep(c("WT", "KO"), each=10)
df <- data.frame(Variable, Levels, ID)
I run ggplot and I get this:
If I had these two lines
df$ID=factor(df$ID, c("WT","KO"))
df$Levels=factor(df$Levels, c("N", "A", "L","B", "O", "C", "U", "R", "E", "Y" ))
I can get this
But there must be a way to do this without entering manually the levels
Just create your initial data frame with the correct factor, i.e.
The
unique
function helpfully maintains the correct order. Alternatively,