I hadn’t found a super-easy-you-don’t-have-to-think-about-it way to crop an image to a circle in R with a transparent background. There’s this stack overflow but I wouldn’t call it straightforward. So, I wrote this small package to do what I wanted.
In a nutshell, you pass a vector of image paths, which can be either local or from a URL link to circle_crop
, and it will
- Download the images if required
- Crop the image to a circle with a transparent background,
- Return the path where the cropped images are saved
circle_crop
can save the images to a specific location for use later, or if no location is given it will save them to a temporary location which is cleared when the session ends. All of this can happen in a single mutate step, so yeah, you don’t have to think about it. The cropped images can then be plotted with ggimage
, ggpath
, etc, or included in tables using e.g. gt
, reactable
, etc.
Installation
Firstly, install from Git.
devtools::install_github("doehm/cropcircles")
Example
Here’s a quick example. This demonstrates how circle_crop
will …
- Download the images from Open Psychometrics
- Circle crop the images and
- Make them available to plot on a chart
All in a few lines.
library(cropcircles)
library(dplyr)
library(ggimage)
images <- c(
"https://openpsychometrics.org/tests/characters/test-resources/pics/BB/1.jpg",
"https://openpsychometrics.org/tests/characters/test-resources/pics/BB/3.jpg",
"https://openpsychometrics.org/tests/characters/test-resources/pics/BB/9.jpg",
"https://openpsychometrics.org/tests/characters/test-resources/pics/BB/8.jpg")
df <- tibble(y = 1:4, images = images) |>
mutate(images_cropped = circle_crop(images)) # where the magic happens
df |>
ggplot() +
geom_image(aes(1.5, y, image = images), size = 0.15) +
geom_image(aes(3.5, y, image = images_cropped), size = 0.15) +
xlim(0, 5) +
ylim(0, 5) +
coord_fixed()

The circle_crop
function can take an image with any dimensions. It will circle crop the image from the center with a diameter of the smallest dimension.