knitr::opts_chunk$set( collapse = TRUE, out.width = "100%", dpi = 300, fig.width = 7.2916667, comment = "#>" ) hook_output <- knitr::knit_hooks$get("output") knitr::knit_hooks$set(output = function(x, options) { lines <- options$output.lines if (is.null(lines)) { return(hook_output(x, options)) # pass to default hook } x <- unlist(strsplit(x, "\n")) more <- "..." if (length(lines)==1) { # first n lines if (length(x) > lines) { # truncate the output, but add .... x <- c(head(x, lines), more) } } else { x <- c(more, x[lines], more) } # paste these lines together x <- paste(c(x, ""), collapse = "\n") hook_output(x, options) })
library(tmap) tmap_options(scale = 0.8)
Each visual variable (e.g. fill
in tm_polygons()
) has an additional .chart
argument via which charts can be shown:
tm_shape(World) + tm_polygons( fill = "press", fill.scale = tm_scale_intervals(n=10, values = "scico.hawaii"), fill.legend = tm_legend("World Press\nFreedom Index"), fill.chart = tm_chart_bar()) + tm_crs("auto")
tm_shape(World) + tm_polygons("HPI", fill.scale = tm_scale_intervals(), fill.chart = tm_chart_donut())
tm_shape(World) + tm_polygons("HPI", fill.scale = tm_scale_intervals(), fill.chart = tm_chart_box())
tm_shape(World) + tm_polygons("HPI", fill.scale = tm_scale_intervals(), fill.chart = tm_chart_violin())
tm_shape(World) + tm_polygons("economy", fill.scale = tm_scale_categorical(), fill.chart = tm_chart_bar())
tm_shape(World) + tm_polygons("economy", fill.scale = tm_scale_categorical(), fill.chart = tm_chart_donut())
tm_shape(World) + tm_polygons(tm_vars(c("HPI", "well_being"), multivariate = TRUE), fill.chart = tm_chart_heatmap())
We can update the position of the chart to bottom right (in a separate frame). See vignette about positioning.
tm_shape(World) + tm_polygons( fill = "press", fill.scale = tm_scale_intervals(n=10, values = "scico.hawaii"), fill.legend = tm_legend("World Press\nFreedom Index"), fill.chart = tm_chart_bar(position = tm_pos_out("center", "bottom", pos.h = "right"))) + tm_crs("auto")
Or, in case we would like the chart to be next to the legend, but in a different frame:
tm_shape(World) + tm_polygons( fill = "press", fill.scale = tm_scale_intervals(n=10, values = "scico.hawaii"), fill.legend = tm_legend("World Press\nFreedom Index", group.frame = FALSE), fill.chart = tm_chart_bar(position = tm_pos_out("center", "bottom", align.v = "top"))) + tm_layout(component.stack_margin = .5) + tm_crs("auto")
require(ggplot2) tm_shape(World) + tm_polygons("HPI", fill.scale = tm_scale_intervals(), fill.chart = tm_chart_bar( extra.ggplot2 = theme( panel.grid.major.y = element_line(colour = "red") )) )
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.