Heat map per column with ggplot2

前端 未结 2 1410
陌清茗
陌清茗 2020-12-11 07:09

I\'m using this R script:

tableau <- read.table(
  text = 
    \"Net    B   C   D   E.(e)   F.(f)
a   1.88    0.15    0.60    10.00   90.00
b   2.05    0.         


        
2条回答
  •  有刺的猬
    2020-12-11 07:39

    Similar idea using tidyr and dplyr to reshape the data to long format and ggvis to plot the heatmap:

    library(dplyr)
    library(ggvis)
    library(tidyr)
    
    tableau %>% 
      gather(variable, value, -Net) %>%
      group_by(variable) %>%
      mutate(scale = percent_rank(value)) %>%
      mutate_each(funs(factor(.)), -value, -scale) %>%
      ggvis(~variable, ~Net, fill=~scale) %>%
      layer_rects(width = band(), height = band(), stroke := NA) %>%
      layer_text(
        x = prop("x", ~variable, scale = "xcenter"),
        y = prop("y", ~Net, scale = "ycenter", ),
        text:=~value, fontSize := 14, fontWeight := "bold", fill:="black", 
        baseline:="middle", align:="center") %>%
      scale_nominal("x", padding = 0, points = FALSE) %>%
      scale_nominal("y", padding = 0, points = FALSE) %>% 
      scale_nominal("x", name = "xcenter", padding = 1, points = TRUE) %>%
      scale_nominal("y", name = "ycenter", padding = 1, points = TRUE) %>%
      scale_numeric("fill", range = c("white", "steelblue")) %>%
      add_axis("x", properties = axis_props(grid = list(stroke = NA))) %>%
      add_axis("y", properties = axis_props(grid = list(stroke = NA))) %>%
      hide_legend("fill")
    

    Which gives:

    enter image description here

提交回复
热议问题