As a follow up, I was finally successful, all I needed to do was use
image() rather than plot() for the hillshade.

###Workable example
library(raster)

ethiopia <- getData('GADM', country = 'ETH', level = 0)

a <- extent(ethiopia)
b <- raster(ext = a)
b[] <- 1
c <- b
d <- rasterize(ethiopia, b)
b <- mask(b, d)
c <- mask(c, d)
xy <- xyFromCell(b, 1:ncell(b))

plot(b) # such that there is space in right margin for following raster
legend and we can use rect() for the blue
rect(min(xy[, 1]+0.05),min(xy[, 2]+0.05),max(xy[, 1]+0.05),max(xy[,
2]+0.05),col = 'lightsteelblue2')
image(c,add=TRUE)
plot(b,add=TRUE,legend=FALSE)
plot(ethiopia,add=TRUE,lwd=1.5,border='#000000')

On Thu, May 17, 2012 at 10:54 AM, Adam Sparks <adamhsparks@gmail.com> wrote:

> Dear all,
> I'm attempting to produce some finished product maps using R and have been
> struggling with an issue.
>
> I want to use a hillshade object created from a DEM using the hillShade()
> function as a backdrop for my raster data that I'm presenting.
>
> Behind that I want to use lightsteelblue2 as the color where there is
> water.
>
> On top of this I want to plot the country outlines for the areas of
> interest.
>
> So in summary I have:
>  the first layer, blue for water
>  the second layer, hillshade DEM
>  the third layer, my raster data I'm presenting
>  the fourth layer, the country outline
>
> Two problems arise. The first is, if I create an empty plot using
> plot(type = 'n') I don't have the correct margins for a legend.
>
> So I can just plot my raster data once, then put the blue rectangle on top
> of that and I have the proper margins.  Not elegant but it works.
>
> The second issue that I've not been able to sort out is that when I plot
> the country outline it doesn't respect the plot area. It acts as if there
> is no margin there. This issue only happens when you add a fourth object to
> the plot.  It doesn't seem to matter what the fourth one is, but the fourth
> plot will not fit within the plot area.  An example with Ethiopia is below.
>
> ### Workable example
>
> library(raster)
>
> ethiopia <- getData('GADM', country = 'ETH', level = 0)
>
> a <- extent(ethiopia)
> b <- raster(ext = a)
> b[] <- 1
> c <- b
> d <- rasterize(ethiopia, b)
> b <- mask(b, d)
> c <- mask(c, d)
> xy <- xyFromCell(b, 1:ncell(b))
>
> plot(b) # such that there is space in right margin for following raster
> legend and we can use rect() for the blue
> rect(min(xy[, 1]+0.05),min(xy[, 2]+0.05),max(xy[, 1]+0.05),max(xy[,
> 2]+0.05),col = 'lightsteelblue2')
> plot(c,add=TRUE,legend=FALSE)
> plot(b,add=TRUE,legend=FALSE)
> plot(ethiopia,add=TRUE,lwd=1.5,border='#000000')
>
> ### eoe
>
> Is there a better way to do this, I could do it all in QGIS, but would
> prefer to just have it all scripted in R if I need to rerun analysis.
>
> --
> --
> Adam Sparks
> Plant Pathologist and GIS Specialist
> IRRI
> DAPO Box 7777, Metro Manila, Philippines
> ph: +63(2) 580-5600
>
>
>


-- 
--
Adam Sparks
Plant Pathologist and GIS Specialist
IRRI
DAPO Box 7777, Metro Manila, Philippines
ph: +63(2) 580-5600

	[[alternative HTML version deleted]]

