Created
February 15, 2018 21:41
-
-
Save muschellij2/295801d5a350fbe3b03379f9074b88a0 to your computer and use it in GitHub Desktop.
ggplot2 with apipe
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(ggplot2) | |
library(dplyr) | |
geom = function(gplot, type, ...) { | |
func_name = paste0("geom_", type) | |
args = list(...) | |
g = do.call(func_name, args = args) | |
gplot + g | |
} | |
# NAs break the line. Use na.rm = T to suppress the warning message | |
df <- data.frame( | |
x = 1:5, | |
y1 = c(1, 2, 3, 4, NA), | |
y2 = c(NA, 2, 3, 4, 5), | |
y3 = c(1, 2, NA, 4, 5) | |
) | |
ggplot(df, aes(x, y1)) + geom_point() + geom_line() | |
df %>% | |
ggplot(aes(x, y1)) %>% | |
geom("point") %>% | |
geom("line") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment