Title: | Create Maps from a Column of Place Names |
---|---|
Description: | Mapping tools that convert place names to coordinates on the fly. These 'ggplot2' extensions make maps from a data frame where one of the columns contains place names, without having to directly work with the underlying geospatial data and tools. The corresponding map data must be registered with 'cartographer' either by the user or by another package. |
Authors: | Carl Suster [aut, cre] , Western Sydney Local Health District, NSW Health [cph] |
Maintainer: | Carl Suster <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.3.2.9000 |
Built: | 2024-12-24 05:14:46 UTC |
Source: | https://github.com/cidm-ph/ggautomap |
Mapping geometries based on {ggplot2}
for turning a spreadsheet into maps.
This package provides ggplot geoms that make maps from a data frame where
one of the columns contains place names. The map data must be registered
with {cartographer}
.
Maintainer: Carl Suster [email protected] (ORCID)
Other contributors:
Western Sydney Local Health District, NSW Health [copyright holder]
Useful links:
Report bugs at https://github.com/cidm-ph/ggautomap/issues
This specialises ggmapinset::configure_inset()
to allow the centre
to be specified as a location. The centroid of that location is used as the
inset's centre.
configure_inset( centre = NULL, scale = NULL, translation = NULL, radius = NULL, units = "km", feature_type = NA )
configure_inset( centre = NULL, scale = NULL, translation = NULL, radius = NULL, units = "km", feature_type = NA )
centre |
Coordinates of the inset centre. Can instead be the name of a
geographic feature if |
scale |
Zoom scale: values larger than one will make the circle bigger. |
translation |
Translate (shift) the inset. This can be an
|
radius |
Radius of the inset circle. |
units |
Base length unit (e.g. |
feature_type |
Type of map feature. See |
An inset configuration object.
ggmapinset::configure_inset
cfg <- configure_inset( centre = "Yancey", feature_type = "sf.nc", scale = 2, translation = c(70, -180), radius = 50, units = "mi" )
cfg <- configure_inset( centre = "Yancey", feature_type = "sf.nc", scale = 2, translation = c(70, -180), radius = 50, units = "mi" )
This allows a default inset configuration to be provided to avoid having to repeat it for each layer. Any layer that is inset-aware can use this as the default configuration if none is specifically provided to that layer. This coord also expands the axis limits to include the inset area.
coord_automap(feature_type = NA, inset = NULL, ...)
coord_automap(feature_type = NA, inset = NULL, ...)
feature_type |
Type of map feature. See |
inset |
Inset configuration; see |
... |
Arguments passed to |
A ggplot coordinate
library(ggplot2) library(cartographer) ggplot(nc_type_example_2, aes(location = county)) + geom_choropleth(aes(colour = type), size = 0.5) + geom_sf_label_inset(aes(label = county), stat = "automap_coords", size = 3) + coord_automap(feature_type = "sf.nc")
library(ggplot2) library(cartographer) ggplot(nc_type_example_2, aes(location = county)) + geom_choropleth(aes(colour = type), size = 0.5) + geom_sf_label_inset(aes(label = county), stat = "automap_coords", size = 3) + coord_automap(feature_type = "sf.nc")
crs_eqc()
gives a CRS that can be used for e.g. computing centroids or
distances.
It is an equidistant cylindrical system that by does not distort latitudes
near latitude
. The CRS is in units of kilometres by default.
crs_eqc(latitude = 0, units = "km")
crs_eqc(latitude = 0, units = "km")
latitude |
The latitude of true scale (the |
units |
Base length unit (e.g. |
CRS object from sf
.
# Sydney, Australia has a latitude of 33.87 S so this CRS will be suitable # for computations close to there: crs_eqc(latitude = -33.87)
# Sydney, Australia has a latitude of 33.87 S so this CRS will be suitable # for computations close to there: crs_eqc(latitude = -33.87)
Retrieves the full map data from {cartographer}
and plots the boundaries.
As well as the chosen feature boundaries, the outline of the map is
drawn separately if one has been registered with the map data, with the
possibility to override its aesthetics.
geom_boundaries( mapping = ggplot2::aes(), data = NULL, stat = "sf_inset", position = "identity", ..., feature_type = NULL, inset = NA, map_base = "normal", map_inset = "auto", na.rm = FALSE, outline.aes = list(colour = "#666666"), show.legend = NA, inherit.aes = FALSE )
geom_boundaries( mapping = ggplot2::aes(), data = NULL, stat = "sf_inset", position = "identity", ..., feature_type = NULL, inset = NA, map_base = "normal", map_inset = "auto", na.rm = FALSE, outline.aes = list(colour = "#666666"), show.legend = NA, inherit.aes = FALSE )
mapping , stat , position , na.rm , show.legend , inherit.aes , ...
|
See |
data |
Ignored (this geometry always uses the registered geographic data). |
feature_type |
Type of map feature. See |
inset |
Inset configuration; see |
map_base |
Controls the layer with the base map. Possible values are
|
map_inset |
Controls the layer with the inset map. Possible values are
|
outline.aes |
A list to override the aesthetics for the outline of the map. This has no effect if the map wasn't registered with a separate outline. |
A ggplot layer.
library(ggplot2) ggplot() + geom_boundaries(feature_type = "sf.nc")
library(ggplot2) ggplot() + geom_boundaries(feature_type = "sf.nc")
Assigns each point a longitude and latitude corresponding to the geographic
centre of its administrative area. This means that all points in the same
area will overlap. The default position
uses position_circle_repel()
to repel the points outwards with an amount controllable with its
scale
parameter.
geom_centroids( mapping = ggplot2::aes(), data = NULL, stat = "automap_coords", position = "circle_repel_sf", ..., fun.geometry = NULL, feature_type = NA, inset = NA, map_base = "clip", map_inset = "auto", na.rm = TRUE, show.legend = "point", inherit.aes = TRUE )
geom_centroids( mapping = ggplot2::aes(), data = NULL, stat = "automap_coords", position = "circle_repel_sf", ..., fun.geometry = NULL, feature_type = NA, inset = NA, map_base = "clip", map_inset = "auto", na.rm = TRUE, show.legend = "point", inherit.aes = TRUE )
mapping , data , stat , position , na.rm , show.legend , inherit.aes , ...
|
|
fun.geometry |
A function that takes a |
feature_type |
Type of map feature. See |
inset |
Inset configuration; see |
map_base |
Controls the layer with the base map. Possible values are
|
map_inset |
Controls the layer with the inset map. Possible values are
|
A ggplot layer.
The location
aesthetic is required.
geom_centroids()
understands the same aesthetics as ggplot2::geom_point()
.
library(ggplot2) cartographer::nc_type_example_2 |> head(n = 100) |> ggplot(aes(location = county)) + geom_boundaries(feature_type = "sf.nc") + geom_centroids(aes(colour = type), position = position_circle_repel_sf(scale = 6), size = 0.5) + coord_automap(feature_type = "sf.nc")
library(ggplot2) cartographer::nc_type_example_2 |> head(n = 100) |> ggplot(aes(location = county)) + geom_boundaries(feature_type = "sf.nc") + geom_centroids(aes(colour = type), position = position_circle_repel_sf(scale = 6), size = 0.5) + coord_automap(feature_type = "sf.nc")
Counts the number of occurrences of each location, then by default maps the
count to the fill aesthetic. If your data has only one row per location and
some other field that you'd like to map to aesthetics, use
geom_sf()
or geom_sf_inset()
with stat = "automap"
instead.
geom_choropleth( mapping = ggplot2::aes(), data = NULL, stat = "choropleth", position = "identity", ..., feature_type = NA, inset = NA, map_base = "normal", map_inset = "auto", na.rm = TRUE, show.legend = NA, inherit.aes = TRUE ) stat_choropleth( mapping = NULL, data = NULL, geom = "sf", position = "identity", ..., feature_type = NA, na.rm = TRUE, show.legend = NA, inherit.aes = TRUE )
geom_choropleth( mapping = ggplot2::aes(), data = NULL, stat = "choropleth", position = "identity", ..., feature_type = NA, inset = NA, map_base = "normal", map_inset = "auto", na.rm = TRUE, show.legend = NA, inherit.aes = TRUE ) stat_choropleth( mapping = NULL, data = NULL, geom = "sf", position = "identity", ..., feature_type = NA, na.rm = TRUE, show.legend = NA, inherit.aes = TRUE )
mapping , data , stat , geom , position , na.rm , show.legend , inherit.aes , ...
|
See |
feature_type |
Type of map feature. See |
inset |
Inset configuration; see |
map_base |
Controls the layer with the base map. Possible values are
|
map_inset |
Controls the layer with the inset map. Possible values are
|
Note that choropleths have a tendency to be misleading by emphasising geographically larger areas.
A ggplot layer.
The location
aesthetic is required.
geom_choropleth()
understands the same aesthetics as ggplot2::geom_sf()
.
rows matching the region
sf
geometry column
limits as computed by ggplot2::stat_sf()
library(ggplot2) cartographer::nc_type_example_2 |> ggplot(aes(location = county)) + geom_choropleth() + geom_boundaries(feature_type = "sf.nc") + scale_fill_steps(low = "#e6f9ff", high = "#00394d") + coord_automap(feature_type = "sf.nc")
library(ggplot2) cartographer::nc_type_example_2 |> ggplot(aes(location = county)) + geom_choropleth() + geom_boundaries(feature_type = "sf.nc") + scale_fill_steps(low = "#e6f9ff", high = "#00394d") + coord_automap(feature_type = "sf.nc")
Each row of data is drawn as a single point inside the geographic area. This has similar strengths to a standard scatter plot, but has the potential to be misleading by implying that there is significance to the exact placement of the points.
geom_geoscatter( mapping = ggplot2::aes(), data = NULL, stat = "geoscatter", position = "identity", ..., feature_type = NA, sample_type = "random", inset = NA, map_base = "clip", map_inset = "auto", na.rm = TRUE, show.legend = "point", inherit.aes = TRUE ) stat_geoscatter( mapping = NULL, data = NULL, geom = "sf_inset", position = "identity", ..., feature_type = NA, sample_type = "random", show.legend = NA, inherit.aes = TRUE )
geom_geoscatter( mapping = ggplot2::aes(), data = NULL, stat = "geoscatter", position = "identity", ..., feature_type = NA, sample_type = "random", inset = NA, map_base = "clip", map_inset = "auto", na.rm = TRUE, show.legend = "point", inherit.aes = TRUE ) stat_geoscatter( mapping = NULL, data = NULL, geom = "sf_inset", position = "identity", ..., feature_type = NA, sample_type = "random", show.legend = NA, inherit.aes = TRUE )
mapping , data , stat , geom , position , na.rm , show.legend , inherit.aes , ...
|
See |
feature_type |
Type of map feature. See |
sample_type |
sampling type (see the |
inset |
Inset configuration; see |
map_base |
Controls the layer with the base map. Possible values are
|
map_inset |
Controls the layer with the inset map. Possible values are
|
A ggplot layer.
The location
aesthetic is required.
geom_geoscatter()
understands the same aesthetics as ggplot2::geom_point()
.
longitude
latitude
library(ggplot2) cartographer::nc_type_example_2 |> ggplot(aes(location = county)) + geom_boundaries(feature_type = "sf.nc") + geom_geoscatter(aes(colour = type), size = 0.5) + coord_automap(feature_type = "sf.nc")
library(ggplot2) cartographer::nc_type_example_2 |> ggplot(aes(location = county)) + geom_boundaries(feature_type = "sf.nc") + geom_geoscatter(aes(colour = type), size = 0.5) + coord_automap(feature_type = "sf.nc")
This position looks for any points with identical x
and y
positions and packs them in a circle around the original point. The _sf
version applies the position adjustment in projected coordinates.
position_circle_repel(scale = 1/4) position_circle_repel_sf(scale = 10)
position_circle_repel(scale = 1/4) position_circle_repel_sf(scale = 10)
scale |
Scale of packing around the central point.
This is in data units, so for the |
Note that extreme choices of scale
may cause errors.
The scale
parameter can instead be specified as an aesthetic for geoms
that support it (geom_centroids()
). This allows different locations to have
different scales, which is especially useful when combined with map insets.
A ggplot position object.
library(ggplot2) points <- data.frame( x = c(rep(1, 10), 1:3), y = c(rep(2, 10), 3:5), s = 0.05 ) ggplot(points, aes(x, y)) + geom_point(size = 3, colour = "red") + geom_point(position = position_circle_repel(0.05), size = 3, alpha = 0.5) cartographer::nc_type_example_2 |> dplyr::filter(!county %in% c("HENDERSON", "GASTON", "LINCOLN")) |> ggplot(aes(location = county)) + geom_boundaries(feature_type = "sf.nc") + geom_centroids(aes(colour = type), position = position_circle_repel_sf(scale = 4), size = 0.2) + coord_automap(feature_type = "sf.nc")
library(ggplot2) points <- data.frame( x = c(rep(1, 10), 1:3), y = c(rep(2, 10), 3:5), s = 0.05 ) ggplot(points, aes(x, y)) + geom_point(size = 3, colour = "red") + geom_point(position = position_circle_repel(0.05), size = 3, alpha = 0.5) cartographer::nc_type_example_2 |> dplyr::filter(!county %in% c("HENDERSON", "GASTON", "LINCOLN")) |> ggplot(aes(location = county)) + geom_boundaries(feature_type = "sf.nc") + geom_centroids(aes(colour = type), position = position_circle_repel_sf(scale = 4), size = 0.2) + coord_automap(feature_type = "sf.nc")
Use cartographer
to attach a spatial column to the data based
on place names in another column. The result can then be used by
ggplot2::geom_sf()
or ggmapinset::geom_sf_inset()
.
stat_automap( mapping = NULL, data = NULL, geom = "sf", position = "identity", ..., feature_type = NA, na.rm = TRUE, show.legend = NA, inherit.aes = TRUE )
stat_automap( mapping = NULL, data = NULL, geom = "sf", position = "identity", ..., feature_type = NA, na.rm = TRUE, show.legend = NA, inherit.aes = TRUE )
mapping , data , geom , position , na.rm , show.legend , inherit.aes , ...
|
See |
feature_type |
Type of map feature. See |
A ggplot layer
sf
geometry column
limits as computed by ggplot2::stat_sf()
library(ggplot2) events <- data.frame( county = c("Mecklenburg", "Carteret", "Moore", "Caldwell"), proportion_A = c(0.1, 0.8, 0.0, 0.6) ) ggplot(events, aes(location = county)) + geom_sf(aes(fill = proportion_A), stat = "automap") ggplot(events, aes(location = county)) + stat_automap(aes(fill = proportion_A)) + coord_automap(feature_type = "sf.nc")
library(ggplot2) events <- data.frame( county = c("Mecklenburg", "Carteret", "Moore", "Caldwell"), proportion_A = c(0.1, 0.8, 0.0, 0.6) ) ggplot(events, aes(location = county)) + geom_sf(aes(fill = proportion_A), stat = "automap") ggplot(events, aes(location = county)) + stat_automap(aes(fill = proportion_A)) + coord_automap(feature_type = "sf.nc")
Use cartographer
to attach a spatial column to the data based
on place names in another column. The spatial data is then reduced to
coordinates in the same way as stat_sf_coordinates()
.
stat_automap_coords( mapping = NULL, data = NULL, geom = "sf_inset", position = "identity", ..., feature_type = NA, na.rm = TRUE, inset = NA, fun.geometry = NULL, show.legend = NA, inherit.aes = TRUE )
stat_automap_coords( mapping = NULL, data = NULL, geom = "sf_inset", position = "identity", ..., feature_type = NA, na.rm = TRUE, inset = NA, fun.geometry = NULL, show.legend = NA, inherit.aes = TRUE )
mapping , data , geom , position , na.rm , show.legend , inherit.aes , ...
|
|
feature_type |
Type of map feature. See |
inset |
Inset configuration; see |
fun.geometry |
A function that takes a |
A plot layer
sf
geometry column representing the points
X dimension of the simple feature
Y dimension of the simple feature
X dimension of the simple feature after inset transformation
Y dimension of the simple feature after inset transformation
logical indicating points inside the inset viewport
1 for points outside the inset, otherwise the configured inset scale parameter
ggmapinset::stat_sf_coordinates_inset()
library(ggplot2) events <- data.frame( county = c("Mecklenburg", "Carteret", "Moore", "Caldwell"), proportion_A = c(0.1, 0.8, 0.0, 0.6) ) ggplot(events, aes(location = county)) + geom_sf(aes(fill = proportion_A), stat = "automap") + geom_label(aes(label = county), stat = "automap_coords") + coord_automap(feature_type = "sf.nc")
library(ggplot2) events <- data.frame( county = c("Mecklenburg", "Carteret", "Moore", "Caldwell"), proportion_A = c(0.1, 0.8, 0.0, 0.6) ) ggplot(events, aes(location = county)) + geom_sf(aes(fill = proportion_A), stat = "automap") + geom_label(aes(label = county), stat = "automap_coords") + coord_automap(feature_type = "sf.nc")