[R-sig-Geo] multi-map figure

Benjamin.Galuardi galuardi at mit.edu
Thu Oct 8 16:40:16 CEST 2015


Hi Chris,

I modified your script to get where you want to be. I added a library 
requirement (for better axes) and commeted out a few things. The margin 
issue, for this example, stems from the fact that 'map' function adds a 
margin by default. See how I got around that and modify to your liking. 
You dont want the Y-axis borders touching or the x axis will overlap 
between plots.

Ben

# load libraries
library(maps)
library(mapdata)
library(sp)

# load data
DivrepNZ <- read.csv("C:/Downloads/DivrepNZ.csv")

# set up plot
par(mfrow = c(1, 3))
mymar = c(.5,.5,.5,.5)

# panel 1
map("world","New Zealand", xlim=c(166,179),ylim=c(-48,-34),lty=1,
col="gray", fill=FALSE, mar = mymar)
# title(xlab = NULL, ylab="Latitude (degrees S)", cex.lab=1.2)
# map.axes(cex.axis=0.85, las=1)
z <- DivrepNZ$Nativearborescent
zcol <- colorRampPalette(c('greenyellow', 'green4',
'black'))(100)[as.numeric(cut(z, breaks = 100))]
points(DivrepNZ$lon, DivrepNZ$lat, pch = 15, cex = 0.45, col = zcol)
box()
text(178,-35, "(a)", cex=1.2)
xleft <- 167
xright <- 168
ybot <- -39
yint <- (39 - 35) / 100
ytop <- ybot + yint
for(c in colorRampPalette(c('greenyellow', 'green4', 'black'))(100))
{ybot = ybot + yint
   ytop = ytop + yint
   rect(xleft, ybot, xright, ytop, border = NA, col = c)
   print(c(xleft, xright, ybot, ytop, c))
}
labels <- round(seq(min(z), max(z), length.out = 5),2)
text(c(xright + 0.2),
      seq(-39, -35, length.out = 5),
      labels = as.character(labels),
      cex = 0.85,
      pos = 4)
text(178, -35, "(a)")
degAxis(1)
degAxis(2)

# panel 2
map("world","New Zealand", xlim=c(166,179),ylim=c(-48,-34),lty=1,
col="gray", fill=FALSE, yaxt=NULL, mar = mymar)
# title(xlab = "Longitude (degrees E)", ylab=NULL, cex.lab=1.2)
# map.axes(cex.axis=0.85, las=1)
z <- DivrepNZ$Divaricates
zcol <- colorRampPalette(c('orange', 'orangered4',
'black'))(100)[as.numeric(cut(z, breaks = 100))]
points(DivrepNZ$lon, DivrepNZ$lat, pch = 15, cex = 0.45, col = zcol)
box()
text(178,-35, "(b)", cex=1.2)
xleft <- 167
xright <- 168
ybot <- -39
yint <- (39 - 35) / 100
ytop <- ybot + yint
for(c in colorRampPalette(c('orange', 'orangered4', 'black'))(100))
{ybot = ybot + yint
   ytop = ytop + yint
   rect(xleft, ybot, xright, ytop, border = NA, col = c)
   print(c(xleft, xright, ybot, ytop, c))
}
labels <- round(seq(min(z), max(z), length.out = 5),2)
text(c(xright + 0.2),
      seq(-39, -35, length.out = 5),
      labels = as.character(labels),
      cex = 0.85,
      pos = 4)
text(178, -35, "(b)")
degAxis(1)

# panel 3
map("world","New Zealand", xlim=c(166,179),ylim=c(-48,-34),lty=1,
col="gray", fill=FALSE, yaxt=NULL, mar = mymar)
Show axes with horizontal tick labels
# map.axes(cex.axis=0.85, las=1)
# title(xlab = NULL, ylab=NULL, cex.lab=1.2)
DivrepNZ <- read.csv("DivrepNZ.csv")
z <- DivrepNZ$divrep
zcol <- colorRampPalette(c('lightskyblue', 'deepskyblue4',
'black'))(100)[as.numeric(cut(z, breaks = 100))]
points(DivrepNZ$lon, DivrepNZ$lat, pch = 15, cex = 0.45, col = zcol)
box()
text(178,-35, "(c)", cex=1.2)
xleft <- 167
xright <- 168
ybot <- -39
yint <- (39 - 35) / 100
ytop <- ybot + yint
for(c in colorRampPalette(c('lightskyblue', 'deepskyblue4', 'black'))(100))
{ybot = ybot + yint
   ytop = ytop + yint
   rect(xleft, ybot, xright, ytop, border = NA, col = c)
   print(c(xleft, xright, ybot, ytop, c))
}
labels <- round(seq(min(z), max(z), length.out = 6),2)
text(c(xright + 0.2),
      seq(-39, -35, length.out = 6),
      labels = as.character(labels),
      cex = 0.85,
      pos = 4)
text(178, -35, "(c)")
degAxis(1)
On 10/4/2015 9:31 PM, Christopher Lusk wrote:
> Hi - I want to combine 3 maps in one figure, showing geographic patterns of
> species richness. Looks OK with the script below, but as the axes are the
> same on all 3 maps, I've been trying to remove the space between the 3
> panels, and get rid of y-axis ticks and tick labels on panels (b) and (c)
> as well. But neither of these is happening, despite several approaches. Can
> anyone see what I need to change? Any advice would be appreciated. The data
> can be downloaded here:
>
> http://sci.waikato.ac.nz/sites/clusk/DivrepNZ.csv
> ....................
>
> library(maps)
> library(mapdata)
>
> par(mfrow = c(1, 3))
> par(mar = c(0, 0, 0, 0), oma = c(1, 1.5, 0.5, 0.5))
>
> map("worldHires","New Zealand", xlim=c(166,179),ylim=c(-48,-34),lty=1,
> col="gray", fill=FALSE)
> title(xlab = NULL, ylab="Latitude (degrees S)", cex.lab=1.2)
> map.axes(cex.axis=0.85, las=1)
> DivrepNZ <- read.csv("DivrepNZ.csv")
> z <- DivrepNZ$Nativearborescent
> zcol <- colorRampPalette(c('greenyellow', 'green4',
> 'black'))(100)[as.numeric(cut(z, breaks = 100))]
> points(DivrepNZ$lon, DivrepNZ$lat, pch = 15, cex = 0.45, col = zcol)
> box()
> text(178,-35, "(a)", cex=1.2)
> xleft <- 167
> xright <- 168
> ybot <- -39
> yint <- (39 - 35) / 100
> ytop <- ybot + yint
> for(c in colorRampPalette(c('greenyellow', 'green4', 'black'))(100))
> {ybot = ybot + yint
>    ytop = ytop + yint
>    rect(xleft, ybot, xright, ytop, border = NA, col = c)
>    print(c(xleft, xright, ybot, ytop, c))
> }
> labels <- round(seq(min(z), max(z), length.out = 5),2)
> text(c(xright + 0.2),
>       seq(-39, -35, length.out = 5),
>       labels = as.character(labels),
>       cex = 0.85,
>       pos = 4)
> text(178, -35, "(a)")
>
> map("worldHires","New Zealand", xlim=c(166,179),ylim=c(-48,-34),lty=1,
> col="gray", fill=FALSE, yaxt=NULL)
> title(xlab = "Longitude (degrees E)", ylab=NULL, cex.lab=1.2)
> map.axes(cex.axis=0.85, las=1)
> DivrepNZ <- read.csv("DivrepNZ.csv")
> z <- DivrepNZ$Divaricates
> zcol <- colorRampPalette(c('orange', 'orangered4',
> 'black'))(100)[as.numeric(cut(z, breaks = 100))]
> points(DivrepNZ$lon, DivrepNZ$lat, pch = 15, cex = 0.45, col = zcol)
> box()
> text(178,-35, "(b)", cex=1.2)
> xleft <- 167
> xright <- 168
> ybot <- -39
> yint <- (39 - 35) / 100
> ytop <- ybot + yint
> for(c in colorRampPalette(c('orange', 'orangered4', 'black'))(100))
> {ybot = ybot + yint
>    ytop = ytop + yint
>    rect(xleft, ybot, xright, ytop, border = NA, col = c)
>    print(c(xleft, xright, ybot, ytop, c))
> }
> labels <- round(seq(min(z), max(z), length.out = 5),2)
> text(c(xright + 0.2),
>       seq(-39, -35, length.out = 5),
>       labels = as.character(labels),
>       cex = 0.85,
>       pos = 4)
> text(178, -35, "(b)")
>
> map("worldHires","New Zealand", xlim=c(166,179),ylim=c(-48,-34),lty=1,
> col="gray", fill=FALSE, yaxt=NULL)
> Show axes with horizontal tick labels
> map.axes(cex.axis=0.85, las=1)
> title(xlab = NULL, ylab=NULL, cex.lab=1.2)
> DivrepNZ <- read.csv("DivrepNZ.csv")
> z <- DivrepNZ$divrep
> zcol <- colorRampPalette(c('lightskyblue', 'deepskyblue4',
> 'black'))(100)[as.numeric(cut(z, breaks = 100))]
> points(DivrepNZ$lon, DivrepNZ$lat, pch = 15, cex = 0.45, col = zcol)
> box()
> text(178,-35, "(c)", cex=1.2)
> xleft <- 167
> xright <- 168
> ybot <- -39
> yint <- (39 - 35) / 100
> ytop <- ybot + yint
> for(c in colorRampPalette(c('lightskyblue', 'deepskyblue4', 'black'))(100))
> {ybot = ybot + yint
>    ytop = ytop + yint
>    rect(xleft, ybot, xright, ytop, border = NA, col = c)
>    print(c(xleft, xright, ybot, ytop, c))
> }
> labels <- round(seq(min(z), max(z), length.out = 6),2)
> text(c(xright + 0.2),
>       seq(-39, -35, length.out = 6),
>       labels = as.character(labels),
>       cex = 0.85,
>       pos = 4)
> text(178, -35, "(c)")
>
>
> --
> Dr. Chris Lusk
> Environmental Research Institute
> The University of Waikato
> Private Bag 3105, Hamilton
> New Zealand / Aotearoa
> http://sci.waikato.ac.nz/sites/clusk/
> Ph 64 7 838 4205
> ~  ~  ~  ~  ~  ~  ~
>
> 	[[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo

-- 
==================================================
Benjamin Galuardi
PhD Student
NMFS-Seagrant Population Dynamics Fellow
SMAST, UMass Dartmouth



More information about the R-sig-Geo mailing list