【问题标题】:heat map with image as background以图像为背景的热图
【发布时间】:2015-09-29 15:34:36
【问题描述】:

我正在尝试使用 R 制作热图。我正在尝试使用 ggplot2。我的实际数据框要大得多,但在这里我只包含一小部分

x <- c(502.9, 512.1, 716.6, 759.7, 776.1, 776.5, 736.1, 271.3, 304.7, 279.9, 263.8, 726.6, 767.6, 778.8, 779.2, 263.6, 291.8, 472.6, 499.9, 684.9) 
y <- c(374.6, 367.4, 378.1, 373.7, 381.4, 395.7, 412.1, 399.2, 364.6, 382.1, 409.1, 410.4, 411.1, 429.4, 477.4, 468.6, 406.5, 343.2, 356.9, 365.2)
a <- data.frame(x,y)
ggplot(a, aes(x = x, y =y))  + stat_density2d(geom = "tile", aes(fill = ..density..), contour = FALSE) + scale_fill_gradient (low= "green", high="red") + geom_point()

我想获得类似这张图片的东西,区域越红,该区域的点越多。

如你所见,当我尝试制作这个时,我的背景是绿色的。如何更改我的代码以获得图像中显示的热图?如何将图片作为背景?

谢谢!

【问题讨论】:

  • 看看ggmap::ggimage

标签: r ggplot2 heatmap


【解决方案1】:

这样的?

library(ggplot2)
data(hadley, package="ggmap")
img <- hadley
set.seed(1)      # for reproducible example
df  <- data.frame(x=rnorm(100,mean=c(150,250),sd=25),
                  y=rnorm(100,mean=480, sd=20))

ggplot(df, aes(x,y))  + 
  annotation_raster(img, xmin=-Inf, xmax=Inf, ymin=-Inf, ymax=Inf)+
  stat_density2d(geom = "polygon", aes(fill=..level..)) + 
  geom_point(size=0.5)+
  scale_fill_gradient(low="green",high="red") + 
  scale_x_continuous(limits=c(0,dim(img)[2]),expand=c(0,0))+
  scale_y_continuous(limits=c(0,dim(img)[1]),expand=c(0,0))+
  coord_fixed()

主要思想是使用geom="polygon"aes(fill=..level..)。图像本身可以使用annotation_raster(...) 添加,如this post 中所述。

向哈德利·威克姆致以诚挚的歉意。

【讨论】:

  • 太完美了,吉霍华德!非常感谢!
  • 我必须将我的分数转换为 z 分数。关于如何将背景中的图像调整为这些新分数的任何想法?
  • 我不明白这个问题。为什么需要调整背景图片?也许你应该用你的新数据(z分数)重申这个问题。
  • 如果您已经预先计算了关卡数据(即,除了xy,还有level 的另一列),这将如何工作?那么需要哪个stat(如果有的话)或geom
猜你喜欢
  • 1970-01-01
  • 2019-02-27
  • 2015-10-07
  • 1970-01-01
  • 1970-01-01
  • 2021-11-21
  • 2013-08-02
  • 2022-07-21
  • 1970-01-01
相关资源
最近更新 更多