Skip to content

Commit

Permalink
symbol shape example #706, fixed grid label bug
Browse files Browse the repository at this point in the history
  • Loading branch information
mtennekes committed Apr 25, 2024
1 parent 7a5cab5 commit ac555af
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 290 deletions.
2 changes: 1 addition & 1 deletion R/process_meta.R
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ process_meta = function(o, d, cdt, aux) {


grid.buffers = if (grid.show) {
as.integer(grid.labels.pos == c("bottom", "left", "top", "right")) * c(bufferH, bufferW, bufferH, bufferW)
as.integer(c("bottom", "left", "top", "right") %in% grid.labels.pos) * c(bufferH, bufferW, bufferH, bufferW)
} else {
rep(0, 4)
}
Expand Down
13 changes: 11 additions & 2 deletions R/tm_layers_symbols.R
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,16 @@ opt_tm_squares = opt_tm_symbols
#' scale, and therefore its own legend. For facet wraps and stacks
#' ([tm_facets_wrap()] and [tm_facets_stack()]) there is only one facet dimension,
#' so the `*.free` argument requires only one logical value.
#'
#'
#' A symbol shape specification is one of the following three options.
#' \enumerate{
#' \item{A numeric value that specifies the plotting character of the symbol. See parameter \code{pch} of \code{\link[graphics:points]{points}} and the last example to create a plot with all options. Note that this is not supported for the \code{"view" mode.}}
#' \item{A \code{\link[grid:grid.grob]{grob}} object, which can be a ggplot2 plot object created with \code{\link[ggplot2:ggplotGrob]{ggplotGrob}}. To specify multiple shapes, a list of grob objects is required. See example of a proportional symbol map with ggplot2 plots}.
#' \item{An icon specification, which can be created with \code{\link{tmap_icons}}.}
#' }
#' To specify multiple shapes (needed for the \code{shapes} argument), a vector or list of these shape specification is required. The shape specification options can also be mixed. For the \code{shapes} argument, it is possible to use a named vector or list, where the names correspond to the value of the variable specified by the \code{shape} argument.
#' For small multiples, a list of these shape specification(s) should be provided.
#'
#' @param fill,fill.scale,fill.legend,fill.chart,fill.free Visual variable that determines the fill color. See details.
#' @param col,col.scale,col.legend,col.chart,col.free Visual variable that determines the col color. See details.
#' @param size,size.scale,size.legend,size.chart,size.free Visual variable that determines the size. See details.
Expand Down Expand Up @@ -100,7 +109,7 @@ opt_tm_squares = opt_tm_symbols
#' @param id name of the data variable that specifies the indices of the spatial features.
#' Only used for `"view"` mode.
#' @param points.only should only point geometries of the shape object (defined in [tm_shape()]) be plotted? By default `"ifany"`, which means `TRUE` in case a geometry collection is specified.
#' @param icon.scale scaling number that determines how large the icons (or grobs) are in plot mode in comparison to proportional symbols (such as bubbles). In view mode, the size is determined by the icon specification (see \code{\link{tmap_icons}}) or, if grobs are specified by \code{grob.width} and \code{grob.heigth}
#' @param icon.scale scaling number that determines how large the icons (or grobs) are in plot mode in comparison to proportional symbols (such as bubbles). For view mode, use the argument `grob.dim`
#' @param just justification of the text relative to the point coordinates. Either one of the following values: \code{"left"} , \code{"right"}, \code{"center"}, \code{"bottom"}, and \code{"top"}, or a vector of two values where first value specifies horizontal and the second value vertical justification. Besides the mentioned values, also numeric values between 0 and 1 can be used. 0 means left justification for the first value and bottom justification for the second value. Note that in view mode, only one value is used.
#' @param grob.dim vector of four values that determine how grob objects (see details) are shown in view mode. The first and second value are the width and height of the displayed icon. The third and fourth value are the width and height of the rendered png image that is used for the icon. Generally, the third and fourth value should be large enough to render a ggplot2 graphic successfully. Only needed for the view mode.
#' @param ... to catch deprecated arguments from version < 4.0
Expand Down
4 changes: 2 additions & 2 deletions R/tmapGridAux.R
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ tmapGridGridXLab = function(bi, bbx, facet_row, facet_col, facet_page, o) {

rc_text = frc(facet_row, facet_col)

rowid = g$rows_facet_ids[facet_row] + ifelse(is_top, -2, 2)
rowid = g$rows_facet_ids[facet_row] + ifelse(is_top, -3, 3)
colid = g$cols_facet_ids[facet_col]

H = g$rowsIn[rowid]
Expand Down Expand Up @@ -461,7 +461,7 @@ tmapGridGridYLab = function(bi, bbx, facet_row, facet_col, facet_page, o) {
rc_text = frc(facet_row, facet_col)

rowid = g$rows_facet_ids[facet_row]
colid = g$cols_facet_ids[facet_col] + ifelse(is_left, -2, 2)
colid = g$cols_facet_ids[facet_col] + ifelse(is_left, -3, 3)

H = g$rowsIn[rowid]
W = g$colsIn[colid]
Expand Down
7 changes: 6 additions & 1 deletion R/tmapLeaflet_layers.R
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ tmapLeafletSymbols = function(shpTM, dt, pdt, popup.format, hdt, idt, gp, bbx, f
symbols_icons <- merge_icons(iconLib)
size = gp2$width[sid] / gp2$baseSize

size[sid] = size[sid] * args$icon.scale
#size[sid] = size[sid] * args$icon.scale # icon.scale is set to 1 in view mode

for (i in seq_along(sid)) {
symbols$iconUrl[sid[i]] = symbols_icons$iconUrl[i]
Expand Down Expand Up @@ -328,6 +328,11 @@ tmapLeafletText = function(shpTM, dt, gp, bbx, facet_row, facet_col, facet_page,
clustering = leaflet::markerClusterOptions()
}

# apply xmod and ymod
delta = delta_per_lineheight(bbx)

coords[,1] = coords[,1] + delta * gp$cex * gp$xmod
coords[,2] = coords[,2] + delta * gp$cex * gp$ymod



Expand Down
5 changes: 3 additions & 2 deletions R/tmapScaleCategorical.R
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,9 @@ tmapScaleCategorical = function(x1, scale, legend, chart, o, aes, layer, layer_a
}
attr(labs, "align") = label.format$text.align

# special case: if icons are used, specify this information in the symbol legend, such that it can be taken (in step4_plot_collect_legends) by other legends (e.g. for symbol sizes)
icon_scale = if ((aes == "shape") && any(values > 999)) layer_args$icon.scale else 1

# SPECIAL CASE: if icons are used, specify this information in the symbol legend, such that it can be taken (in step4_plot_collect_legends) by other legends (e.g. for symbol sizes)
icon_scale = if ((aes == "shape") && any(values > 999) && getOption("tmap.mode") == "plot") layer_args$icon.scale else 1

legend = within(legend, {
nitems = length(labs)
Expand Down
160 changes: 18 additions & 142 deletions examples/tm_symbols.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,145 +26,21 @@ tm_shape(land) +
shape.legend = tm_legend_combine("size")) +
tm_labels("name", options = opt_tm_labels(remove.overlap = FALSE))


##### tmap v3

data(World, metro)
metro$growth <- (metro$pop2020 - metro$pop2010) / (metro$pop2010 * 10) * 100

tm_shape(World) +
tm_fill("grey70") +
tm_shape(metro) +
tm_bubbles("pop2010", col = "growth",
border.col = "black", border.alpha = .5,
style="fixed", breaks=c(-Inf, seq(0, 6, by=2), Inf),
palette="-RdYlBu", contrast=1,
title.size="Metro population",
title.col="Growth rate (%)") +
tm_format("World")

tm_shape(metro) +
tm_symbols(size = "pop2010", col="pop2010", shape="pop2010",
legend.format = list(text.align="right", text.to.columns = TRUE)) +
tm_legend(outside = TRUE, outside.position = "bottom", stack = "horizontal")


if (require(ggplot2) && require(dplyr) && require(tidyr) && require(tmaptools) && require(sf)) {
data(NLD_prov)

origin_data <- NLD_prov %>%
st_set_geometry(NULL) %>%
dplyr::mutate(FID = factor(dplyr::row_number())) %>%
dplyr::select(FID, origin_native, origin_west, origin_non_west) %>%
tidyr::pivot_longer(
cols = c(origin_native, origin_west, origin_non_west),
names_to = "origin",
values_to = "perc",
names_transform = as.factor
) %>%
dplyr::arrange(origin, FID) %>%
as.data.frame()

origin_cols <- get_brewer_pal("Dark2", 3)

grobs <- lapply(split(origin_data, origin_data$FID), function(x) {
ggplotGrob(ggplot(x, aes(x="", y=-perc, fill=origin)) +
geom_bar(width=1, stat="identity") +
scale_y_continuous(expand=c(0,0)) +
scale_fill_manual(values=origin_cols) +
theme_ps(plot.axes = FALSE))
})

names(grobs) <- NLD_prov$name

tm_shape(NLD_prov) +
tm_polygons(group = "Provinces") +
tm_symbols(size = "population", shape="name",
shapes = grobs,
sizes.legend = c(.5, 1,3)*1e6,
scale = 1,
legend.shape.show = FALSE,
legend.size.is.portrait = TRUE,
shapes.legend = 22,
title.size = "Population",
group = "Charts",
id = "name",
popup.vars = c("population", "origin_native",
"origin_west", "origin_non_west")) +
tm_add_legend(type = "fill",
group = "Charts",
col = origin_cols,
labels = c("Native", "Western", "Non-western"),
title = "Origin") +
tm_format("NLD")

grobs2 = grobs
grobs2[[6]] = 21
names(grobs2) <- as.character(NLD_prov$name)
NLD_prov$population[1:5] = 500000
tm_shape(NLD_prov) +
tm_polygons(group = "Provinces") +
tm_symbols(shape="name",
fill = "red",
col = "blue",
size = "population",
size.scale = tm_scale_continuous(values.scale = 4),
shape.scale = tm_scale_categorical(values = grobs2),
shape.legend = tm_legend_hide())

}



# TIP: check out these examples in view mode, enabled with tmap_mode("view")

\dontrun{
if (require(rnaturalearth)) {

airports <- ne_download(scale=10, type="airports", returnclass = "sf")
airplane <- tmap_icons(system.file("img/airplane.png", package = "tmap"))

current.mode <- tmap_mode("view")
tm_shape(airports) +
tm_symbols(shape=airplane, size="natlscale",
legend.size.show = FALSE, scale=1,
border.col = NA, id="name", popup.vars = TRUE)
#+ tm_view(set.view = c(lon = 15, lat = 48, zoom = 4))
tmap_mode(current.mode)
}
}

#####################################################################################

\dontrun{
# plot all available symbol shapes:
if (require(ggplot2)) {
ggplot(data.frame(p=c(0:25,32:127))) +
geom_point(aes(x=p%%16, y=-(p%/%16), shape=p), size=5, fill="red") +
geom_text(mapping=aes(x=p%%16, y=-(p%/%16+0.25), label=p), size=3) +
scale_shape_identity() +
theme(axis.title = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank(),
panel.background = element_blank())
}
}


tm_shape(metro) +
tm_symbols("pop2010", size.scale = tm_scale_continuous(n = 8, values.scale = 3), shape = 22)



data("NLD_prov")
NLD_prov$x = seq(10, by = 5, length.out = 12)

tm_shape(NLD_prov) +
tm_symbols(size = "x", size.scale = tm_scale_continuous(values.scale = 4)) +
tm_text(text = "x")

tm_shape(metro) +
tm_symbols("pop2010", fill = "pop2020",
fill.scale = tm_scale_continuous(),
size.scale = tm_scale_continuous(), shape = 22)

########################
## plot symbol shapes
########################

# create grid of 25 points in the Athlantic
library(sf)
x = st_as_sf(cbind(expand.grid(x = -51:-47, y = 20:24), id = seq_len(25)), coords = c("x", "y"), crs = 4326)

tm_shape(x, bbox = bb(x, ext = 1.1)) +
tm_symbols(shape = "id",
size = 2,
lwd = 2,
fill = "orange",
col = "black",
shape.scale = tm_scale_asis()) +
tm_text("id", ymod = -2)

# also supported in view mode :-)
Loading

0 comments on commit ac555af

Please sign in to comment.