MyNixOS website logo
Description

Record Your Plot History.

Record and generate a 'gif' of your 'R' sessions plots. When creating a visualization, there is inevitably iteration and refinement that occurs. Automatically save the plots made to a specified directory, previewing them as they would be saved. Then combine all plots generated into a 'gif' to show the plot refinement over time.

camcorder

Codecov testcoverage R-CMD-check

{camcorder} is an an R package to track and automatically save graphics generated with {ggplot2} that are created across one or multiple sessions with the eventual goal of creating a GIF showing all the plots saved sequentially during the design process.

After installation, the package enables you to:

  • save a ggplot automatically every time you run ggplot() in any format with given specifications
  • generate a GIF that showcases every step of the design process using those image files
  • inspect the ggplot output directly with your specifications in the RStudio IDE—you’ll get what you see[^1]

Installation

Currently {camcorder} is only available on GitHub, and can be installed using the following command.

# install.packages("camcorder")
remotes::install_github("thebioengineer/camcorder")

Goal End Product

The idea of tracking your plots as part of your development process and generating a making-of movie was popularized by two contributors to this project: Georgios Karamanis and Cédric Scherer. They have wowed the R community weekly with their “how its made” videos. Below are a few examples of the goal end products.

TidyTuesday 2020/28
by Cédric Scherer

TidyTuesday 2020/15
by Georgios Karamanis

How To

To get started, load {camcorder} and initialize recording using the gg_record() function. This function has several options, such as where to save the recordings, device to use to save the recordings, and the height/width of the image to create. By default it will save to a temporary directory so recordings will go away once the R session is closed.

library(ggplot2)
library(camcorder)

gg_record(
  dir = file.path(tempdir(), "recording100"), # where to save the recording
  device = "png", # device to use to save images
  width = 4,      # width of saved image
  height = 6,     # height of saved image
  units = "in",   # units for width and height
  dpi = 300       # dpi to use when saving image
)

Once the recorder is initialized, any ggplot that is made and printed will be automatically (or automagically[^2]) recorded.

ggplot(mtcars, aes(x = mpg, y = hp)) +
  geom_point()

ggplot(mtcars, aes(x = mpg, y = hp)) + 
  geom_point(aes(shape = as.factor(gear)))

ggplot(mtcars, aes(x = mpg, y = hp)) + 
  geom_point(aes(color = as.factor(gear)))

ggplot(mtcars, aes(x = mpg, y = hp)) + 
  geom_point(aes(color = as.factor(gear))) +
  geom_path()

ggplot(mtcars, aes(x = mpg, y = hp)) +
  geom_point(aes(color = disp)) +
  geom_smooth()

ggplot(mtcars, aes(x = mpg, y = hp)) +
  geom_smooth() +
  geom_point(aes(color = disp))

ggplot(mtcars, aes(x = mpg, y = hp)) + 
  geom_smooth() +
  geom_point(aes(color = disp)) +
  scale_color_viridis_c() +
  theme_light()

ggplot(mtcars, aes(x = mpg, y = hp)) + 
  geom_smooth() +
  geom_point(aes(color = disp)) +
  scale_color_viridis_c() +
  theme_light() +
  labs(
    title = "MPG vs Horse Power!",
    subtitle = "Power and economy, the classic compromise!"
  )

ggplot(mtcars, aes(x = mpg, y = hp)) + 
  geom_smooth() +
  geom_point(aes(color = disp)) +
  scale_color_viridis_c() +
  theme_light(base_family = "Roboto Mono") +
  labs(
    title = "MPG vs Horse Power!",
    subtitle = "Power and economy, the classic compromise!"
  )

ggplot(mtcars, aes(x = mpg, y = hp)) + 
  geom_smooth() +
  geom_point(aes(color = disp)) +
  scale_color_viridis_c() +
  theme_light(base_family = "Roboto Mono") +
  labs(
    title = "MPG vs Horse Power!",
    subtitle = "Power and economy, the classic compromise!", 
    x = "Efficiency (Miles/Gallon)",
    y = "Power (Horsepower)",
    color = "Displacement\n(Cubic Inch)"
  )

If at any point, that you want to save your plots in a different format than what the recorder was initialized with this can be done through the gg_resize_film() function. This will set the size and dpi of all plots going forward.

gg_resize_film(
  height = 4,
  width = 6,
  units = "in",
  dpi = 350
)
ggplot(mtcars, aes(x = mpg, y = hp)) + 
  geom_smooth() +
  geom_point(aes(color = disp)) +
  scale_color_viridis_c() +
  theme_light(base_family = "Roboto Mono") +
  labs(
    title = "MPG vs Horse Power!",
    subtitle = "Power and economy, the classic compromise!", 
    x = "Efficiency (Miles/Gallon)",
    y = "Power (Horsepower)",
    color = "Displacement\n(Cubic Inch)"
  ) +
  theme(
    plot.title.position = "plot",
    plot.title = element_text(face = "bold")
  )

ggplot(mtcars, aes(x = mpg, y = hp)) + 
  geom_smooth() +
  geom_point(aes(color = disp)) +
  scale_color_viridis_c() +
  theme_light(base_family = "Roboto Mono")  +
  labs(
    title = "MPG vs Horse Power!",
    subtitle = "Power and economy, the classic compromise!", 
    x = "Efficiency (Miles/Gallon)",
    y = "Power (Horsepower)",
    color = "Displacement\n(Cubic Inch)"
  ) +
  theme(
    plot.title.position = "plot",
    plot.title = element_text(face = "bold"),
    panel.background = element_rect(colour = "turquoise", fill = "turquoise")
  )

Finally, to generate the final GIF, use the gg_playback() function. The user can define: - where the final GIF gets saved by setting the name argument, - duration of the first and last images with first_image_duration or last_image_duration - delay between frames in seconds with frame_duration

gg_playback(
  name = file.path(tempdir(), "recording", "vignette_gif.gif"),
  first_image_duration = 5,
  last_image_duration = 15,
  frame_duration = .4,
  image_resize = 800
)

Once rendering is complete, a GIF is opened in your viewer.

[^1]: In case you are saving to PDF, the file will automatically open in your default PDF viewer.

[^2]: A previous typo but actually it fits quite well.

Metadata

Version

0.1.0

License

Unknown

Platforms (78)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    uefi
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-freebsd
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64-uefi
  • aarch64-windows
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-linux
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-uefi
  • x86_64-windows