创建自定义热图

时间:2015-12-23 22:12:35

标签: r plot ggplot2

我想创建一个自定义热图,其中矩阵定义具有唯一指定颜色的一系列正方形的强度(alpha)。此外,定义每个方块的轴将是唯一的(参见下面的示例)。

哪些套餐可以帮助您做到这一点?来自"word1 word2 word3".rangeOfWord(2) => 6 to 10 的{​​{1}}函数(在this different question中使用)似乎很有希望,但与给定的情节紧密耦合?

例如

数据:

geom_rect

轴:

ggplot2

结果模型:

enter image description here

1 个答案:

答案 0 :(得分:2)

这会给你你想要的吗?

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())
相关问题