Skip to content

Commit

Permalink
added opt_tm_sf,improved tm_polygons examples
Browse files Browse the repository at this point in the history
  • Loading branch information
mtennekes committed Mar 25, 2024
1 parent 77529b8 commit 37e968d
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 43 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ export(opt_tm_labels)
export(opt_tm_lines)
export(opt_tm_polygons)
export(opt_tm_raster)
export(opt_tm_sf)
export(opt_tm_sqaures)
export(opt_tm_symbols)
export(opt_tm_text)
Expand Down
51 changes: 31 additions & 20 deletions R/tm_layers_sf.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,27 @@
#' @rdname tm_sf
#' @name opt_tm_sf
#' @export
opt_tm_sf = function(polygons.only = "yes",
lines.only = "yes",
points.only = "yes",
points.icon.scale = 3,
points.just = NA,
points.grob.dim = c(width = 48, height = 48, render.width = 256, render.height = 256)) {

list(points = list(trans.args = list(points.only = points.only),
mapping.args = list(icon.scale = points.icon.scale,
just = points.just,
grob.dim = points.grob.dim)),
lines = list(trans.args = list(lines.only = lines.only),
mapping.args = list()),
polygons = list(trans.args = list(polygons.only = polygons.only),
mapping.args = list()))

}




#' Map layer: simple features
#'
#' Map layer that draws simple features as they are. Supported visual variables
Expand Down Expand Up @@ -49,9 +73,8 @@
#' @param plot.order.list Specification in which order the spatial features are drawn.
#' This consists of a list of three elementary geometry types: for polygons, lines and, points.
#' For each of these types, which are drawn in that order, a [tm_plot_order()] is required.
#' @param trans.args.list,mapping.args.list lists that are passed on to internal
#' transformation and mapping functions respectively. Each is a list of three
#' items, named `polygons`, `lines`, and `points`. See [tm_polygons()], [tm_lines()], and [tm_dots()].
#' @param polygons.only,lines.only,points.only,points.icon.scale,points.just,points.grob.dim See [opt_tm_polygons()], [opt_tm_lines()] and [opt_tm_symbols()]
#' @param options options passed on to the corresponding `opt_<layer_function>` function
#' @param zindex Map layers are drawn on top of each other.
#' The `zindex` numbers (one for each map layer) determines the stacking order.
#' By default the map layers are drawn in the order they are called.
Expand Down Expand Up @@ -103,23 +126,14 @@ tm_sf = function(fill = tm_const(),
plot.order.list = list(
polygons = tm_plot_order("AREA"), lines = tm_plot_order("LENGTH"), points = tm_plot_order("size")
),
trans.args.list = list(
polygons = list(polygons.only = "yes"), lines = list(lines.only = "yes"), points = list(points.only = "yes")
),
mapping.args.list = list(
polygons = list(),
lines = list(),
points = list(icon.scale = 3, just = NA, grob.dim = c(width = 48, height = 48, render.width = 256, render.height = 256))
),
options = opt_tm_sf(),
zindex = NA,
group = NA,
group.control = "check",
...) {
args = list(...)
calls = names(match.call(expand.dots = TRUE)[-1])



#paste(paste(names(formals(tm_polygons)), names(formals(tm_polygons)), sep = " = "), collapse = ", ")
tm = tm_polygons(
fill = fill, fill.scale = fill.scale, fill.legend = fill.legend,
Expand All @@ -131,16 +145,15 @@ tm_sf = function(fill = tm_const(),
col_alpha = col_alpha, col_alpha.scale = col_alpha.scale,
col_alpha.legend = col_alpha.legend, col_alpha.free = col_alpha.free,
linejoin = linejoin, lineend = lineend, plot.order = plot.order.list$polygons,
trans.args = trans.args.list$polygons, mapping.args = mapping.args.list$polygons) +
options = options$polygons) +
tm_lines(
col = col, col.scale = col.scale, col.legend = col.legend,
col.free = col.free, lwd = lwd, lwd.scale = lwd.scale,
lwd.legend = lwd.legend, lwd.free = lwd.free, lty = lty, lty.scale = lty.scale,
lty.legend = lty.legend, lty.free = lty.free, col_alpha = col_alpha,
col_alpha.scale = col_alpha.scale, col_alpha.legend = col_alpha.legend,
col_alpha.free = col_alpha.free, linejoin = linejoin, lineend = lineend,
plot.order = plot.order.list$lines, trans.args = trans.args.list$lines,
mapping.args = mapping.args.list$lines)
plot.order = plot.order.list$lines, options = options$lines)


if ("shape" %in% calls) {
Expand All @@ -155,8 +168,7 @@ tm_sf = function(fill = tm_const(),
fill_alpha.legend = fill_alpha.legend, fill_alpha.free = fill_alpha.free,
col_alpha = col_alpha, col_alpha.scale = col_alpha.scale,
col_alpha.legend = col_alpha.legend, col_alpha.free = col_alpha.free,
plot.order = plot.order.list$points, trans.args = trans.args.list$points,
mapping.args = mapping.args.list$points)
plot.order = plot.order.list$points, options = options$points)
} else {
tm + tm_dots(fill = fill, fill.scale = fill.scale, fill.legend = fill.legend,
fill.free = fill.free, size = size, size.scale = size.scale,
Expand All @@ -165,8 +177,7 @@ tm_sf = function(fill = tm_const(),
lty = lty, lty.scale = lty.scale, lty.legend = lty.legend, lty.free = lty.free,
fill_alpha = fill_alpha, fill_alpha.scale = fill_alpha.scale,
fill_alpha.legend = fill_alpha.legend, fill_alpha.free = fill_alpha.free,
plot.order = plot.order.list$points, trans.args = trans.args.list$points,
mapping.args = mapping.args.list$points)
plot.order = plot.order.list$points, options = options$points)
}

}
15 changes: 9 additions & 6 deletions examples/tm_polygons.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,27 @@ tm_shape(Africa_border) + tm_borders()

# specification with visual variable values
tm_shape(Africa) +
tm_polygons(fill = "limegreen", col = "purple", lwd = 3, lty = "solid", col_alpha = 0.3) +
tm_polygons(fill = "limegreen", col = "purple", lwd = 2, lty = "solid", col_alpha = 0.3) +
tm_text("name", options = opt_tm_text(remove.overlap = TRUE)) +
tm_shape(Africa_border) +
tm_borders("darkred", lwd = 4)
tm_borders("darkred", lwd = 3)

# specification with a data variable
tm_shape(Africa) +
tm_polygons(fill = "income_grp", fill.scale = tm_scale_categorical(values = "tol.muted"))
tm_polygons(fill = "income_grp", fill.scale = tm_scale_categorical(values = "-tol.muted"))

# continuous color scale with landscape legend
tm_shape(Africa) +
tm_polygons(fill = "inequality",
fill.scale = tm_scale_continuous(values = "kovesi.rainbow_bu_pk"),
fill.scale = tm_scale_continuous(values = "-scico.roma"),
fill.legend = tm_legend(
title = "", orientation = "landscape",
position = tm_pos_out("center", "bottom"), frame = FALSE
)) +
tm_title("Inequality index", position = tm_pos_in("right", "TOP"), frame = FALSE) +
tm_layout(frame = FALSE)
tm_shape(Africa_border) +
tm_borders(lwd = 2) +
tm_title("Inequality index", position = tm_pos_in("right", "TOP"), frame = FALSE) +
tm_layout(frame = FALSE)

# bivariate scale
tm_shape(World) +
Expand Down
2 changes: 1 addition & 1 deletion examples/tm_sf.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ World$geometry[World$continent == "South America"] <-
sf::st_cast(World$geometry[World$continent == "South America"],
"MULTILINESTRING", group_or_split = FALSE)

tm_shape(World) +
tm_shape(World, crs = "+proj=robin") +
tm_sf()
15 changes: 9 additions & 6 deletions man/tm_polygons.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 16 additions & 10 deletions man/tm_sf.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 37e968d

Please sign in to comment.