Learn R Programming

inlmisc (version 0.2.4)

PlotMap: Plot Method for Maps

Description

This function maps raster and point data. A key showing how the colors map to raster values is shown below the map. The width and height of the graphics region will be automagically determined in some cases.

Usage

PlotMap(r, p = NULL, ..., layer = 1, att = NULL, n = NULL,
  breaks = NULL, xlim = NULL, ylim = NULL, zlim = NULL, asp = NULL,
  extend.xy = FALSE, extend.z = FALSE, reg.axs = TRUE, dms.tick = FALSE,
  bg.lines = FALSE, bg.image = NULL, bg.image.alpha = 1, pal = NULL,
  col = NULL, max.dev.dim = c(43, 56), labels = NULL, scale.loc = NULL,
  arrow.loc = NULL, explanation = NULL, credit = NULL, shade = NULL,
  contour.lines = NULL, rivers = NULL, lakes = NULL, roads = NULL,
  draw.key = NULL, draw.raster = TRUE, file = NULL, close.file = TRUE,
  useRaster)

Arguments

r
Raster*, SpatialGridDataFrame, or CRS. An object that can be converted to a raster layer, or a coordinate reference system (CRS).
p
SpatialPointsDataFrame. Spatial point data to be plotted.
...
Graphics parameters to be passed to AddPoints. Unused if p = NULL.
layer
integer. Layer to extract from if r is of class RasterStack/Brick or SpatialGridDataFrame.
att
integer or character. The levels attribute to use in the Raster Attribute Table (RAT); requires r values of class factor.
n
integer. Desired number of intervals to partition the range of raster values (or zlim if specified) (optional).
breaks
numeric. Vector of break points used to partition the colors representing numeric raster values (optional).
xlim
numeric. Vector of length 2 giving the minimum and maximum values for the x-axis.
ylim
numeric. Vector of length 2 giving the minimum and maximum values for the y-axis.
zlim
numeric. Vector of length 2 giving the minimum and maximum raster values for which colors should be plotted.
asp
numeric. The y/x aspect ratio for spatial axes. Defaults to 1 (one unit on the x-axis equals one unit on the y-axis) when r is projected, otherwise, a calculated value based on axes limits is used.
extend.xy
logical. If true, the spatial limits will be extended to the next tick mark on the axes beyond the grid extent.
extend.z
logical. If true, the raster value limits will be extended to the next tick mark on the color key beyond the measured range.
reg.axs
logical. If true, the spatial data range is extended.
dms.tick
logical. If true and r is projected, the axes tickmarks are specified in degrees, minutes, and decimal seconds (DMS).
bg.lines
logical. If true, grids or graticules are drawn in back of the raster layer using white lines and a grey background.
bg.image
RasterLayer. An image to drawn in back of the main raster layer r.
bg.image.alpha
numeric. Opacity of the background image from 0 to 1.
pal
function. Color palette to be used to assign colors in the plot, rainbow by default.
col
character. Vector of colors to be used in the plot. This argument requires breaks specification for numeric values of r and overrides any palette function specification. For numeric values there should be one less color than breaks. Factors require a color for each level.
max.dev.dim
numeric. Vector of length 2 giving the maximum width and height for the graphics device in picas, respectively. Suggested dimensions for single-column, double-column, and sidetitle figures are c(21, 56), c(43, 56), and c(56, 43), respectively. This argument is only applicable when the file argument is specified.
labels
list. Describes the location and values of labels in the color key. This list may include components at and labels.
scale.loc
character. Position of the scale bar: "bottomleft", "topleft", "topright", or "bottomright" to denote scale location.
arrow.loc
character. Position of the north arrow: "bottomleft", "topleft", "topright", or "bottomright" to denote arrow location.
explanation
character. Label explaining the raster cell value.
credit
character. Label crediting the base map.
shade
list. If specified, a semi-transparent shade layer is drawn on top of the raster layer. This layer is described using a list of arguments supplied to raster::hillShade function. Passed arguments include "angle" and "direction". Additional arguments also may be passed that control the vertical aspect ratio ("z.factor") and color opacity ("alpha").
contour.lines
list. If specified, contour lines are drawn. The contours are described using a list of arguments supplied to contour. Passed arguments include "drawlables", "method", and "col".
rivers
list. If specified, lines are drawn. The lines are described using a list of arguments supplied to the plot method for SpatialLines. Passed arguments include "x", "col", and "lwd".
lakes
list. If specified, polygons are drawn. The polygons are described using a list of arguments supplied to the plot method for SpatialPolygons. Passed arguments include "x", "col", "border", and "lwd". Bitmap images require a regular grid.
roads
list. If specified, lines are drawn. The lines are described using a list of arguments supplied to the plot method for SpatialLines. Passed arguments include "x", "col", and "lwd".
draw.key
logical. If true, a color key should be drawn.
draw.raster
logical. If true, the raster image is drawn.
file
character. Name of the output file. Specifying this argument will start a graphics device driver for producing a PDF or PNG file format---the file extension determines the format type. The width and height of the graphics region will be automagically determined and included with the function's returned values, see "Value" section for details; these device dimensions can be useful when creating similar map layouts in dynamic reports.
close.file
logical. If true, the graphics device driver is shut down when the function exits. Unused if file = NULL
useRaster
logical. If true, a bitmap raster is used to plot r instead of using polygons. If UseRaster is not specified, raster images are used when the getOption("preferRaster") is true.

Value

Used for the side-effect of a new plot generated. Returns a list object with the following graphical parameters:
din
device dimensions (width, height), in inches.
usr
extremes of the coordinates of the plotting region (x1, x2, y1, y2).
heights
relative heights on the device (upper, lower) for the map and color-key plots.

See Also

AddScaleBar, AddColorKey

Examples

Run this code
r <- raster::raster(nrow = 10, ncol = 10, crs = NA)
r[] <- 1L
r[51:100] <- 2L
r[3:6, 1:5] <- 8L
r <- raster::ratify(r)
rat <- raster::levels(r)[[1]]
rat$land.cover <- c("Pine", "Oak", "Meadow")
rat$code <- c(12, 25, 30)
levels(r) <- rat
PlotMap(r, att = "land.cover", col = c("grey", "orange", "purple"))
PlotMap(r, att = "code")

r <- raster::raster(system.file("external/test.grd", package="raster"))
credit <- "Label crediting the map."
explanation <- "Label explaining the raster cell value."
PlotMap(r, scale.loc = "bottomright", dms.tick = TRUE, credit = credit,
        explanation = explanation)
data(meuse, package = "sp")
sp::coordinates(meuse) = ~ x + y
points(meuse)

val <- PlotMap(r, scale.loc = "topleft", dms.tick = TRUE, credit = credit,
               explanation = explanation, file = "Rplots1.pdf")
print(val)

pdf(file = "Rplots2.pdf", width = val$din[1], height = val$din[2])
PlotMap(r, scale.loc = "topleft", dms.tick = TRUE, credit = credit,
        explanation = explanation)
points(meuse)
dev.off()

file.remove(c("Rplots1.pdf", "Rplots2.pdf"))
graphics.off()

Run the code above in your browser using DataLab