Bastien
Bastien

Reputation: 3176

add margins to a plotRGB graph

I'm trying to plot 6 rgb images with raster::plotRGB and I have a problem adjusting the margins. It seems by default, plotRGB overwrites every par(mar) settings that we called before. My goal is to have a 3*2 symetrical graph panel.

library(raster)
par.default <- par(no.readonly=T)

png("c:\\temp\\img_plotRGB.png", width=6, height=9,unit="in", res=300)
par(mfrow=c(3,2), oma=c(1,1,1,1), mar=c(1,1,1,1))
for(i in 1:6){
  r <- g <- b <- raster(ncol=10, nrow=10, res=1, crs=CRS("+init=EPSG:32198"), ext=extent(c(0,10,0,10)))
  values(r) <- runif(ncell(r))
  values(g) <- runif(ncell(r))
  values(b) <- runif(ncell(r))
  rgb = rgb<-stack(r*255,g*255,b*255)
  plotRGB(rgb)
}
par(par.default)
dev.off()

gives that: enter image description here

You can see there are no separation between two graphs on the same line even if par(mar=c(1,1,1,1) is set.

If I add the setting axes=TRUEto plotRGB I get the layout I want, but with axis I do not want :

png("c:\\temp\\img_plotRGB_axes.png", width=6, height=9,unit="in", res=300)
par(mfrow=c(3,2), oma=c(1,1,1,1), mar=c(1,1,1,1))
for(i in 1:6){
  r <- g <- b <- raster(ncol=10, nrow=10, res=1, crs=CRS("+init=EPSG:32198"), ext=extent(c(0,10,0,10)))
  values(r) <- runif(ncell(r))
  values(g) <- runif(ncell(r))
  values(b) <- runif(ncell(r))
  rgb = rgb<-stack(r*255,g*255,b*255)
  plotRGB(rgb, axes=TRUE)
}
par(par.default)
dev.off()

enter image description here

Any idea how to get the first graph with spacing or the second graph without the axis written?

Upvotes: 1

Views: 831

Answers (2)

Matthias  Hinz
Matthias Hinz

Reputation: 99

Update May 11th, 2019: This issue has now been addressed by the package developers, see: https://github.com/rspatial/raster/issues/48

So, starting from the raster package version >= 2.9-3, the custom margins will work as expected when you add the parameter magins=TRUE when plotting, i.e. plotRGB(rgb, margins=TRUE).

Note that until the update is pushed to the package's stable release on the CRAN repository, you will have to install the current development version from GitHub, either by downloading the source code or more conformably using the devtools package so:

library(devtools)
install_github("rspatial/raster")

For raster package version < 2.9-3 (original answer):

Better late than never: It is possible to suppress plotting the axes by setting the graphical parameters xaxt and yaxt to "n" (see ?par for the related R docs)

Thus ist is possible to use the side-effect of axes=TRUE in plotRGB so that (some) graphical parameters are honored without actually plotting the axes. This would look like the following:

library(raster)
par.default <- par(no.readonly=T)
png("img_plotRGB.png", width=6, height=9,unit="in", res=300)

library(raster)
par(mfrow=c(3,2),mar=rep(1,4),oma=rep(1,4), xaxt="n", yaxt="n")
for(i in 1:6){
  r <- g <- b <- raster(ncol=10, nrow=10, res=1, ext=extent(c(0,10,0,10)))
  values(r) <- runif(ncell(r))
  values(g) <- runif(ncell(r))
  values(b) <- runif(ncell(r))
  rgb = rgb<-stack(r*255,g*255,b*255)
  plotRGB(rgb, axes=TRUE)
}

par(par.default)
dev.off()

This is the result:

enter image description here

If the problem is the bug, it should actually be submitted as a report to the package's bug tracker. Anyways, this workaround should be sufficient for the above-mentioned purpose.

Upvotes: 3

Bastien
Bastien

Reputation: 3176

I've finally found a hack to (kind of) fix it. It's less than ideal but I really think it's a bug in the function so there's is no other way to get around it without rewriting the function.

The idea of my fix is to make a SpatialPolygons out of the extent of the raster and plot it twice with different width borders:

png("c:\\temp\\img_plotRGB_border.png", width=6, height=9,unit="in", res=300)
par(mfrow=c(3,2), oma=c(0,0,0,0), mar=c(0,0,0,0))
for(i in 1:6){
  r <- g <- b <- raster(ncol=10, nrow=10, res=1, crs=CRS("+init=EPSG:32198"), ext=extent(c(0,10,0,10)))
  values(r) <- runif(ncell(r))
  values(g) <- runif(ncell(r))
  values(b) <- runif(ncell(r))
  rgb = rgb<-stack(r*255,g*255,b*255)
  plotRGB(rgb)

###  New lines!
  plot(as(extent(c(0,10,0,10)),"SpatialPolygons"),border="black", lwd=8, add=T)
  plot(as(extent(c(0,10,0,10)),"SpatialPolygons"),border="white", lwd=4, add=T)
###  New lines!
}
par(par.default)
dev.off()

enter image description here

It works but it can cause problem if you want to add legend or other things like that.

Upvotes: 0

Related Questions