I'm currently working on writing a large file that will reference several ".R" code scripts to read a number of saved datasets to create a series of plots. I would like to avoid reading the data into my environment to minimize clutter (there are a lot of files).
For this reason, I would like to reference a dataset and pipe it to a "chunk" of ggplot options to create my desired aesthetics, etc. However, it appears that ggplot "forgets" the piped dataset after the first layer and I would like to find out if there is a way to get around this behavior.
There is a similar question here on SO (Referencing piped dataset in ggplot layers for subsetting) but the reference to the dataset occurs in a "geom" step with an explicit call for a data object. Oddly, I have no issues using the suggestions from this other question but they don't work for my particular problem.
In particular, I need to reference the dataset in order to tell ggplot how many axis ticks to create (I would like the data to inform this step so that a new axis will be created if the data changes in the future, e.g. more "years" are added). As an example:
dataset <- data.frame(year = c("2014", "2015", "2016"), measure = c(10, 15, 20))
dataset %>%
ggplot(aes(x = year, y = measure)) +
geom_bar(stat = "identity") +
scale_x_continuous(breaks = unique(.$year))
Gives the error
Error in unique(.$year) : object '.' not found
The pipe works fine in the first step (explicitly, 'data = ., ...'), but referencing the object that '.' is a placeholder for doesn't work in "downstream" layers.
Frequently I can use curly bracketing {} to circumvent this issue (for reasons I don't fully understand), but this doesn't work either:
dataset %>%
ggplot(aes(x = year, y = measure)) +
geom_bar(stat = "identity") +
{scale_x_continuous(breaks = unique(.$year))}
I suspect I may be too new to magrittr and ggplot to fully understand why "%=>% and "+" don't seem to play nicely together, but was hoping someone may be able to point me in the right direction. Thanks!