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

Roger Bivand Roger.Bivand at nhh.no
Mon Jun 4 09:38:21 CEST 2007

```On Mon, 4 Jun 2007, Roger Bivand wrote:

A couple of improvements - main= is not being passed through, so use
title(). The function in geoR is legend.krige:

plot(as(input, "Spatial"), axes=TRUE)
title(main="meuse river data set\ncolour indicates distance to river")
legend.krige(x.leg=c(178200, 178400), y.leg=c(331800, 333800),
input\$dist, col=kleur, vertical=TRUE)

and a simple 1000m scale:

lines(c(180500, 181500), c(329570, 329570))
lines(c(180500, 180500), c(329570, 329620))
lines(c(181000, 181000), c(329570, 329620))
lines(c(181500, 181500), c(329570, 329620))
text(c(180500, 181000, 181500), c(329670, 329670, 329670),
labels=c("0", "500", "1000m"), cex=0.6)

Roger

> On Mon, 4 Jun 2007 Jan.Verbesselt at csiro.au wrote:
>
> > 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?
>
> Because the col= argument is passed using the "..." mechanism, it is seen
> twice in the image() method (once to draw the axes, next to draw the
> image) unless add=TRUE. So:
>
> plot(as(input, "Spatial"), axes=TRUE)
> image(input, col=kleur, main = paste("meuse river data set;",
> "colour indicates distance to river"), add=TRUE)
>
> works. col.axis in par() only sets the colour of the axis annotation, and
> is not used here.
>
> >
> > (How can a legend and scale be added to this plot?)
> >
>
> In principle, the axes give the cardinal directions and the scale.
> Although this is cartographic heresy, I have not seen anyone with a ruler
> using a scale on a computer display to read off distances, so that
> inserting a scale is probably overkill, the same with the north arrow. It
> can be done for presentation purposes, or if a reader demands it, but it
> is redundant is axes=TRUE. If axes=FALSE, then hand construction with
> lines() and text() (arrows() for N arrow) isn't hard.
>
> The legend can be added as you suggest, or using a similar function in
> geoR. Both involve placing manually - which could be improved by using the
> placing names from legend(), but still give no guarantee that content will
> not be overpainted. Perhaps the function from geoR could be migrated to
> sp, since geoR now depends on sp.
>
> > 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?
>
> The sp package provides classes and methods for Spatial*, maptools
> provides tools for simple import/export and interfaces to other R
> packages, rgdal more advanced import/export. None are designed to provide
> presentation cartographic output - they provide analytic graphics, and the
> functions to export the data if you need something more fancy.
>
> I find that users put far too much time into presentation, losing touch
> with what the data are telling them. My understanding of data
> visualisation is that communication depends on getting the important
> things across, which are the data, and an unobtrusive support for position
> (the real "map" is its representation inside the computer, not the
> hardcopy output).
>
> For example, informative setting of the class intervals is important -
> using the defaults in image() can be very deceptive.
>
> Hope this helps,
>
> Roger
>
> >
> > /***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)
> > plot(meuse.sl, add=T)
> > 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

```