| Title: | Export 'ggplot2' as 'QGIS' Project |
|---|---|
| Description: | Convert a 'ggplot2'-based map plot (e.g. 'geom_sf()') into a 'QGIS' project ('.qgs') file. |
| Authors: | Hiroaki Yutani [aut, cre] (ORCID: <https://orcid.org/0000-0002-3385-7233>) |
| Maintainer: | Hiroaki Yutani <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.0.0.9000 |
| Built: | 2026-07-21 12:19:38 UTC |
| Source: | https://github.com/yutannihilation/ggplot2qgis |
Converts a ggplot2 plot whose layers are drawn from sf objects (or, for
ggplot2::geom_point(), ggplot2::geom_path(), ggplot2::geom_line()
and ggplot2::geom_polygon(), from plain data frames) into a QGIS
project (.qgs) file. The data of each layer is saved as a GeoPackage
under <path minus extension>_data/, and the layer is styled after the
plot's trained color scale:
write_qgs( plot, path, use_plot_crs = FALSE, gradient_style = c("graduated", "continuous"), overwrite = FALSE, layer_names = NULL, basemap = NULL )write_qgs( plot, path, use_plot_crs = FALSE, gradient_style = c("graduated", "continuous"), overwrite = FALSE, layer_names = NULL, basemap = NULL )
plot |
A ggplot object. Each layer must be backed by sf data, or be one of the supported data.frame geoms (see Data frame layers). |
path |
Path of the |
use_plot_crs |
If |
gradient_style |
How a continuous
Binned scales are unaffected: their bins are exact in a graduated
renderer, so there is nothing to trade off. Requesting |
overwrite |
If |
layer_names |
Names for the layers, used for the GeoPackage files
and in the QGIS layer tree: a character vector with one name per
layer, bottom-most first.
with a numbered suffix ( |
basemap |
An XYZ tile layer to add below the vector layers, or
XYZ tiles are in EPSG:3857; QGIS reprojects them to the project CRS on the fly. |
a continuous fill/colour scale becomes a graduated renderer with
fine-grained equal-interval classes (or a continuously interpolated
color, see gradient_style),
a binned one (e.g. ggplot2::scale_fill_steps()) becomes a graduated
renderer with one class per bin, using the scale's exact bin
boundaries and colors,
a discrete one becomes a categorized renderer,
a layer with no fill/colour mapping becomes a single symbol with
the color ggplot2 would have used.
Following ggplot2's semantics for polygons, fill is the interior and
colour is the border: a colour scale on a polygon layer colors the
outlines while the interior keeps the constant fill. Constant outline
colors and widths are taken from the plot as well. Mapping both fill
and colour on the same layer is not supported.
Only a bare column name is supported for the fill/colour aesthetics;
a constant or a computed expression (e.g. aes(fill = AREA * 2)) is an
error.
path, invisibly.
A geom_point(), geom_path(), geom_line() or geom_polygon() layer
drawn from a plain data frame is converted to an sf layer: one point per
row, or one linestring/polygon per group (ggplot2's grouping — an
explicit group aesthetic or the interaction of the discrete
aesthetics; geom_line() orders each line by x like ggplot2 does,
and polygon rings are closed). The plot must use ggplot2::coord_sf(),
and the x/y values are taken to be coordinates in the panel CRS:
coord_sf()'s crs argument if given, otherwise the CRS of the first
sf layer (coord_sf(default_crs = ) is not supported). Like
fill/colour, the x/y aesthetics must be bare column names, and
the layer must use the identity stat and position.
A point layer keeps every column of the data frame as attributes; a
line/polygon layer keeps the columns that are constant within every
group, one feature per group (a mapped fill/colour column must be
constant within each group). fill = NA/colour = NA (including
geom_polygon()'s default colour) render as "not drawn" in QGIS.
The size, shape, linetype and alpha aesthetics are not carried
over; those symbol properties keep the QGIS defaults.
The project opens zoomed to the plot's displayed range (the panel range,
including the default expansion and any ggplot2::coord_sf() xlim/
ylim), reprojected to the project CRS, rather than the whole world.
library(ggplot2) nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE) p <- ggplot(nc) + geom_sf(aes(fill = AREA)) write_qgs(p, tempfile(fileext = ".qgs"))library(ggplot2) nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE) p <- ggplot(nc) + geom_sf(aes(fill = AREA)) write_qgs(p, tempfile(fileext = ".qgs"))