Setting, not mapping, after plot creation

7 views
Skip to first unread message

Doug Mitarotonda

unread,
Jul 29, 2015, 3:31:07 PM7/29/15
to ggplot2
Hey everyone,
Another question for you all today. I often like to create a “base” plot in a function and then update the aesthetics I want later, which is easy to do with the `aes` function. But if I want to set, not map, later, how do you do that?

library(ggplot2)
dft <- data.frame(x=LETTERS[1:3], y=1:3, stringsAsFactors = FALSE)
basePlot <- function(dft){ ggplot(dft) + geom_bar(aes(x, y), stat = "identity") }

# Now I can do things like, without changing the base plot
basePlot(dft) + aes(fill = x)
basePlot(dft) + aes(alpha = y)

# However, suppose I want to set, not map, alpha, is that possible to do outside of the geom?
# errors
# basePlot(dft) + layer(alpha = 0.5)

Thanks!
Doug

Hadley Wickham

unread,
Jul 29, 2015, 3:54:02 PM7/29/15
to Doug Mitarotonda, ggplot2
Unfortunately there's no way to change the layers after they've been created.
Hadley
> --
> --
> You received this message because you are subscribed to the ggplot2 mailing list.
> Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>
> ---
> You received this message because you are subscribed to the Google Groups "ggplot2" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
http://had.co.nz/

Doug Mitarotonda

unread,
Jul 29, 2015, 4:06:57 PM7/29/15
to Hadley Wickham, ggplot2
No problem, thanks for letting me know Hadley. I can work around this by making it a part of the basePlot function call. I just have always been wondering if there was a parallel structure.

basePlot <- function(dft, ...){ ggplot(dft) + geom_bar(aes(x, y), stat = "identity", ...) }
Reply all
Reply to author
Forward
0 new messages