简体   繁体   中英

A true heat map in R

I'd like to make a true heat map in R, much like a weather map, except my data is much more simple.

Consider this 3d data:

x <- c(1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,4,4,4,4,4)
y <- c(1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,1,2,3,4,5)
z <- rnorm(20)

The z would be color.

Here is what a discrete looking heatmap would like for this data: 在此输入图像描述

How can I make a heatmap such that the colors are smooth and the full 2d space is filled with smoothed out colors based on the z values.

Please include sample code, not just a link that will probably confuse me even more, and I've probably already visited that site anyhow. Thanks :)

Use the following:

interp in the akima package

image.plot in the fields package

x <- c(1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,4,4,4,4,4)
y <- c(1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,1,2,3,4,5)
z <- rnorm(20)

library(fields)
library(akima)

s <- interp(x,y,z)
image.plot(s)

smooth.2d in the fields package does a good job (and it is much faster than interp from akima package for larger number of input points.

library(fields)

x <- c(1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,4,4,4,4,4)
y <- c(1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,1,2,3,4,5)
z <- rnorm(20)

s = smooth.2d(z, x=cbind(x,y), theta=0.5)
image.plot(s)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM