user11418708
user11418708

Reputation: 902

How to adjust the axis of a heatmap?

I am using Jaccard coefficient to identify similarities and dissimilarities between people.

However I am struggling with representing the coefficients (0 - dissimilar, 1 similar ).

Also when I want to past my data structure I receive the following error:

Error in dput(head, m) : 'file' must be a character string or connection

To create the heatmap I am using the following code:

heatmap(m, Colv = NA, Rowv = NA, 
        scale = "none", 
        xlab = "Identification Numbers",
        ylab = "Identification Numbers")

When using part of my data just 12 cases I receive the following plot enter image description here

If I increase the number of cases the axis of the heatmap are diffciutl to read.

How can i adjust the axis to show every 10th case?

Also how can i add a legend to the heatmap?

Many thanks

enter image description here

Upvotes: 0

Views: 169

Answers (1)

Humpelstielzchen
Humpelstielzchen

Reputation: 6441

There is no in-built support for either unfortunately. But here is a workaround for the axis breaks: Create a character vector for the row and column names and replace 9 of 10 elements with empty space.

 mat <- matrix(runif(10000, min = 0, max = 10), ncol = 100, byrow = T)

rows <- as.character(1:nrow(mat))
rows[-seq(0, length(rows), 10)] <- ""

cols <- as.character(1:ncol(mat)) 
cols[-seq(0, length(cols), 10)] <- ""

heatmap(mat,
        Colv = NA, Rowv = NA,
        labRow = rows,
        cexRow = 1,
        labCol = cols,
        cexCol = 1)

enter image description here

If you're willing to use another package I recommend levelplot() from lattice-package:

(plot makes no sense, it's just a visual to show what it can do)

library(lattice)
library(RColorBrewer)

rows <- seq(11010903, 11011203, length.out = nrow(mat))
cols <- seq(11010903, 11011203, length.out = ncol(mat))

pal <- colorRampPalette(c("red", "yellow"), space = "rgb")
levelplot(mat, xlab="", ylab="", 
          row.values = rows, column.values = cols,
          col.regions = pal(5), at = seq(1,10, by = 2), scales = list(tck = c(1,0))) 

enter image description here

Upvotes: 1

Related Questions