[R-sig-Geo] Vedr.: Re: plotting a polygon with holes
Bjarke Christensen
Bjarke.Christensen at sydbank.dk
Fri Jun 12 10:01:59 CEST 2009
That works like a charm:
> myDEM.pix <- as(myDEM, 'SpatialPixelsDataFrame')
> idx <- overlay(myDEM.pix, islands)
> plot(oceans, col="blue")
> plot(islands, col="white, add=T)
> image(myDEM.pix[!is.na(idx),], add=T)
Thank you very much, once again.
Bjarke Christensen.
Roger Bivand
<Roger.Bivand at nhh
.no> Til
Bjarke Christensen
11-06-2009 15:21 <Bjarke.Christensen at sydbank.dk>
cc
r-sig-geo at stat.math.ethz.ch
Besvar venligst Emne
til Re: [R-sig-Geo] plotting a polygon
Roger.Bivand at nhh. with holes
no
On Thu, 11 Jun 2009, Bjarke Christensen wrote:
>
> Hi,
>
> I have a shapefile/SpatialPolygonsDataFrame of coastlines. Each polygon
> corresponds to an island, and there are no holes. I can plot this so that
> the islands are shaded by using
>> plot(islands, col="gray")
>
> What I now want, is to plot the same information so that the ocean is
blue
> and the islands are transparent. Something like:
>> image(myDEM)
>> plot(ocean, col="lightblue", add=T)
>
> which I would hope would allow the DEM to be visible on the islands, but
> not in the ocean.
As you observe, the approach you are taking does not work, as the R
graphics devices work by over-painting in layers. To see the image, it has
to be painted after the enclosing rectangle. Holes are painted by
re-painting the hole in a chosen background colour, which by default is
"transparent", so you just get lots of blue.
Why not do something like
o <- overlay(myDEM, islands)
(untried) to get just the raster cells within the island polygons as a
SpatialPixelsDataFrame object and image() that, possibly setting the
background to a suitable colour (NAs will get transparent by default).
This works with the graphics system, rather than trying to work round it -
it doesn't "remember" that there is anything on the canvas that should be
protected from overpainting, so it is safer just to paint what needs
painting. I can also imagine painting first with reduced opacity (or
intensity) in a different palette, then overpainting just the islands with
full intensity in the target palette, which might be more visually
pleasing than just flat blue sea.
Hope this helps,
Roger
--
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Helleveien 30, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: Roger.Bivand at nhh.no
More information about the R-sig-Geo
mailing list