R with the number of members in the cell

I have a dataset that I use to create a heat map. One of the problems is that some cells have very few members in them and may have outliers that are not averaged by other members.

To this end, I would like to include in the cell (the graph in the center of the cells) how many examples really are in the cell.

Below is my code for heat map:

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?

+4
source share
1 answer

After calculating the angles 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 with counts

+2
source

Source: https://habr.com/ru/post/1494278/


All Articles