polygon_glow: Apply glow style to polygons.

View source: R/polygon_glow.R

polygon_glowR Documentation

Apply glow style to polygons.

Description

Forms a grob graphical object that will draw polygons with a glow.

Usage

polygon_glow(
  x,
  y,
  id,
  sub_id = id,
  gp = gpar(),
  n = 10,
  amount = 3,
  bg = TRUE,
  closed = FALSE,
  rule = "evenodd",
  ...
)

Arguments

x, y

A numeric or unit object specifying x/y coordinates.

id, sub_id

A numeric to separate locations into distinct paths or sub-paths. All coordinates with the same id belong to the same path or sub-path. These arguments are assumed to start at 1 and increase by exactly 1 at the start of a new path.

n

An integer for the number of times a line is to be repeated. Larger numbers look smoother but might also be somewhat heavier to compute.

amount

A numeric of length 1 indicating the amount of glow to produce.

bg

A logical of length 1. Whether to return the filled background of the polygon (default: TRUE).

closed

A logical of length 1. Whether the polygon is to be closed (default: FALSE).

...

Other arguments passed on to the grid::gpar() function.

colour, fill

A colour specification of either length 1 or of the same length as length(unique(sub_id)).

lwd

A numeric specifying the line width of either length 1 or of the same length as length(unique(sub_id)).

lty

A linetype specification of either length 1 or of the same length as length(unique(sub_id)).

Details

The glowing effect is achieved by copying the polygon several times with increasing width and decreasing alpha.

Will make separate foreground (lines) and background (fills) grobs.

Value

A list with elements fg and bg which are grobs or NULL.

Examples

# Some example data
df <- data.frame(
  x = c(0, 0.5, 1, 0.5, 0.25, 0.5, 0.75, 0.5),
  y = c(0.5, 0, 0.5, 1, 0.5, 0.25, 0.5, 0.75),
  sub_id = rep(c(1, 2), each = 4),
  id = rep(1, each = 8)
)

# Constructing polygon
pgon <- polygon_glow(
  df$x,  df$y,
  df$id, df$sub_id,
  colour = c("blue", "red"), lty = c(1, 4),
  fill = "grey50", n = 50, closed = TRUE, lwd = 3
)

# Rendering in grid
grid.newpage()
grid.draw(pgon$bg)
grid.draw(pgon$fg)

teunbrand/elementalist documentation built on Oct. 13, 2024, 11:11 a.m.