Reputation: 15
Given a matrix like:
m <- matrix(ncol = 4, nrow = 3)
colnames(m) <-c("attribute1-1","attribute1-2","attrbute1-3","attribute2")
rownames(m) <-c("sample1","sample2","sample3")
m[1,] <- c(5,7,0,2)
m[2,] <- c(0,0,0,6)
m[3,] <- c(0,0,4,1)
I would like to make a heat map where attributes 1-1 : 1-3 are used to indicate presence/absence (whether that individual entry is blank or not, depending if there is a positive value or 0) and attribute 2 is used to indicate intensity. In other words: if, according to the attribute1 column there is the presence of that row in that instance of attribute 1 (e.g. positive value for attribute1-2 for row1) then the intensity is prescribed by attribute 2.
I think the presence/absence could be done using a formula such as "decostand": X.pa <- decostand(X,method="pa")
And heatmaps can be created from several packages, notably "Superheat".
I can't seem to get anything working, though. Any advice as to how much to continue would be much appreciated. Thank you in advance.
Upvotes: 1
Views: 480
Reputation: 85
superheat
https://rlbarter.github.io/superheat/index.html is a great library to quickly visualize matrices
library(superheat)
m <- matrix(ncol = 4, nrow = 3)
colnames(m) <-c("attribute1-1","attribute1-2","attrbute1-3","attribute2")
rownames(m) <-c("sample1","sample2","sample3")
m[1,] <- c(5,7,0,2)
m[2,] <- c(0,0,0,6)
m[3,] <- c(0,0,4,1)
superheat(m)
Upvotes: 1
Reputation: 1898
Thank you for adding sample data. Solution using ggplot2
:
m <- matrix(ncol = 4, nrow = 3)
colnames(m) <-c("attribute1-1","attribute1-2","attrbute1-3","attribute2")
rownames(m) <-c("sample1","sample2","sample3")
m[1,] <- c(5,7,0,2)
m[2,] <- c(0,0,0,6)
m[3,] <- c(0,0,4,1)
m
#> attribute1-1 attribute1-2 attrbute1-3 attribute2
#> sample1 5 7 0 2
#> sample2 0 0 0 6
#> sample3 0 0 4 1
# library
library(tidyverse)
# adjust data
dat <- m %>%
as_tibble(rownames="sample") %>%
gather(name,value, -c(sample,attribute2)) %>%
mutate(attribute2=case_when(value==0 ~ 0, TRUE ~attribute2))
#make plot
ggplot(dat,aes(name,sample)) +
geom_tile(aes(fill = attribute2), colour = "white") +
scale_fill_gradient(low = "white", high = "steelblue")
Created on 2020-04-04 by the reprex package (v0.3.0)
Upvotes: 1