💾 Archived View for republic.circumlunar.space › users › johngodlee › posts › 2019-08-20-expression.… captured on 2023-04-19 at 23:51:57. Gemini links have been rewritten to link to archived content
⬅️ Previous capture (2021-12-04)
-=-=-=-=-=-=-
DATE: 2019-08-20
AUTHOR: John L. Godlee
On the current theme of writing papers, I've been preparing figures in R using ggplot2 for a manuscript. I need to put subscripts, greek letters and other non-standard bits of text into facet labels and axis labels, mostly to denote the units of a variable. I found that there are multiple ways to achieve this and unfortunately it seems like different ways are necessary in different contexts, however, a lot of what I needed to do could be covered by expression(), which constructs mathematical expressions in R using "plotmath" syntax.
I wrote notes on how to accomplish different tasks requiring plotmath and I thought I would put those notes here. See below:
<hr>
expression() notation:
Notes:
Example workflow for ggplot2 axis titles:
library(ggplot2) library(dplyr) library(tidyr) area <- rnorm(n = 50, mean = 10, sd = 1) leaf_chloro <- rnorm(n = 50, mean = 100, sd = 2) groups <- rep(c("A", "B", "C", "D", "E"), times = 10) df <- data.frame(area, leaf_chloro, groups) ggplot(df, aes(x = groups, y = area)) + geom_bar(stat = "identity", aes(fill = groups)) + labs(x = "Group", y = expression("Leaf" ~ "area" ~ (cm^2)))
Facet labels:
df_long <- gather(df, key = var, value = value, -groups) df_label <- df_long %>% mutate(var_exp = factor(var, levels = c("area", "leaf_chloro"), labels = c( expression("Leaf" ~ "area" ~ (cm^2)), expression("Chlorophyll-" ~ alpha) ))) ggplot(df_label, aes(x = groups, y = value)) + geom_bar(stat = "identity", aes(fill = groups)) + facet_wrap(~var_exp, labeller = label_parsed)
Axis labels:
ggplot(df, aes(x = groups, y = area)) + geom_bar(stat = "identity", aes(fill = groups)) + scale_x_discrete(labels = c( "A" = expression(alpha), "B" = expression(beta), "C" = expression(gamma), "D" = expression(epsilon), "E" = expression(omega)))
Custom annotations and labels:
ggplot(df, aes(x = groups, y = area)) + geom_bar(stat = "identity", aes(fill = groups)) + geom_label(aes(x = 1, y = 25, label = "(alpha != Alpha)"), parse = T)