### Install ggplot2qgis from R-universe
Source: https://github.com/yutannihilation/ggplot2qgis/blob/main/README.md
Use this command to install the package from the official R-universe repository.
```r
install.packages("ggplot2qgis", repos = c("https://yutannihilation.r-universe.dev", "https://cloud.r-project.org"))
```
--------------------------------
### Define Paletted Raster Renderer Palette Entries
Source: https://github.com/yutannihilation/ggplot2qgis/blob/main/docs/qgs-format.md
Example of the colorPalette structure used for unique value rendering in QGIS raster layers.
```xml
```
--------------------------------
### Exporting tmap objects to QGIS
Source: https://github.com/yutannihilation/ggplot2qgis/blob/main/man/write_qgs.Rd
Use this snippet to save a tmap object as a QGIS project file. Requires the tmap package to be installed and loaded.
```R
if (requireNamespace("tmap", quietly = TRUE)) {
x <- tmap::tm_shape(nc) + tmap::tm_polygons(fill = "AREA")
write_qgs(x, tempfile(fileext = ".qgs"))
}
```
--------------------------------
### Render a QGIS project to PNG
Source: https://github.com/yutannihilation/ggplot2qgis/blob/main/tools/qgis/README.md
Executes the rendering script to convert a .qgs project file into a PNG image.
```sh
tools/qgis/render_project.py project.qgs out.png
```
--------------------------------
### Structure of a .qgs XML file
Source: https://github.com/yutannihilation/ggplot2qgis/blob/main/docs/qgs-format.md
The root element contains project metadata, layer tree definitions, and individual map layer configurations.
```xml
... project-wide settings ...
... layer tree entries ...
...
...
...
...
... more project-wide settings ...
```
--------------------------------
### Run script with QGIS environment
Source: https://github.com/yutannihilation/ggplot2qgis/blob/main/tools/qgis/README.md
Uses the qgis-python wrapper to execute a script within the QGIS Python environment.
```sh
tools/qgis/qgis-python probe.py project.qgs
```
--------------------------------
### Generate marker shapes reference
Source: https://github.com/yutannihilation/ggplot2qgis/blob/main/tools/qgis/README.md
Draws QGIS SimpleMarker shapes to a PNG file for reference and measurement.
```sh
tools/qgis/marker_shapes.py --png shapes.png
```
--------------------------------
### Compare R plot with QGIS rendering
Source: https://github.com/yutannihilation/ggplot2qgis/blob/main/tools/qgis/README.md
Demonstrates the workflow for comparing an R-generated plot with a QGIS-rendered project file.
```sh
# The R side: write the project and render the same plot to a PNG.
Rscript -e 'write_qgs(map, "map.qgs"); png("r.png", 1200, 800, res = 96); print(map); dev.off()'
# The QGIS side, at the same size and resolution.
tools/qgis/render_project.py map.qgs qgis.png --size 1200x800 --dpi 96
```
--------------------------------
### write_qgs(x, filename)
Source: https://github.com/yutannihilation/ggplot2qgis/blob/main/man/write_qgs.Rd
Writes a spatial object to a QGIS project file.
```APIDOC
## write_qgs
### Description
Exports a spatial object to a QGIS project file (.qgs).
### Parameters
- **x** (object) - The spatial object to be exported (e.g., a tmap object).
- **filename** (string) - The file path where the .qgs file should be saved.
### Example
```r
if (requireNamespace("tmap", quietly = TRUE)) {
x <- tmap::tm_shape(nc) + tmap::tm_polygons(fill = "AREA")
write_qgs(x, tempfile(fileext = ".qgs"))
}
```
```
--------------------------------
### Adding a basemap to QGIS export
Source: https://github.com/yutannihilation/ggplot2qgis/blob/main/README.md
Include a basemap in the exported QGIS project by specifying a key or URL template in the write_qgs function.
```r
write_qgs(p, "nc.qgs", basemap = "osm")
```
--------------------------------
### Measure rendered features
Source: https://github.com/yutannihilation/ggplot2qgis/blob/main/tools/qgis/README.md
Renders a specific extent and measures the output dimensions in pixels and millimeters.
```sh
tools/qgis/render_project.py one.qgs one.png \
--size 800x800 --extent=-80,34,-78,36 --measure
#> drawn: 22x22 px (5.82x5.82 mm at 96 dpi)
```
--------------------------------
### write_qgs(plot, path, ...)
Source: https://github.com/yutannihilation/ggplot2qgis/blob/main/man/write_qgs.Rd
Converts a ggplot2 plot into a QGIS project file. The data for each layer is saved as a GeoPackage or GeoTIFF, and the layer is styled based on the plot's color scales and aesthetics.
```APIDOC
## write_qgs
### Description
Converts a ggplot2 plot into a QGIS project (.qgs) file. The function maps ggplot2 layers (sf objects, SpatRaster) to QGIS layers and translates aesthetics into QGIS renderers.
### Parameters
- **plot** (ggplot) - The ggplot2 object to convert.
- **path** (character) - The file path for the output .qgs file.
- **...** (dots) - Additional arguments passed to methods.
### Returns
- **path** (character) - The path to the created .qgs file, returned invisibly.
```
--------------------------------
### write_qgs
Source: https://github.com/yutannihilation/ggplot2qgis/blob/main/man/write_qgs.Rd
Exports a ggplot2 or tmap object to a QGIS project file.
```APIDOC
## write_qgs(plot, path, ...)
### Description
Writes a ggplot2 or tmap object as a QGIS project file. This function handles the conversion of map layers and styling into a format compatible with QGIS.
### Parameters
- **plot** (object) - Required - A ggplot2 or tmap object.
- **path** (string) - Required - The file path where the .qgs file will be saved.
- **use_plot_crs** (boolean) - Optional - Whether to use the plot's CRS (default: TRUE for tmap, FALSE for ggplot).
- **gradient_style** (string) - Optional - How to render continuous scales: 'graduated' or 'continuous'.
- **overwrite** (boolean) - Optional - Whether to overwrite an existing file (default: FALSE).
- **layer_names** (character vector) - Optional - Custom names for the layers.
- **basemap** (string) - Optional - An XYZ tile layer URL or predefined key (e.g., 'osm').
- **create_na_layer** (boolean) - Optional - Whether to create an NA layer (tmap specific, default: TRUE).
```
--------------------------------
### Export tidyterra pseudocolor raster to QGIS
Source: https://github.com/yutannihilation/ggplot2qgis/blob/main/README.md
Creates a single-band pseudocolor layer in QGIS that reproduces the continuous fill scale from a tidyterra plot.
```r
library(tidyterra)
volcano2 <- terra::rast(system.file("extdata/volcano2.tif", package = "tidyterra"))
p <- ggplot() +
geom_spatraster(data = volcano2) +
scale_fill_whitebox_c()
write_qgs(p, "volcano.qgs")
```
--------------------------------
### Exporting tmap objects to QGIS
Source: https://github.com/yutannihilation/ggplot2qgis/blob/main/README.md
Export tmap objects to QGIS, preserving color scales and basemaps defined in the tmap object.
```r
library(tmap)
x <- tm_basemap("OpenStreetMap") +
tm_shape(nc) +
tm_polygons(fill = "AREA")
write_qgs(x, "nc_tmap.qgs")
```
--------------------------------
### Export tidyterra filled contours to QGIS
Source: https://github.com/yutannihilation/ggplot2qgis/blob/main/README.md
Creates a polygon layer where each contour band is represented as a feature with a categorized renderer.
```r
p <- ggplot() +
geom_spatraster_contour_filled(data = volcano2) +
geom_spatraster_contour(data = volcano2)
write_qgs(p, "volcano_bands.qgs")
```
--------------------------------
### Export tmap raster to QGIS
Source: https://github.com/yutannihilation/ggplot2qgis/blob/main/README.md
Converts tmap raster objects into QGIS raster layers, preserving classes, colors, and legend labels.
```r
library(stars)
data(land, package = "tmap")
x <- tm_shape(land) + tm_raster("cover")
write_qgs(x, "land.qgs")
```
--------------------------------
### Export tidyterra contour labels to QGIS
Source: https://github.com/yutannihilation/ggplot2qgis/blob/main/README.md
Produces a line layer with QGIS labeling enabled, using custom label formatting.
```r
p <- ggplot() +
geom_spatraster_contour_text(
data = volcano2,
label_format = scales::label_number(suffix = " m")
)
write_qgs(p, "volcano_contour_text.qgs")
```
--------------------------------
### Export tidyterra contour lines to QGIS
Source: https://github.com/yutannihilation/ggplot2qgis/blob/main/README.md
Generates a GeoPackage-backed line layer where contour values are preserved as attributes.
```r
p <- ggplot() +
geom_spatraster(data = volcano2) +
geom_spatraster_contour(data = volcano2, aes(colour = after_stat(level)))
write_qgs(p, "volcano_contour.qgs")
```
--------------------------------
### Exporting tidyterra SpatVector to QGIS
Source: https://github.com/yutannihilation/ggplot2qgis/blob/main/README.md
Export spatial data visualized with tidyterra's geom_spatvector to a QGIS project.
```r
library(tidyterra)
cyl <- terra::vect(system.file("extdata/cyl.gpkg", package = "tidyterra"))
p <- ggplot(cyl) +
geom_spatvector(aes(fill = name))
write_qgs(p, "cyl.qgs")
```
--------------------------------
### Exporting ggplot2 sf objects to QGIS
Source: https://github.com/yutannihilation/ggplot2qgis/blob/main/README.md
Use write_qgs to export a ggplot2 object containing geom_sf layers to a QGIS project file.
```r
library(ggplot2)
library(ggplot2qgis)
nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)
p <- ggplot(nc) +
geom_sf(aes(fill = AREA))
write_qgs(p, "nc.qgs")
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.