Learn R Programming

tmap (version 1.4-1)

tm_shape: Specify the shape object

Description

Creates a tmap-element that specifies the shape object. Also the projection and covered area (bounding box) can be set. It is possible to use multiple shape objects within one plot (see tmap-element).

Usage

tm_shape(shp, is.master = NA, projection = NULL, bbox = NULL, unit = "km", unit.size = 1000, line.center.type = c("segment", "midpoint"), ...)

Arguments

shp
shape object, which is one of
  1. SpatialPolygons(DataFrame)
  2. SpatialPoints(DataFrame)
  3. SpatialLines(DataFrame)
  4. SpatialGrid(DataFrame)
  5. SpatialPixels(DataFrame)
  6. RasterLayer, RasterStack, or RasterBrick

For drawing layers tm_fill and tm_borders, 1 is required. For drawing layer tm_lines, 3 is required. Layers tm_bubbles and tm_text accept 1 to 3. For layer tm_raster, 4, 5, or 6 is required.

is.master
logical that determines whether this tm_shape is the master shape element. The bounding box, projection settings, and the unit specifications of the resulting thematic map are taken from the tm_shape element of the master shape object. By default, the first master shape element with a raster shape is the master, and if there are no raster shapes used, then the first tm_shape is the master shape element.
projection
character that determines the projection. Either a PROJ.4 character string or a shortcut. See get_proj4 for a list of shortcut values. By default, the projection is used that is defined in the shp object itself, which can be obtained with get_projection.
bbox
bounding box. One of the following:

If unspecified, the current bounding box of shp is taken. The bounding box is feed to bb (as argument x. The other arguments of bb can be specified directly as well (see ..).

unit
unit specification. Needed when calculating density values in choropleth maps (argument convert2density in tm_fill) drawing a scale bar with tm_scale_bar. See also unit.size.
unit.size
size of the unit in terms of coordinate units. The coordinate system of many projections is approximately in meters while thematic maps typically range many kilometers, so by default unit="km" and unit.size=1000 (meaning 1 kilometer equals 1000 coordinate units).
line.center.type
vector of two values specifying how the center of spatial lines is determined Only applicable if shp is a SpatialLines(DataFrame), and bubbles, dots, and/or text labels are used for this shape. The two values are:
"feature", "single"
If "feature" is specified, a pair of coordinates (used for bubbles, dots, and text labels) is chosen for each feature (i.e., a row in the SpatialLines(DataFrame)). If "segment" is specified, a pair of coordinates is chosed for each line segment.

"midpoint" or "centroid"
The midpoint is the middle point on the line, so the coordinates (used for bubbles, dots, and text labels) correspond to the midpoints of the line segments. In case the first value is "feature", then per feature, the midpoint of the line segment that is closest to the centroid is taken.

...
Arguments passed on to bb. For instance, with the expension factor ext, the bounding box can be enlarges or shrinked.

Value

tmap-element

See Also

read_shape to read ESRI shape files, set_projection, vignette("tmap-nutshell")

Examples

Run this code
current.mode <- tmap_mode("plot")

data(World, metro, rivers)

tm_shape(World, projection="longlat") + 
    tm_polygons() + 
tm_layout("Long lat coordinates (WGS84)", inner.margins=c(0,0,.1,0), title.size=.8)

World$highlighted <- ifelse(World$iso_a3 %in% c("GRL", "AUS"), "gold", "gray75")
tm_shape(World, projection="merc", ylim=c(.1, 1), relative = TRUE) + 
    tm_polygons("highlighted") + 
tm_layout("Web Mercator projection. Although widely used, it is discouraged for
statistical purposes. In reality, Australia is 3 times larger than Greenland!",
    inner.margins=c(0,0,.1,0), title.size=.6)

tm_shape(World, projection="wintri") + 
    tm_polygons() +
tm_layout(
"Winkel-Tripel projection, adapted as default by the National Geographic Society for world maps.",
    inner.margins=c(0,0,.1,0), title.size=.8)

tm_shape(World) +
    tm_polygons() + 
tm_layout("Eckhart IV projection. Recommended in statistical maps for its equal-area property.",
    inner.margins=c(0,0,.1,0), title.size=.8)

# three groups of layers, each starting with tm_shape
## Not run: 
# tm_shape(World) +
#     tm_fill("darkolivegreen3") +
# tm_shape(metro) +
#     tm_bubbles("pop2010", col = "grey30", scale=.5) +
# tm_shape(rivers) +
#     tm_lines("lightcyan1") +
# tm_layout(bg.color="lightcyan1", inner.margins=c(0,0,.02,0), legend.show = FALSE)
# ## End(Not run)

# restore current mode
tmap_mode(current.mode)

Run the code above in your browser using DataLab