1、运行环境RStudio。打开数据集,这里以txhousing数据为例,选取city、year、sales三列数据。
2、设置基层图。fill设置为sales,sales值越大热度越高。library(ggplot2)p<-ggplot(data=txhousing,aes(x=year,y=city,fill=sales))
3、绘制热力图。直接加geom_tile。p+geom_tile()
4、geom_raster也可以达到相同的效果,设置参数interpolate = TRUE。geom_raster(aes(fill = density), interpolate = TRUE)
5、设置参数fill = dens坡纠课柩ity。p+ stat_density(aes(fill = ..density..)荑樊综鲶, geom = "raster", position = "identity")
6、添加参数binwidth = c(0.01,0.01)。p+geom_raster()+stat_bin2d(binwidth = c(0.01,0.01),colour="grey")可以根据实际需要进行设置。