简体   繁体   中英

Creating custom heatmap

I want to create a custom heatmap, whereby a matrix defines the intensity (alpha) of a series of squares with uniquely specified colors. Additionally, the axis defining each square will be unique (see example below).

Which packages may help do this? The geom_rect function from ggplot2 (used in this different question seems promising but too tightly coupled to a given plot?


eg

Data:

      [,1] [,2] 
[1,]   30    5   
[2,]    3    50  

Axis:

x_bounds <- t(matrix(c(
    0,10,
    10,30
), 2))
y_bounds <- t(matrix(c(
    0,-50,
    -50,1000
), 2))

Result mock-up:

在此处输入图片说明

Does this give you what you want?

library(ggplot2)
x_bounds <- c(0,10,30)
y_bounds <- c(0,-50,1000)

df <- data.frame(x = c(0,1,0,1), 
                 y = c(0,0,1,1), 
                 fill = c("red","green","blue","yellow"),
                 alpha = c(0.6,0.6,0.5,0.8))

ggplot(data = df) +
  geom_rect(aes(xmin = x, xmax = x+1, ymin = y, ymax = y+1, 
                fill =  fill, alpha = alpha)) +
  scale_x_continuous(breaks = min(df$x):(max(df$x)+1),
                     labels = x_bounds) +
  scale_y_continuous(breaks = min(df$y):(max(df$y)+1),
                     labels = y_bounds) +
  scale_fill_identity() +
  theme(panel.background=element_blank(),
        panel.border=element_blank(),
        panel.grid.major=element_blank(),
        panel.grid.minor=element_blank(),
        plot.background=element_blank())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM