Last active
April 16, 2023 03:56
-
-
Save cmrnp/1bd97585640302811de709ec5e551cd0 to your computer and use it in GitHub Desktop.
An example of creating a plot in ggplot somewhat matching the style of SuperMongo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tidyverse) | |
library(ggrepel) | |
library(ragg) | |
library(showtext) | |
showtext_auto() | |
font_add("AVHershey Complex Medium", "AVHersheyComplexMedium.ttf") | |
data(Animals, package = "MASS") | |
theme_supermongo <- function( | |
base_size = 11, | |
base_family = "AVHershey Complex Medium", | |
base_line_size = 0.5, | |
base_rect_size = 0.5 | |
) { | |
theme_bw(base_size, base_family, base_line_size, base_rect_size) + | |
theme( | |
axis.text = element_text(size = rel(1), colour = "black"), | |
panel.grid.major = element_blank(), | |
panel.grid.minor = element_blank(), | |
axis.ticks.length = unit(0, "pt"), | |
plot.margin = margin(11, 11, 5.5, 5.5, "pt"), | |
) | |
} | |
ggplot(Animals, aes(x = log10(body), y = log10(brain))) + | |
geom_smooth( | |
method = "lm", | |
se = FALSE, | |
colour = "black", | |
linetype = "dashed", | |
size = 0.5 | |
) + | |
geom_point(pch = 1) + | |
geom_text_repel( | |
aes(label = rownames(Animals)), | |
size = 9 / .pt, | |
family = "AVHershey Complex Medium" | |
) + | |
scale_x_continuous( | |
limits = c(-2, 5), | |
breaks = -2:5, | |
labels = scales::label_math(), | |
expand = expansion(0), | |
) + | |
scale_y_continuous( | |
limits = c(-1, 4), | |
breaks = -1:4, | |
labels = scales::label_math(), | |
expand = expansion(0), | |
) + | |
annotation_logticks( | |
sides = "trbl", | |
short = unit(2.75, "pt"), | |
mid = unit(2.75, "pt"), | |
long = unit(5.5, "pt"), | |
size = 0.25 | |
) + | |
labs(x = "Body weight (kg)", | |
y = "Brain weight (g)") + | |
theme_supermongo() | |
showtext_opts(dpi = 600) | |
ggsave( | |
"ggplot_supermongo_animals.png", | |
width = 7, | |
height = 5, | |
dpi = 600, | |
device = ragg::agg_png | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment