Graphic Design with ggplot2

Working with Labels and Annotations:
Solution Exercise 2

Cédric Scherer // rstudio::conf // July 2022
  • Create this logo:

Load and Modify Image

skyline <- magick::image_read(here::here("materials", "exercises", "img", "rstudioconf-washington-bg.png"))

skyline

Create a Blue Dot

library(tidyverse)

ggplot(mapping = aes(x = 0, y = 0)) +
  geom_point(
    color = "#71a5d4", size = 150
  ) +
  xlim(-5, 5) +
  ylim(-5, 5) +
  theme_void()

Add the “R”

ggplot(mapping = aes(x = 0, y = 0)) +
  geom_point(
    color = "#71a5d4", size = 150
  ) +
  geom_text(
    label = "R", size = 80,
    family = "Tabular",
    color = "white"
  ) +
  xlim(-5, 5) +
  ylim(-5, 5) +
  theme_void()

Add the rstudio::conf Skyline

ggplot(mapping = aes(x = 0, y = 0)) +
  annotation_custom(
    grid::rasterGrob(
      image = skyline,
      x = .5,
      y = .5
    )
  ) +
  geom_point(
    color = "#71a5d4", size = 150
  ) +
  geom_text(
    label = "R", size = 80,
    family = "Tabular",
    color = "white"
  ) +
  xlim(-5, 5) +
  ylim(-5, 5) +
  theme_void()

Position the rstudio::conf Skyline

ggplot(mapping = aes(x = 0, y = 0)) +
  annotation_custom(
    grid::rasterGrob(
      image = skyline,
      x = .5,
      y = .1
    )
  ) +
  geom_point(
    color = "#71a5d4", size = 150
  ) +
  geom_text(
    label = "R", size = 80,
    family = "Tabular",
    color = "white"
  ) +
  xlim(-5, 5) +
  ylim(-7.5, 5) +
  theme_void()

Position the rstudio::conf Skyline

ggplot(mapping = aes(x = 0, y = 0)) +
  annotation_custom(
    grid::rasterGrob(
      image = skyline,
      x = .5,
      y = -.2
    )
  )  +
  geom_point(
    color = "#71a5d4", size = 150
  ) +
  geom_text(
    label = "R", size = 80,
    family = "Tabular",
    color = "white"
  ) +
  coord_cartesian(clip = "off") +
  xlim(-5, 5) +
  ylim(-5, 5) +
  theme_void() +
  theme(
    plot.margin = margin(t = 30, b = 120)
  )

Change Background Color

ggplot(mapping = aes(x = 0, y = 0)) +
  annotation_custom(
    grid::rasterGrob(
      image = skyline,
      x = .5,
      y = .1
    )
  ) +
  geom_point(
    color = "#71a5d4", size = 150
  ) +
  geom_text(
    label = "R", size = 80,
    family = "Tabular",
    color = "white"
  ) +
  xlim(-5, 5) +
  ylim(-7.5, 5) +
  theme_void() +
  theme(
    plot.background = element_rect(
      fill = "#fffdf9", color = "#fffdf9"
    )
  )

Save the Plot

ggsave(here::here("exercises", "plots", "05_annotations_ex2.png"),
       width = 5, height = 6, dpi = 300)
The final logo with an aspect ratio of 5 x 6 inches.

Or: Combine Images with {magick}

g <- ggplot(mapping = aes(x = 0, y = 0)) +
  geom_point(color = "#71a5d4", size = 150) +
  geom_text(
    label = "R", size = 80,
    family = "Tabular", color = "white"
  ) +
  theme_void()  +
  theme(plot.background = element_rect(fill = "#fffdf9", color = "#fffdf9"))
ggsave(plot = g, filename = here::here("exercises", "img", "rstudio-dot.png"),
       width = 5, height = 5, dpi = 3600)

Or: Combine Images with {magick}

library(magick)

dot <- image_read(here::here("exercises", "img", "rstudio-dot.png"))
img <- c(dot, skyline)
img <- image_append(image_scale(img, "1500"), stack = TRUE)
image_write(img, path = here::here("exercises", "plots", "05_annotations_ex2_combined.png"), format = "png")
The final logo, combined with ImageMagick.