# [R-sig-Geo] Optimize image plot of a grid with legend - colors - and extras ?

Jan.Verbesselt at csiro.au Jan.Verbesselt at csiro.au
Mon Jun 4 06:30:00 CEST 2007

```Hi Roger,

I made a reproducible example: When you change the colors of the spatial
image the colors of the axes also change. How could this be solved?

(How can a legend and scale be added to this plot?)

All other tips and tricks are mostly welcome,

Thanks a lot,
Jan

Ps. Great that this sp() package make spatial analysis and visualization
possible in R! - Does maptools package offer better or alternative
mapping functions?

/***script***

library(fields)
library(sp)

data(meuse.grid)
coordinates(meuse.grid) = ~x+y
gridded(meuse.grid) = TRUE
data(meuse)
coordinates(meuse) = ~x+y
data(meuse.riv)
plot(meuse.riv)
meuse.sl = SpatialLines(list(Lines(list(Line(meuse.riv)))))

## image plot with points and lines
nr <- 200
ramp <- colorRamp(c("blue", "green"))
kleur <- rgb( ramp(seq(0, 1, length = nr)), max = 255)
par( mar=c(5,5,5,8))
input <- meuse.grid["dist"]
image(input, col=kleur, axes=T,main = "meuse river data set; colour
indicates distance to river", col.axis=1)
points(meuse, pch = 3)
image.plot( zlim=c(min(input\$dist),max(input\$dist)),
nlevel=nr,legend.only=TRUE, horizontal=F, col=kleur)

-----Original Message-----
From: Roger Bivand [mailto:Roger.Bivand at nhh.no]
Sent: Friday, 1 June 2007 7:01 PM
To: Verbesselt, Jan (Ensis, Clayton)
Cc: r-sig-geo at stat.math.ethz.ch
Subject: Re: Optimize image plot of a grid with legend - colors - and
extras ?

On Fri, 1 Jun 2007 Jan.Verbesselt at csiro.au wrote:

> Thanks Roger,
>
> It's all working now ; I plotted a shape file on top of a spatial grid
> image via the following script. Only, when you change the colors of
the
> image() the axis colors also change. I couldn't solve this and tried
> several options (par, axis(), etc), but nothing worked.

In order to find this out, please provide an example with the meuse.grid

dataset shipped with sp, and for the shape, the meuse.riv river contour.

It may be that the col= argument is being passed through by accident to
plot.Spatial(), but without a reproducible example, it is difficult to
say.

Roger

>
> I also used image.plot() of the fields library to add a legend to the
> image.
>
> Does somebody has some advise how to optimize these type of spatial
> image -grid plots with a legend, colors, and other extra's?
>
> All examples are welcome,
> Cheers and thanks,
> Jan
>
>
> # combining the  usual image function and adding a legend
> # first change margin for some more room
> nr <- 100
> ramp <- colorRamp(c("blue", "green"))
> kleur <- rgb( ramp(seq(0, 1, length = nr)), max = 255)
> par( mar=c(5,5,5,8), col.axis =1)
> image(GRID, col=kleur, axes=T)
> plot(shape,add =TRUE , border = "red", axes=F, las=1)
> image.plot( zlim=c(min(data["ndvi"]),max(data["ndvi"])),
> nlevel=nr,legend.only=TRUE, horizontal=F, col=kleur)
> box()
>
>
>

--
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

```