Order data inside a geom_tile

后端 未结 2 1967
长发绾君心
长发绾君心 2020-12-03 15:11

i have a data frame and i want to generate a geom_tile() plot from it , but I want the graph to be ordered not based on the alphabetic but based on a variable i

2条回答
  •  时光取名叫无心
    2020-12-03 15:23

    As an answer to the duplicate question, here's a solution that uses ggplot directly and does not require changing the data.

    x <- structure(list(V1 = c("a", "y", "w", "p", "v", "h", "i"), 
                        V2 = c("r", "w", "q", "m", "l", "q", "g"), 
                        V3 = c( "5", "2", "9", "2", "1", "3", "0")), 
                   .Names = c("V1", "V2", "V3"), class = "data.frame", 
                   row.names = c(NA, -8L))
    
    x <- x[1:7,]
    
    ggplot(x, aes(V1, V2, fill=V3)) + geom_tile() +
      scale_x_discrete(limits=(x$V1)[order(x$V3)]) + 
      scale_y_discrete(limits=(x$V2)[order(x$V3)])
    

提交回复
热议问题