Reputation: 319
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?
e.g
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:
Upvotes: 1
Views: 87
Reputation: 1948
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())
Upvotes: 2