2

When running the following code, R is returning the image show below.

How should I change my par() settings (provided below) so that it returns a single image? I think that this happened because I changed par() settings manually. Is there a way to set par() to default settings, given that I don't know what my default settings were?

library(raster)
r1 <- brick(system.file("external/rlogo.grd", package="raster"))
plotRGB(r1)
plot(r1, xlim=c(0, 50), ylim=c(0, 50))


> par()
$xlog
[1] FALSE

$ylog
[1] FALSE

$adj
[1] 0.5

$ann
[1] TRUE

$ask
[1] FALSE

$bg
[1] "white"

$bty
[1] "o"

$cex
[1] 1

$cex.axis
[1] 1

$cex.lab
[1] 1

$cex.main
[1] 1.2

$cex.sub
[1] 1

$cin
[1] 0.2000000 0.2666667

$col
[1] "black"

$col.axis
[1] "black"

$col.lab
[1] "black"

$col.main
[1] "black"

$col.sub
[1] "black"

$cra
[1] 14.4 19.2

$crt
[1] 0

$csi
[1] 0.2666667

$cxy
[1] 3.659406 4.879208

$din
[1] 9.000000 4.208333

$err
[1] 0

$family
[1] ""

$fg
[1] "black"

$fig
[1] 0 1 0 1

$fin
[1] 9.000000 4.208333

$font
[1] 1

$font.axis
[1] 1

$font.lab
[1] 1

$font.main
[1] 2

$font.sub
[1] 1

$lab
[1] 5 5 7

$las
[1] 0

$lend
[1] "round"

$lheight
[1] 1

$ljoin
[1] "round"

$lmitre
[1] 10

$lty
[1] "solid"

$lwd
[1] 1

$mai
[1] 1.360000 1.093333 1.093333 0.560000

$mar
[1] 5.1 4.1 4.1 2.1

$mex
[1] 1

$mfcol
[1] 1 1

$mfg
[1] 1 1 1 1

$mfrow
[1] 1 1

$mgp
[1] 3 1 0

$mkh
[1] 0.001

$new
[1] FALSE

$oma
[1] 0 0 0 0

$omd
[1] 0 1 0 1

$omi
[1] 0 0 0 0

$page
[1] TRUE

$pch
[1] 1

$pin
[1] 9.000000 4.208333

$plt
[1] 0 1 0 1

$ps
[1] 16

$pty
[1] "m"

$smo
[1] 1

$srt
[1] 0

$tck
[1] NA

$tcl
[1] -0.5

$usr
[1] -31.83663 132.83663   0.00000  77.00000

$xaxp
[1]   0 100   2

$xaxs
[1] "r"

$xaxt
[1] "s"

$xpd
[1] FALSE

$yaxp
[1]  0 70  7

$yaxs
[1] "r"

$yaxt
[1] "s"

$ylbias
[1] 0.2

enter image description here

jbaums
  • 27,115
  • 5
  • 79
  • 119
user2543622
  • 5,760
  • 25
  • 91
  • 159
  • Of relevance: http://stackoverflow.com/questions/9292563/how-do-i-reset-the-graphical-parameters-back-to-default-values-without-use-of-de – thelatemail Apr 30 '15 at 22:12
  • @thelatemail why? OP's mfrow and mfcol settings are already the default – rawr Apr 30 '15 at 22:37
  • 1
    @rawr - I'm simply providing some more info on their statement: "Is there a way to set par() to default settings given that i dont know what were my default settings?" – thelatemail Apr 30 '15 at 23:21

1 Answers1

4

The plot method for raster objects redefines mfrow depending on the number of layers the raster has.

Your raster image has three layers (one for each band: red, green, and blue), as seen here:

r1

## class       : RasterStack 
## dimensions  : 77, 101, 7777, 3  (nrow, ncol, ncell, nlayers)
## resolution  : 1, 1  (x, y)
## extent      : 0, 101, 0, 77  (xmin, xmax, ymin, ymax)
## coord. ref. : +proj=merc 
## names       : red, green, blue 
## min values  :   0,     0,    0 
## max values  : 255,   255,  255 

As a result, when plotted, mfrow is set to c(2, 2) to accommodate the three layers.

Take a look at getMethod('plot', 'Raster') to see what's going on behind the scenes. Here's part of it:

...
if (nl > 1) {
      if (missing(nc)) {
        nc <- ceiling(sqrt(nl))
      }
      else {
        nc <- max(1, min(nl, round(nc)))
      }
      if (missing(nr)) {
        nr <- ceiling(nl/nc)
      }
      else {
        nr <- max(1, min(nl, round(nr)))
        nc <- ceiling(nl/nr)
      }
      old.par <- par(no.readonly = TRUE)
      on.exit(par(old.par))
      par(mfrow = c(nr, nc), mar = c(4, 4, 2, 2))
...

If you wanted to plot just a single band (layer), or two of them, you could use the following, in which case mfrow will be set to c(1, 1) or c(1, 2), respectively:

plot(r1[[3]])   # third band; equivalently, plot(r1, 3)
plot(r1[[1:2]]) # bands 1 and 2; equivalently, plot(r1, 1:2)

As noted by @RobertH, you can modify the layout with the nc argument to the plot method, e.g. plot(r1, nc=1) will give you a single column of three plots.


In contrast, raster::plotRGB plots such RGB raster objects as a single RGB image.

jbaums
  • 27,115
  • 5
  • 79
  • 119
  • 1
    And you can use `plot(x, 2)` to see a particular layer (the second one in this case) or `plot(b, nc=1)` to change the par columns and rows (to one column in this example). – Robert Hijmans May 01 '15 at 01:07