I am trying to create a correlation heat map using ggplot, but I cannot seem to control the order of my variables on the x or y axis. Specifally, ggplot seems to try to order t
Try this if the padded zero solution doesn't fit for your purpose:
cust_breaks<-unique(keep$Var1[order(as.numeric(gsub("x","",keep$Var1)))])
ggplot(keep,aes(Var1,Var2),xlab=NULL,ylab=NULL) +
geom_tile(aes(fill = value),colour = "white") +
scale_fill_gradient(low = "white",high = "steelblue") +
theme(axis.title.x=element_blank(),axis.title.y=element_blank()) +
scale_x_discrete(limits=cust_breaks) +
scale_y_discrete(limits=cust_breaks)
This works for me:
keep$Var1 <- factor(keep$Var1, levels = unique(keep$Var1), ordered = TRUE)
keep$Var2 <- factor(keep$Var2, levels = unique(keep$Var2), ordered = TRUE)
ggplot(keep,aes(Var1,Var2),xlab=NULL,ylab=NULL) + geom_tile(aes(fill = value),colour = "white") + scale_fill_gradient(low = "white",high = "steelblue")+theme(axis.title.x=element_blank(),axis.title.y=element_blank())
For this particular example, one option would be to name your variables x01
, x02
, etc and then they will order correctly.