Search code examples
rggvis

Heatmap merging common rows for a consolidated plot in R


I am using ggvis to create the heat map (plot1) using the following sample data.

Class Super_class   cell1   cell2   cell3
A1      A             2      3.96   0.6
A2      A             1      2.92   0
A3      A             5      0.56   6.4
A4      A             1      10.92  22.36
A5      A             0        0    5.32
B       B             0        0    1
C1      C             0.64     2.4  3.4
C2      C             0       3.6   2.56
C3      C             0.6      2     2
C4      C             1.96     1    9.96
C5      C             4.6    17.56   10

But since the Class can be classified in to super classes as A , B , C -- I would like to do a merged plot that has the same information as plot1 but with the colours overlayed to give more density (plot2).

In the plot that I have tried it looks like they are not the same; as the high intensity seen in A4,cell3 (plot1) is not reflected in the plot2 (for A,cell3)

Any suggestions will be of great help enter image description here

here is the code that i am using

dfm = melt(df)
sample <- dfm %>% 
  ggvis(~factor(variable), ~factor(Class), fill=~value) %>%
  layer_rects(width = band(), height = band(), strokeWidth := 0) %>%
  scale_nominal("x", padding = 0) %>%
  scale_nominal("y", padding = 0) %>%
  scale_numeric("fill", range=c("white", "red")) %>%
  add_legend("fill", title = "Score") %>%
  add_axis("x", orient='top',title = "Sample",properties=axis_props(labels = list(angle=70, fontSize=12))) %>%
  add_axis("y",orient='right', title = "Class")

Solution

  • It's unclear to me what you are trying to achieve, but from what I understand:

    library(ggvis)
    library(tidyr)
    library(dplyr)
    
    df %>% 
      gather(variable, value, -Class, -Super_class) %>%
      group_by(Super_class, variable) %>%
      summarise(value = sum(value)) %>%
      mutate_each(funs(factor), -value) %>%
      ggvis(~variable, ~Super_class, fill=~value) %>%
      layer_rects(width = band(), height = band(), strokeWidth := 0) %>%
      layer_text(
        x = prop("x", ~variable, scale = "xcenter"),
        y = prop("y", ~Super_class, scale = "ycenter"),
        text:=~value, fontSize := 14, fill:="black", 
        baseline:="middle", align:="center") %>%
      scale_nominal("x", padding = 0) %>%
      scale_nominal("y", padding = 0) %>%
      scale_nominal("x", name = "xcenter", padding = 1, points = TRUE) %>%
      scale_nominal("y", name = "ycenter", padding = 1, points = TRUE) %>%
      scale_numeric("fill", range=c("white", "red")) %>%
      add_legend("fill", title = "Score") %>%
      add_axis("x", orient = 'top', title = "Sample") %>%
      add_axis("y", orient = 'right', title = "Class")
    

    Which gives:

    enter image description here