1

Below is my sample data, basically its a matrix with row names as person names and some columns for each of these rows. All I have in the data is just zeros and ones. I would like to visualize it using heatmaps. (reds for 0s and green for 1s or any other color coding). How do I accomplish this using R? you can show me using any example dataset with just ones and zeros (binary values).

enter image description here

yome
  • 2,853
  • 5
  • 20
  • 18
  • Possible duplicate of [ggplot2 heatmap with colors for ranged values](https://stackoverflow.com/questions/10981324/ggplot2-heatmap-with-colors-for-ranged-values) or any other standard heatmap question... `tg<-matrix(sample(0:1,100,replace=TRUE),nrow=20)` and `image(tg,col=c("green","red"))` – nadizan Dec 27 '17 at 12:29

3 Answers3

2

Just another approach using ggplot

library(ggplot2)
library(reshape2)
library(plyr)
library(scales)

df <- structure(list(people = structure(c(2L, 1L), .Label = c("Dwayne", "LeBron"), class = "factor"),
                 G = c(1L, 0L),
                 MIN = c(1L, 0L),
                 PTS = c(0L, 1L),
                 FGM = c(0L,0L),
                 FGA = c(0L,0L),
                 FGP = c(1L,1L)),
            .Names = c("people", "G", "MIN", "PTS", "FGM", "FGA", "FGP"),
            class = "data.frame",
            row.names = c(NA, -2L))


df.m <- melt(df)
df1.m <- ddply(df.m, .(variable), transform, rescale = value)
p <- ggplot(df1.m, aes(variable, people)) +
geom_tile(aes(fill = rescale), colour = "black")
p + scale_fill_gradient(low = "green", high = "red")
show(p)

Adopted from this tutorial

Bussller
  • 1,961
  • 6
  • 36
  • 50
0

With highcharter:

library(highcharter)
library(tidyr)
library(dplyr)
df<-data.frame(row=c("Dwayne","James"),G=c(1,0),MIN=c(1,0),PTS=c(0,1),FGM=c(0,0),FGA=c(0,0),FGP=c(1,1))
rownames(df)<-c("Dwayne","James")
df$row<-rownames(df)
data<-df%>%
     tidyr::gather(row,value)%>%
     setNames(c("name","variable","value"))


hchart(data, "heatmap", hcaes(x = variable, y = name, value = value)) %>% 
  hc_colorAxis(stops = color_stops(2, c("red","green")))

enter image description here UPDATE: You can add hc_size(height = 800) for height=800 or make something like that

  x<-50
  hg<-length(unique(data$name))*x+100

  hchart(data, "heatmap", hcaes(x = variable, y = name, value = value)) %>% 
    hc_colorAxis(stops = color_stops(2, c("red","green")))%>% 
    hc_size(height = hg)

Where each row in dataset makes chart bigger by 50 points. You can change it in x

jyjek
  • 2,627
  • 11
  • 23
  • Why not just use the `image` function, or any other "one command heatmap function", such as `pheatmap`, `heatmap.2`, etc? – nadizan Dec 27 '17 at 12:51
  • thanks, it worked, but I have more than 200 rows to display and the output seems to be squeezed to display in the page. I dont want the rows to be squeezed as it's making the visualization unclear. I'm okay to use scroll bar to pull it down, but I want the visualization to be clear. How do I accomplish it? – yome Dec 27 '17 at 13:45
  • @yome hope `hc_size(height = 800)` can fix it – jyjek Dec 27 '17 at 14:20
  • @jyjek thanks for the update, its perfect, but actual values are being shown on the heatmap. I want it to show anyway but not "actual" values. The way I want to show is, wherever there is green heatmap (non zero value) there I want to show nothing number 5. where should I tweak the code? – yome Dec 29 '17 at 06:31
  • @jyjek else how do I change color of the values that are being displayed in the heatmap? – yome Dec 29 '17 at 08:11
  • @yome try `hchart(data, "heatmap", hcaes(x = variable, y = name, value = value), dataLabels =list(enabled = TRUE, format='{point.value}')) %>% hc_colorAxis(stops = color_stops(2, c("orange","yellow")))` – jyjek Dec 29 '17 at 08:36
  • @jyjek its resulting in the same chart hchart(as.matrix(final), "heatmap", hcaes(x = variable, y = name, value = value), dataLabels =list(enabled = TRUE, format='{point.value}')) %>% hc_colorAxis(stops = color_stops(2, c("green","red")))%>%hc_size(height = 500) – yome Dec 29 '17 at 08:49
  • @jyjek else, I'm still okay if I can make the labels disappear. – yome Dec 29 '17 at 09:06
0

This answer uses plotly and hence adding it as another answer. Using the same data as the following one.

library(plotly)
df1 <- as.matrix(df)
p <- plot_ly(x = colnames(df), y = df[,1], z = as.matrix(df[-1]), colors = colorRamp(c("green", "red")), type = "heatmap")

This is much simpler than the ggplot2 in terms of getting the output.

Hope this helps!

Bussller
  • 1,961
  • 6
  • 36
  • 50