Skip to content

Instantly share code, notes, and snippets.

@BerriJ
Last active January 19, 2023 11:58
Show Gist options
  • Save BerriJ/edf1e9a9ac7cef2f455599dbd9acc737 to your computer and use it in GitHub Desktop.
Save BerriJ/edf1e9a9ac7cef2f455599dbd9acc737 to your computer and use it in GitHub Desktop.
Snippets for plotting with R
library(dplyr)
library(ggplot2)
library(ggsci)
library(tidyr)
material_pals <- c(
"red", "pink", "purple", "deep-purple", "indigo",
"blue", "light-blue", "cyan", "teal", "green", "light-green", "lime",
"yellow", "amber", "orange", "deep-orange", "brown", "grey", "blue-grey"
)
cols <- purrr::map(material_pals, ~ pal_material(.x)(10)) %>%
purrr::reduce(cbind)
colnames(cols) <- material_pals
cols %>%
as_tibble() %>%
mutate(idx = as.factor(1:10)) %>%
pivot_longer(-idx, names_to = "var", values_to = "val") %>%
mutate(var = factor(var, levels = material_pals[19:1])) %>%
ggplot() +
xlab(NULL) +
ylab(NULL) +
geom_tile(aes(x = idx, y = var, fill = val)) +
scale_fill_identity() +
scale_x_discrete(expand = c(0, 0)) +
scale_y_discrete(expand = c(0, 0)) +
theme_minimal() -> plot_cols
# Tell tikzdevice which documentclass you are using so that element dims are calculated accordingly.
options(tikzDocumentDeclaration = "\\documentclass[preprint,12pt]{elsarticle}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment