Skip to contents
library(geoviz)
library(sf)

world <- st_read(
  system.file("gpkg/world.gpkg", package = "geoviz"),
  quiet = TRUE
)

Geoviz is a package based on htmlwidgets. The maps are therefore SVG maps displayed in a web page, which makes them interactive. The package offers two types of interaction: tooltips and zoom.

Tooltips

To add a tooltip, you simply need to use the tip parameter and provide a character string.

viz_create() |>
viz_path(data = world, fill = "#38896F", tip = "Hello World") |>
viz_render()

In most cases, tooltips are used to display information related to the elements hovered over. To do this, use the $ prefix with the field name.

viz_create() |>
viz_path(data = world, fill = "#38896F", tip = "$name") |>
viz_render()

Setting the value to TRUE displays all available fields.

viz_create() |>
viz_path(data = world, fill = "#38896F", tip = TRUE) |>
viz_render()

You can also combine plain text with variables to compose the tooltip of your choice (line breaks are preserved).

viz_create() |>
viz_path(data = world, fill = "#38896F",
tip = "This country is $name
It is located in $region
Its population is $pop") |>
viz_render()

Zoom

The first way to make a map zoomable is to add a tile layer.

viz_create() |>
viz_tile(url = "cartodbvoyager") |>
viz_path(data = world, stroke = "#38896F", strokeWidth = 1.5, fill = "none") |>
viz_render()

If you just want to display one or more layers in the projection of your choice, you can also simply use the zoomable parameter of the viz_create function.

viz_create(zoomable = T) |>
viz_outline() |>
viz_graticule(stroke = "white") |>
viz_path(data = world, fill = "#38896F") |>
viz_render()

A second zoom mode is available. It allows not just simple panning, but also changing the projection center.

viz_create(zoomable = "versor") |>
viz_outline() |>
viz_graticule(stroke = "white") |>
viz_path(data = world, fill = "#38896F") |>
viz_render()

This mode is particularly suited for creating interactive globes.

viz_create(zoomable = "versor", projection = "orthographic") |>
viz_outline() |>
viz_graticule(stroke = "white") |>
viz_path(data = world, fill = "#38896F") |>
viz_render()