Skip to content

Instantly share code, notes, and snippets.

@muschellij2
Created February 15, 2018 21:41
Show Gist options
  • Save muschellij2/295801d5a350fbe3b03379f9074b88a0 to your computer and use it in GitHub Desktop.
Save muschellij2/295801d5a350fbe3b03379f9074b88a0 to your computer and use it in GitHub Desktop.
ggplot2 with apipe
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