I want to streamline my work with ggplot2 given certain settings I repeat across plots. To this end, I want to:
- set aliases for those
ggplot2settings (assigned to individuallistobjects) - store them all in an
.Rscript source()the.Rscript- call the aliases when creating a plot with
ggplot
However, this workflow fails when I set aliases for ggplot functions that call vectors. For example, scale_x_discrete() has an argument called labels, which takes a character vector of strings. This vector can be set in-place (e.g., scale_x_discrete(labels = c("a", "b", "c"))) or otherwise call a vector object stored in the environment:
labels_for_x <- c("a", "b", "c")
scale_x_discrete(labels = labels_for_x)
Therefore, my problem happens when I want to set an "alias" for a ggplot function that relies on an additional, separate vector.
Demonstrating the problem with example
Step 1 -- set alias for scale_x_discrete in a new .R script
library(tidyverse)
mygg_scale_x_relabel <-
scale_x_discrete() %>%
list
Works! But what if:
mygg_scale_x_relabel <-
scale_x_discrete(labels = labels_for_barplot) %>%
list
Error in check_breaks_labels(breaks, labels) : object 'labels_for_barplot' not found
So I can do:
labels_for_barplot <- c()
mygg_scale_x_relabel <-
scale_x_discrete(labels = labels_for_barplot) %>%
list
And this runs OK.
Step 2 -- open a new .R/.Rmd script and source the previous .R script with alias
source(file = ...)
Step 3 -- in the new script, generate a bar plot
library(ggplot2)
df <- data.frame(trt = c("col_1", "col_2", "col_3"), outcome = c(2.3, 1.9, 3.2))
p <-
ggplot(df, aes(trt, outcome)) +
geom_col()
p
Step 4 -- (PROBLEM HAPPENS HERE) -- call the scale_x_discrete alias
labels_for_barplot <- c(col_1 = "new_lab_col_1",
col_2 = "new_lab_col_2",
col_3 = "new_lab_col_3")
p +
mygg_scale_x_relabel
<----- You can see here that all x labels are now gone!
This is because I've set labels_for_barplot <- c() (meaning, an empty vector) in my aliases script. But why this cannot be overridden with an updated vector assignment after script is sourced?


