Say I have a heatmap with contours like so:
set.seed(1)
X <- matrix(runif(100*200),nrow=100)
X <- apply(X,1,sort)
#png("Surface.png",width=800,height=400)
lattice::levelplot(t(X),contour=TRUE)
#dev.off()
getwd()
it looks like this:
There are many ways to make variants of this in R (fields::image.plot()
, image()
, and so forth. If I save this filled contour plot as a pdf, it's a squeaky-clean vector image that I can rescale and include in a conference poster (using e.g., Inkscape). It turns out in this case that the like-colored areas are actually individual raster cells rather than merged polygons implying a very large number of vertices, which hogs memory and slows down Inkscape after including a couple such surfaces in the poster. The easiest solution would be so save out to the ideal merged-cell format from R, where the contiguous like-colored areas are unified/merged polygon()
s or similar, thereby decreasing the number of vertices by an order of magnitude or more.
My question is whether there is some surface function that already does this by default, or a low effort way to emulate this kind of surface output. The high-effort approach would be to dive into R's spatial functions to merge like cells, but I'd rather avoid this. Thanks in advance!
Here is the "high-effort approach" (not really)
set.seed(1)
X <- matrix(runif(100*200),nrow=100)
X <- apply(X,1,sort)
lattice::levelplot(X,contour=TRUE)
library(raster)
r <- raster(X)
z <- cut(r, seq(0, 1, 0.1))
p <- rasterToPolygons(z, dissolve=TRUE)
spplot(p)