Ordering x-axis on correlation heat map using ggplot

前端 未结 3 1769
盖世英雄少女心
盖世英雄少女心 2021-01-26 04:49

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

相关标签:
3条回答
  • 2021-01-26 05:24

    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)
    
    0 讨论(0)
  • 2021-01-26 05:27

    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())
    

    enter image description here

    0 讨论(0)
  • 2021-01-26 05:39

    For this particular example, one option would be to name your variables x01, x02, etc and then they will order correctly.

    0 讨论(0)
提交回复
热议问题