简体   繁体   中英

R heatmap with number of members in cell

I have a data set I'm using to create a heatmap. One of the issues is that some cells have very few members in them and can have outliers that aren't averaged out by other members.

To this end I'd like to include in the cell (plot in the center of the cells), how many examples are actually in the cell.

Below is my code for the heatmap:

library(fields)
library(akima)

x1 <- round(runif(20) * 100,0)
y1 <- round(runif(20) * 100,0)
z1 <- round(runif(20) * 100,0)

s <- interp(x1,y1,z1,
        xo = seq(0,100,20)
        ,yo = seq(0,100,20)
        )

image.plot(s)

Any suggestions?

After computing both the corners and centers of the cells, you can use findInterval and table to count the observations.

library(fields)
library(akima)

x1 <- floor(runif(20) * 100)
y1 <- floor(runif(20) * 100)
z1 <- floor(runif(20) * 100)

# Corners of the cells, to count the observations
x0 <- seq(0,100,20)
y0 <- seq(0,100,20)

# Centers of the cells, for the interpolation
x00 <- x0[-length(x0)] + diff(x0) / 2
y00 <- y0[-length(y0)] + diff(y0) / 2

s <- interp(x1,y1,z1, xo=x00, yo=y00)
image.plot(x=x0, y=y0, z=s$z)

counts <- table( 
  findInterval(x1, x0),
  findInterval(y1, y0)
)
# Plot the observations, to check that I have not confused rows and columns
points( x1, y1 )
# Number of observations
text(x=x00[row(counts)], y=y00[col(counts)], labels=counts)

image.plot与计数

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