Elizabeth
Elizabeth

Reputation: 6581

Heat maps with bar plots or mosaic plots in r

I have a matrix showing the trait similarity between species at 2 different sites i.e.

relationship<-matrix(1:6,ncol=2)
colnames(relationship)<-c("Sp1","Sp2")
rownames(relationship)<-c("Sp3","Sp4","Sp5")

     Sp1 Sp2
Sp3   1   4
Sp4   2   5
Sp5   3   6

I also have a matrix showing their abundance at each site

abundance<-matrix(1:5,ncol=1)
rownames(abundance)<-c("Sp1","Sp2","Sp3","Sp4","Sp5")
colnames(abundance)<-"abundance"

       abundance
 Sp1         1
 Sp2         2
 Sp3         3
 Sp4         4
 Sp5         5

I would like to create either a heat map with a bar plots along the axes like this:

enter image description here

UPDATE TO ORIGINAL QUESTION

Alternatively (as suggested by BenBarnes ) I would like to create a mosaic plot using the abundances to control the size of the tiles and the matrix to indicate the 'intensity' of the colour. So very crudely for the example above the mosaic plot would look like this:

enter image description here

Also I would like to know your opinion on which method most clearly displays the relationship between the species as well as the relationship between their abundances?

Upvotes: 0

Views: 871

Answers (1)

dickoa
dickoa

Reputation: 18437

Using graphics package functions (barplot, image, etc.) you can do something like this.

bp1 <- barplot(t(abundance[3:5, ]), width = 0.2, space = 0.7, plot = FALSE)
bp2 <- barplot(t(abundance[1:2, ]), horiz = TRUE, width = 0.05, space = 1, plot = FALSE)


par(fig = c(0, 0.8, 0, 0.8), new = TRUE)
par(xaxt = "n", yaxt = "n")
image(relationship)
par(fig = c(0, 0.8, 0.55, 1), new = TRUE)
barplot(t(abundance[3:5, ]), width = 0.2, space = 0.7)
text(bp1, abundance[3:5,] - 0.5, c("Sp3", "Sp4", "Sp5"))
par(fig = c(0.65, 1, 0, 0.8), new = TRUE)   
barplot(t(abundance[1:2, ]), horiz = TRUE, width = 0.05, space = 1)
text(abundance[1:2,] - 0.5, bp2, c("Sp1", "Sp2"))

enter image description here

Upvotes: 3

Related Questions