热图与图像作为背景 [英] heat map with image as background

查看:196
本文介绍了热图与图像作为背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



<$ p (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 (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,319.6,349.2,369.2,349.3, (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()

我想获得与此图像类似的东西,该区域越红,该区域的点数就越多。





马可在想法是使用 geom =polygon aes(fill = .. level ..)。可以使用 annotation_raster(...)添加图片本身,如



对Hadley Wickham致以诚挚的歉意。


I am trying to make a heat map with R. I am trying with ggplot2.My actual dataframe is much larger but in here I only include a small part

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()

I would like to obtain something similar to this image, the more red the area is, the more points there are in that area.

As you can see, when I try to make this, my background is green. How can I change my code to obtain a heatmap as the one showed in the image? How can I put an image as background?

Thanks!

解决方案

Something like this?

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()

The main idea is to use geom="polygon" with aes(fill=..level..). The image itself can be added using annotation_raster(...) as described in this post.

With sincere apologies to Hadley Wickham.

这篇关于热图与图像作为背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆