Reset par to the default values at startup
Every time a new device is opened par() will reset, so another option is simply do dev.off()
and continue.
Reset the graphical parameters back to default values without use of dev.off()
See ?par. The idea is that you save them as they are when you found them, and then restore:
old.par <- par(mar = c(0, 0, 0, 0))
## do plotting stuff with new settings
Now restore as they were before we changed mar
:
par(old.par)
after par(fig), mtext is slightly off
op4 does not have a usr element. After the small inset plot, usr is changed. To reset, something like this can be used:
dev.off()
plot(1:10)
usr <- par("usr")
op4 <- par(fig=c(0.1,0.8,0.3,0.8), new=TRUE)
plot(rnorm(400), type="l")
par(op4)
par(usr=usr)
mtext("hello", adj=1, col=4); mtext("hello", adj=0, col=4)
Fixing RStudio's default par() settings
If the plot window in RStudio is too small, you will get this error.
Keeping that in mind, two options to try are:
- Enlarge the plot window :-)
- Try plotting with the standard graphics device (which I believe should be
x11()
for you).
Changing par() settings to default when plotting rasters
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.
How to reset par(mfrow) in R
You can reset the mfrow parameter
par(mfrow=c(1,1))
How do I reset all options() arguments to their default values?
If you restart your R session, it will reset the options to the default values.
Options are saved in a list, and calling options()
will show that list.
You can save the default options after restarting R:
backup_options <- options()
You can make any changes you need, and then to revert to the default options:
options(backup_options)
Related Topics
How to Suppress the Creation of a Plot While Calling a Function in R
Matching a Sequence in a Larger Vector
Find All Date Ranges for Overlapping Start and End Dates in R
Flip Ordering of Legend Without Altering Ordering in Plot
Package "Rvest" for Web Scraping Https Site with Proxy
Ggplot Custom Scale Transformation with Custom Ticks
Split a String Column into Several Dummy Variables
R Dplyr Rowwise Mean or Min and Other Methods
R: How to Use Coord_Cartesian on Facet_Grid with Free-Ranging Axis
Replace Missing Values (Na) in One Data Set with Values from Another Where Columns Match
Update Shiny's 'Selectinput' Dropdown with New Values After Uploading New Data Using Fileinput
How to Convert Dd/Mm/Yy to Yyyy-Mm-Dd in R
Accessing Columns in Data.Table Using a Character Vector of Column Names
Mutate with Case_When and Contains
Adding Ellipses to a Principal Component Analysis (Pca) Plot