Skip to content

Instantly share code, notes, and snippets.

@yjunechoe
Created February 3, 2021 17:17
Show Gist options
  • Save yjunechoe/60d5cdbac319a4972b7e5f7dac09d160 to your computer and use it in GitHub Desktop.
Save yjunechoe/60d5cdbac319a4972b7e5f7dac09d160 to your computer and use it in GitHub Desktop.
library(tidyverse)
library(patchwork)
library(magick)
df <- tribble(
~Condition, ~Referent, ~Accuracy,
"Primacy", "Single", 0.63,
"Primacy", "Primacy", 0.59,
"Recency", "Single", 0.63,
"Recency", "Recency", 0.5,
"Both", "Single", 0.63,
"Both", "Primacy", 0.5,
"Both", "Recency", 0.31
) %>%
mutate(
error_low = runif(7, .04, .06),
error_high = runif(7, .04, .06),
Condition_name = factor(Condition, levels = c("Primacy", "Recency", "Both")),
Condition = as.numeric(Condition_name),
Referent = factor(Referent, levels = c("Single", "Primacy", "Recency")),
left = Referent == "Single",
color = case_when(
Referent == "Single" ~ "#29476B",
Referent == "Primacy" ~ "#AD403D",
Referent == "Recency" ~ "#9BBB58"
)
)
my_plot <- ggplot(mapping = aes(x = Condition, y = Accuracy, fill = color)) +
geom_col(
data = filter(df, left),
width = .3,
color = "white",
position = position_nudge(x = -.3),
) +
geom_errorbar(
aes(ymin = Accuracy - error_low, ymax = Accuracy + error_high),
data = filter(df, left),
width = .1,
position = position_nudge(x = -.3)
) +
geom_col(
data = filter(df, !left),
color = "white",
width = .3,
) +
geom_errorbar(
aes(y = y, ymin = y - error_low, ymax = y + error_high),
data = filter(df, !left) %>%
group_by(Condition) %>%
mutate(y = accumulate(Accuracy, sum)),
width = .1
) +
scale_fill_identity(
labels = c("Single", "Primacy", "Recency"),
guide = guide_legend(
title = NULL,
override.aes = list(fill = c("#29476B", "#AD403D", "#9BBB58"))
)
) +
scale_x_continuous(
breaks = 1:3 - .15,
labels = levels(df$Condition_name),
expand = expansion(c(.1, .05))
) +
scale_y_continuous(
breaks = scales::pretty_breaks(6),
labels = scales::percent_format(1),
limits = 0:1,
expand = expansion(0)
) +
labs(
title = "Accuracy by Condition and Referent",
y = NULL
) +
theme_classic(
base_size = 12
) +
theme(
plot.title.position = "plot",
plot.margin = margin(rep(.2, 4), unit = 'in')
)
my_png <- magick::image_read("https://upload.wikimedia.org/wikipedia/commons/thumb/d/dc/Grumpy_Cat_%2814556024763%29_%28cropped%29.jpg/220px-Grumpy_Cat_%2814556024763%29_%28cropped%29.jpg")
my_png_framed <- magick::image_frame(my_png, "grey70", "80x80+10+5") # top x bottom + outerframe + innerframe
my_png_as_plot <- magick::image_ggplot(my_png_framed)
my_patchwork <- wrap_plots(
my_plot, my_png_as_plot,
my_plot, my_plot,
my_plot, my_plot
) +
plot_layout(
widths = 1, # keeps width of all plots same ratio
ncol = 2,
guides = 'collect'
)
my_final <- my_patchwork +
plot_annotation(tag_levels = "A", tag_prefix = "Fig. ") &
theme(plot.tag = element_text(size = 10, hjust = 0, margin = margin(b = .1, unit = 'in')))
ggsave("my_patchwork.png", my_final, height = 12, width = 9, dpi = 300)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment