Learn R Programming

geometr (version 0.2.5)

gs_polygon: Create a polygon geom

Description

Create any (regular) polygon geometry (of class geom) either by specifying anchor values or by sketching it.

Usage

gs_polygon(
  anchor = NULL,
  window = NULL,
  features = 1,
  vertices = NULL,
  sketch = NULL,
  regular = FALSE,
  ...
)

gs_triangle(anchor = NULL, window = NULL, sketch = NULL, features = 1, ...)

gs_square(anchor = NULL, window = NULL, sketch = NULL, features = 1, ...)

gs_rectangle(anchor = NULL, window = NULL, sketch = NULL, features = 1, ...)

gs_hexagon(anchor = NULL, window = NULL, sketch = NULL, features = 1, ...)

Arguments

anchor

[geom(1)|data.frame(1)] Object to derive the geom from. It must include column names x, y and optionally a custom fid.

window

[data.frame(1)] in case the reference window deviates from the bounding box of anchor (minimum and maximum values), specify this here.

features

[integerish(1)] number of polygons to create.

vertices

[integerish(.)] number of vertices per polygon; will be recycled if it does not have as many elements as specified in features.

sketch

[raster(1)] raster object that serves as template to sketch polygons.

regular

[logical(1)] should the polygon be regular, i.e. point symmetric (TRUE) or should the vertices be selected as provided by anchor (FALSE, default)?

...

[various] graphical parameters to gt_locate, in case points are sketched; see gpar

Value

An invisible geom.

Functions

  • gs_triangle: wrapper of gs_polygon where vertices = 3 and regular = TRUE.

  • gs_square: wrapper of gs_polygon where vertices = 4 and regular = TRUE.

  • gs_rectangle: wrapper of gs_polygon where vertices = 2, regular = FALSE and the two complementing corners are derived from the two given opposing corners.

  • gs_hexagon: wrapper of gs_polygon where vertices = 6 and regular = TRUE.

Details

The arguments anchor and sketch indicate how the line is created:

  • if anchor is set, the line is created parametrically from the given objects' points,

  • if an object is set in sketch, this is used to create the geom interactively, by clicking into the plot.

The argument regular determines how the vertices provided in anchor or via sketch are transformed into a polygon:

  • if regular = FALSE the resulting polygon is created from all vertices in anchor,

  • if regular = TRUE, only the first two vertices are considered, as center and indicating the distance to the (outer) radius.

See Also

Other geometry shapes: gs_line(), gs_point(), gs_random()

Examples

Run this code
# NOT RUN {
# 1. create a polygon programmatically
coords <- data.frame(x = c(40, 70, 70, 50),
                     y = c(40, 40, 60, 70))

# if no window is set, the bounding box will be set as window
(aGeom <- gs_polygon(anchor = coords))

# the vertices are plottet relative to the window
library(magrittr)
window <- data.frame(x = c(0, 80),
                     y = c(0, 80))
gs_polygon(anchor = coords, vertices = 6, window = window,
           regular = TRUE) %>%
  visualise(linecol = "green")

# when a geom is used in 'anchor', its properties are passed on
aGeom <- setWindow(x = aGeom, to = window)
gs_polygon(anchor = aGeom) %>%
  visualise(geom = ., fillcol = "deeppink")
gs_rectangle(anchor = aGeom) %>%
  visualise(geom = ., new = FALSE)
# }
# NOT RUN {
# 2. sketch a hexagon by clicking into a template
gs_hexagon(sketch = gtRasters$continuous) %>%
  visualise(geom = ., linecol = "deeppink", linetype = 2, new = FALSE)
# }

Run the code above in your browser using DataLab