library(geoviz)
library(sf)
world <- st_read(
system.file("gpkg/world.gpkg", package = "geoviz"),
quiet = TRUE
)The geoviz library offers numerous functions that allow you to display specific layers and overlay them to create the map of your choice. This gives you plenty of options for laying out your map.
Base Map and Structure
Outline
The viz_outline function allows to create a layer with
Earth outline in the projection. You can customize the display using SVG
parameters such as fill, stroke,
strokeWidth, opacity, fillOpacity
and many others.
viz_create(projection = "EqualEarth") |>
viz_outline(fill = "#38896F") |>
viz_path(data = world, fill = "white",
fillOpacity = 0.2, stroke = "none") |>
viz_render()Graticule
The viz_graticule function allows function allows to
create a layer with lat/long lines. You can customize the display using
SVG parameters such as step, stroke,
strokeWidth, strokeDasharray, and many
others.
viz_create(projection = "EqualEarth") |>
viz_path(data = world, fill = "#38896F",
fillOpacity = 0.2, stroke = "none") |>
viz_graticule(step = 20, stroke = "#38896F",
strokeDasharray = c(5,10)) |>
viz_render()Earth
The viz_earth function displays PNG files representing
the Earth’s surface. The data comes from Natural Earth. Credit as
follows: Natural Earth. Free vector and raster map data @
naturalearthdata.com. Unlike tiles, you can choose whichever projection
you want. the url parameter allows to change the image to
display. You can choose one from this list: "GRAY_50M_SR",
"GRAY_50M_SR_OB", "GRAY_50M_SR_W",
"HYP_50M_SR", "HYP_50M_SR_W",
"MSR_50M", "NE1_50M_SR_W",
"NE2_50M_SR", "NE2_50M_SR_W",
"OB_50M", "PRIMSA_SR_50M",
"SR_50M".
viz_create(projection = "Polar") |>
viz_earth(url = "HYP_50M_SR_W") |>
viz_render()Tile
The viz_tile function allows function allows to add
zoomable raster tiles. When you use this mark, the Mercator projection
is automaticaly used. With the parmeter url, you can choose
the style of the tiles. Try "openstreetmap",
"opentopomap", "worldterrain",
"worldimagery", "worldStreet",
"worldphysical", "shadedrelief",
"stamenterrain", "cartodbvoyager",
"stamentoner" , "stamentonerbackground" ,
"stamentonerlite", "stamenwatercolor",
"hillshade", "worldocean",
"natgeo" or "worldterrain".
viz_create() |>
viz_tile(url = "worldimagery") |>
viz_render()Map Decorations and Annotations
Labels
The viz_text function allows to add a text on the map.
It allow also to create a layer with labels from a spatial
dataframe.
africa <- world[world$region == "Africa",]
viz_create(projection = "Mercator", domain = africa) |>
viz_path(datum = africa, fill = "#CCC", fillOpacity = 0.5) |>
viz_text(data = africa, text = "name", fill = "#38896F",
stroke = "white", strokeWidth = 4) |>
viz_render()Header
The viz_header function allows function allows to add a
title above the map. You can customize the display using parameters such
as text, fill, fill_background,
textAnchor, dominantBaseline and many
others.
viz_create(projection = "EqualEarth") |>
viz_outline(fill = "#CCCCCC") |>
viz_path(data = world, fill = "white",
fillOpacity = 0.2, stroke = "none") |>
viz_header(text = "Hello World", fill = "#38896F") |>
viz_render()Footer
The viz_footer function allows function allows to add a
text below the map. You can customize the display using parameters such
as text, fill, fill_background,
textAnchor, dominantBaseline and many
others.
viz_create(projection = "EqualEarth") |>
viz_outline(fill = "#CCCCCC") |>
viz_path(data = world, fill = "white", fillOpacity = 0.2,
stroke = "none") |>
viz_footer(text = "Author, source, date...",
fill = "#38896F") |>
viz_render()North
The viz_north function adds a north arrow. By default,
the arrow indicates true north at the location where it is placed.
Depending on the map projection, the arrow may therefore not point in
the same direction everywhere on the map. The pos parameter
allows you to position the arrow using the SVG document coordinates (0,0
is at the top-left corner).
africa <- world[world$region == "Africa",]
viz_create(projection = "Mercator", domain = africa) |>
viz_path(datum = africa, fill = "#CCC", fillOpacity = 0.5) |>
viz_north(pos = c(50,50), fill = "#38896F") |>
viz_render()Scalebar
The viz_scalebar function allows add a scalebar. The
pos parameter allows you to position the scalebar.
viz_create(projection = "Mercator", domain = africa) |>
viz_path(datum = africa, fill = "#CCC", fillOpacity = 0.5) |>
viz_scalebar() |>
viz_render()Location map
The viz_minimap function allows to display a location
map.
viz_create(projection = "Mercator", domain = africa, zoomable = TRUE) |>
viz_path(datum = world, fill = "#CCC", fillOpacity = 0.5) |>
viz_minimap(projection = "Polar", location_stroke = "#38896F",
location_fill = "#38896F", location_fillOpacity = 0.3,
basemap_fill="#CCC", basemap_fillOpacity = 1,
outline_fill = "white", outline_stroke = "#303030") |>
viz_render()Rhumbs
The viz_rhumbs function allows to display “rhumb lines”
like on old portolan charts
viz_create(projection = "Mercator", domain = africa) |>
viz_path(datum = africa, fill = "#CCC", fillOpacity = 0.5) |>
viz_rhumbs(pos = c(20,20), stroke = "#38896F", nb = 32) |>
viz_render()Pattern
The viz_pattern function allows to display a pattern on
the map
viz_create(projection = "Equal Earth") |>
viz_outline(fill = "white") |>
viz_pattern(clipOutline = TRUE, stroke = "#38896F", pattern = "waves") |>
viz_path(datum = world, fill = "#CCC") |>
viz_render()Effects
The geoviz package makes it possible to create
publication-quality maps. Since these maps are built in SVG format, SVG
filters can be defined. This works in two steps. First, you define an
effect by giving it an identifier. Then, you apply it to the layer of
your choice.
aus <- world[world$ISO3 == "AUS",]blur
The viz_blur function is used to define a blur effect
The id parameter defines the filter identifier. The
stdDeviation parameter determines the blur range.
# A simple map
viz_create() |>
# 1 - create filter
viz_blur(id = "my_blur_effect", stdDeviation = 5) |>
# 2 - add path and apply filter
viz_path(datum = aus, fill = "#38896F",
filter = "url(#my_blur_effect)") |>
viz_render()shadow
The viz_shadow function can be used to draw a drop
shadow
viz_create(margin = 5) |>
viz_shadow(id = "my_shadow_effect", stdDeviation = 2.5,
dx = 5, dy = 5) |>
viz_path(datum = aus, fill = "#38896F",
filter = "url(#my_shadow_effect)") |>
viz_render()radialGradient
With viz_radialGradient, you can create a radial
gradient. In addition to the filter id, the function takes several
parameters as input. color1 and color2 define
the gradient colors. The parameters offset1,
offset2, fx and fy define how the
gradation is performed. Then, you have to apply it on the
fill attribute.
viz_create(margin = 5) |>
viz_radialGradient(id = "my_radial_gradient",
color1 = "#7db054", color2 = "#38896f",
offset1 = 50, offset2 = 100, fx = 50, fy = 50) |>
viz_path(datum = aus, fill = "url(#my_radial_gradient)") |>
viz_render()clipPath
The viz_clipPath() function.
viz_create() |>
viz_clipPath(id = "my_clip_path", datum = aus )|>
viz_tile(url = "worldimagery", clipPath = "url(#my_clip_path)") |>
viz_render()pattern
The viz_pattern function (which we have already
discussed) allows you to create patterns and apply them to any geometry.
It supports multiple textures: "lines",
"crosshatch", "dots", "waves",
"triangles", and "zigzag".
viz_create() |>
viz_pattern(data = aus, pattern = "cross",
stroke = "#38896F", strokeOpacity = 2,
angle = 45) |>
viz_render()sketch
The viz_sketch function generates a hand-drawn (sketchy)
SVG representation of a spatial dataframe. It applies SVG filters
(feTurbulence and feDisplacementMap) to create a
pencil-like effect.
viz_create() |>
viz_sketch(data = aus, stroke = "#38896F") |>
viz_render()Design nice maps
With all these highly customizable layers, it is possible to create very nice maps with geoviz. Here is an example.
viz_create(projection = "mercator", domain = africa,
margin = 20, zoomable = FALSE) |>
viz_clipPath(id = "africaclip", datum = africa )|>
viz_shadow(id = "africashadow", stdDeviation = 4,
dx = 4, dy = 4, opacity = 0.3) |>
viz_outline()|>
viz_path(datum = world, fill = "white", fillOpacity = 0.3) |>
viz_path(data = africa, filter = "url(#africashadow)") |>
viz_rhumbs(pos = c(0,-10), coords = "geo", stroke = "white",
strokeWidth = 2, strokeDasharray = c(5,3)) |>
viz_path(data = africa, fill = "#d6bc47", fillOpacity = 1) |>
viz_tile(url = "hillshade", clipPath = "url(#africaclip)",
opacity = 0.3) |>
viz_sketch(data = africa, simplify = 1, strokeOpacity = 0.6) |>
viz_header(text = "The African continent", fill = "white",
background_fill = "black") |>
viz_pattern(stroke = "white", angle = 45) |>
viz_scalebar()|>
viz_north()|>
viz_render()