[R] Filled vector contours
David Winsemius
dwinsemius at comcast.net
Mon Aug 25 00:58:27 CEST 2014
On Aug 24, 2014, at 1:30 PM, Jan Tosovsky wrote:
> On 2014-08-24 Prof Brian Ripley wrote:
>> On 24/08/2014 08:51, Jan Tosovsky wrote:
>>>
>>> I am trying to create vector output (SVG) of filled contours.
>>>
>>> I've found two approaches so far:
>>>
>>> (1)
>>> library('lattice')
>>> svg("D:/test.svg")
>>> filled.contour(volcano)
>>> #levelplot(volcano, panel=panel.levelplot.raster)
>>> dev.off()
>>>
>>> (2)
>>> library("raster")
>>> svg("D:/test.svg")
>>> rr <- raster(t(volcano))
>>> rc <- cut(rr, breaks= 10)
>>> pols <- rasterToPolygons(rc, dissolve=T)
>>> spplot(pols)
>>> dev.off()
>>>
>>> But I'd like to get smooth closed polygons not broken into small
>>> cells.
>>
>> How can a polygon be 'smooth'?
>
> This was related to the result of example (2), which is more coarse
> comparing it to the result of (1).
>
>>
>> But the region between two contours is not in general a closed polygon.
>
> I appologize for not being precise here.
>
> My goal is to get something like this:
> http://drifted.in/other/contours/composition.svg
>
> which is a composition of level plots, e.g.
> http://drifted.in/other/contours/level_plot.svg
>
> By 'level plot' I mean the cut of the data at certain level. It is always a
> closed polygon (or multiple polygons) consisting of contour lines connected,
> if required, by border lines.
>
> For me it is preferred way over 'isoband', which doesn't include areas
> hidden by upper levels:
> http://drifted.in/other/contours/isoband.svg
>
> Please note I am not sure if this terminology (isoband, level plot, etc) is
> correct.
>
Most people with any R experience would have thought that "levelplot" referred to something like:
(Example 6.9 from Lattice, by Deepayan Sarkar to which I have only added the contour line parameter which it appears is what you were seeking.)
env <- environmental
env$ozone <- env$ozone^(1/3)
env$Radiation <- equal.count(env$radiation, 4)
fm1.env <- lm(ozone ~ radiation * temperature * wind, env)
fm2.env <- loess(ozone ~ wind * temperature * radiation, env, span = 0.75, degree = 1)
fm3.env <- loess(ozone ~ wind * temperature * radiation, env, parametric = c("radiation", "wind"), span = 0.75, degree = 2)
library("locfit") ; library(lattice)
fm4.env <- locfit(ozone ~ wind * temperature * radiation, env)
w.mesh <- with(env, do.breaks(range(wind), 50))
t.mesh <- with(env, do.breaks(range(temperature), 50))
r.mesh <- with(env, do.breaks(range(radiation), 3))
grid <- expand.grid(wind = w.mesh, temperature = t.mesh, radiation = r.mesh)
grid[["fit.linear"]] <- predict(fm1.env, newdata = grid)
grid[["fit.loess.1"]] <- as.vector(predict(fm2.env, newdata = grid))
grid[["fit.loess.2"]] <- as.vector(predict(fm3.env, newdata = grid))
grid[["fit.locfit"]] <- predict(fm4.env, newdata = grid)
png()
print(levelplot(fit.linear + fit.loess.1 + fit.loess.2 + fit.locfit ~ wind * temperature | radiation, data = grid, contour=TRUE))
dev.off()
--
David.
>>> Are there better methods in R ?
>>
>> Neither of those are 'in R'!
>> But you could get a long way by looking at the following
>>
>> ?contourLines
>> ?polygon
>> ?grid::grid.polygon
>>
>> all of which are part of R. R is a fully-fledged programming
>> language and you could write SVG directly from R: using the svg device
>> is limited by the steps to the R graphics primitives (see the 'R
>> Internals' manual) and cairographics.
>>
>
> Thanks a lot for detailed explanation. Now I understand it more.
>
> Most of contouring libraries create the list of line fragments. And this
> seems to be the case also of R's 'contourLines'.
>
> But filling the areas requires a further post-processing (joining line
> fragments, joining contours, set the correct direction to preserve holes).
> And if I understand correctly, this is let to R 'libraries'. Unfortunately,
> none of these two is optimal for my use case.
>
> If there is any hidden option or another library (implementing e.g. Marching
> squares) which could produce output similar to that linked SVG file, I am
> one big ear.
>
> Thanks, Jan
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
David Winsemius
Alameda, CA, USA
More information about the R-help
mailing list