I am having a difficulty to make a facetted plot with different pictures. Without not giving more background info, here is my code, the resulting plot, and the problem:
library(tidyverse)
library(showtext)
library(showtextdb)
library(ggtext)
library(magick)
library(ggimage)
library(cropcircles)
bob_ross <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2023/2023-02-21/bob_ross.csv')
bob_ross_top <- bob_ross %>%
mutate(col_num_n = str_count(colors, ",") + 1) %>%
arrange(desc(col_num_n)) %>%
slice(1:10) %>%
mutate(image = map(img_src, image_read),
img_circle = circle_crop(img_src, border_size = 12, border_colour = "black"))
ggplot(bob_ross_top, aes(x = painting_title, y = 1, image = img_circle)) +
geom_image(size = 1) +
scale_x_discrete(expand = c(0, 0)) +
scale_y_continuous(expand = c(0, 0)) +
theme_minimal() +
theme(axis.title = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank(),
panel.grid = element_blank(),
strip.text = element_text(hjust = 0.5)) +
facet_wrap(~ painting_title, ncol = 2)
and the resulting plot is this
As can be seen, the pictures in the facetted plot are weirdly aligned. How can I center align them? Additionally, is there a way to customize the horizontal and vertical space between facetted plots? Thank you for your attention beforehand.
>Solution :
The issue is that you map painting_title on x. Instead, to center align your images map a constant value on x, e.g. x = 1. For the spacing of the panels you could use theme options panel.spacing.x/y but I only use panel.spacing.y. Concerning the horizontal space it depends a bit on what you want to achieve, e.g. I simply reduced the width of the exported image to reduce the amount of horizontal white space.
ggplot(
bob_ross_top,
aes(x = 1, y = 1, image = img_circle)
) +
geom_image(size = 1) +
scale_x_discrete(expand = c(0, 0)) +
scale_y_continuous(expand = c(0, 0)) +
theme_minimal() +
theme(
axis.title = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank(),
panel.grid = element_blank(),
strip.text = element_text(hjust = 0.5),
panel.spacing.y = unit(1, "cm")
) +
facet_wrap(~painting_title, ncol = 2)

