Connecting artpack Assets to ggplot2 Geoms

Introduction

This vignette goes over different artpack asset creations and their recommended ggplot2 geoms that can be used when creating art in R with artpack and ggplot2. To start, let’s look at a handy chart that can be used as a reference for artpack users.

Feeding in the Created artpack Data to ggplot Plots

The focus of artpack is to provide you with crafted data frames that can be easily used within ggplot2 plots. Note that there are different ways to place data into a ggplot:

Via aes()

With I()


#| fig-alt: "Scatter plot on dark background showing various colored circular points and concentric spiral patterns. Points include solid circles in blue, teal, purple, green, and orange, along with larger spiral/circular patterns in magenta, purple, and orange-red. The elements are randomly distributed across the plot area."

# install.packages("artpack")
library(ggplot2)
library(artpack)
set.seed(0515)

df_packed_circles <-
   packer(
   n = 25, big_r = 7, med_r = 3, small_r = 1,
   min_x = 0, max_x = 100, min_y = 0, max_y = 100,
   color_pal = art_pals("rainbow", 15),
   circle_type = "swirl"
 )
#> ⠙ Sampling for big-sized circles started✔ Sampling for big-sized circles started [51ms]
#> ℹ Big-sized circles complete!✔ Big-sized circles complete! [11ms]
#> ⠙ Sampling for medium-sized circles started✔ Sampling for medium-sized circles started [91ms]
#> ℹ Medium-sized circles complete!✔ Medium-sized circles complete! [10ms]
#> ⠙ Sampling for small-sized circles started✔ Sampling for small-sized circles started [55ms]
#> ℹ Small-sized circles complete!✔ Small-sized circles complete! [15ms]
 
 df_packed_circles |>
   ggplot(aes(x, y, group = group, color = I(color))) + # I (base R) takes the color values as-is and passes it through
   theme_void() +
   theme(plot.background = element_rect(fill = "#333333")) +
   geom_path() +
   coord_equal()

With scale_{xx}-type functions


#| fig-alt: "Scatter plot on dark background showing various colored circular points and concentric spiral patterns. Points include solid circles in blue, teal, purple, green, and orange, along with larger spiral/circular patterns in magenta, purple, and orange-red. The elements are randomly distributed across the plot area."
#| 
df_packed_circles |>
  ggplot(aes(x, y, group = group, color = color)) +
  theme_void() +
  theme(plot.background = element_rect(fill = "#333333")) +
  scale_color_identity(guide = "none") + # Takes color values as-is and removes plot legend
  geom_path() +
  coord_equal()

Via geom layers


#| fig-alt: "Similar scatter plot to previous with slight variations in positioning. Shows colored circular points and concentric spiral patterns on dark background, with solid circles in various colors (blue, teal, purple, green, orange) and larger decorative spiral elements in magenta, purple, and orange-red."
#| 
df_packed_circles |>
  ggplot(aes(x, y, group = group)) +
  theme_void() +
  theme(plot.background = element_rect(fill = "#333333")) +
  geom_path(aes(x = x + 1, y = y - .7), color = "#000000") + # Changes made directly in the layer
  geom_path(color = df_packed_circles$color) + # Column from df placed directly in the layer
  coord_equal()

Making Sense of Grouping In ggplot2

One last important thing to remember about using ggplot2 for art, is grouping. Getting comfortable with ggplot grouping is an essential skill for creating generative art. When creating art on a ggplot, you can think of it as drawing on a piece of paper in real life. Think of “grouping” as your way of telling ggplot2 to “pick the pencil up” off the “paper”. These grouping variables help ggplot to understand when to “stop drawing” a line for a shape. Let’s look at a quick example using the grid_maker() function:


#| fig-alt: "Heat map or grid visualization with color gradient from yellow at bottom to bright magenta/pink at top. The grid appears to be approximately 12x12 squares with smooth color transitions creating a temperature or intensity visualization."
#| 
df_grid <-
  grid_maker(
    xlim = c(0,10),
    ylim = c(0,10),
    size = 10,
    fill_pal = art_pals("sunnyside", 5),
    color_pal = sapply(art_pals("sunnyside", 5), \(x) set_brightness(x, 0.40))
  )

df_grid |>
  ggplot(aes(x,y, group = group)) +
  geom_polygon(
    fill = df_grid$fill,
    color = df_grid$color
  ) +
  theme_void() +
  coord_equal()

With the grouping function, the image looks as expected with nicely colored squares in a grid. If we remove the group argument from aes() in the ggplot() function:


#| fig-alt: "Grid-based visualization on yellow background showing a diagonal white triangular or wedge pattern. The triangle extends from the bottom-left corner toward the upper-right, creating a clear geometric division in the square grid layout."
#| 
df_grid |>
  ggplot(aes(x,y)) +
  geom_polygon(
    fill = df_grid$fill,
    color = df_grid$color
  ) +
  theme_void() +
  coord_equal()

It turns into a hot mess! This is because without a grouping variable, ggplot doesn’t understand where the data for a square stops and ends. Using groups allows more control over creative decisions and can simplify your workflow by allowing you to create and use multiple variables from a data frame within one geom layer. Remember that artpack has additional group-related functions to help you wrangle your group variables, like the group_sample() function which allows you to sample a data frame by groups (instead of just rows):


#| fig-alt: "Pixelated or mosaic-style visualization with irregular white and colored blocks. Uses a gradient color scheme from yellow at bottom through orange and red to magenta at top, creating an abstract pattern with white negative spaces throughout."
#| 
set.seed(01234)

df_grid |>
  group_sample(group = group, prop = .70) |>
  ggplot(aes(x,y, group = group, fill = I(fill), color = I(color))) +
  geom_polygon() +
  theme_void() +
  coord_equal()

Wrapping Up

This vignette demonstrated how artpack’s asset creation functions can be integrated within ggplot2’s geom layers.

Remember that:

See Also: