Skip to content

Instantly share code, notes, and snippets.

@abikoushi
Created April 2, 2024 05:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abikoushi/00acd4b9b2f8b7889703f56a80d908f9 to your computer and use it in GitHub Desktop.
Save abikoushi/00acd4b9b2f8b7889703f56a80d908f9 to your computer and use it in GitHub Desktop.
geom_function with facet_wrap
library(dplyr)
library(ggplot2)
set.seed(123)
fdf <- expand_grid(year=1:4, generation=1:4) %>%
mutate(mu=rnorm(16),sigma=abs(rnorm(16)))
layerfun <- split(fdf, interaction(fdf$year, fdf$generation)) %>%
lapply(function(x){
geom_function(data = x,
fun = dnorm,
args = list(mean=x$mu, sd=x$sigma),
aes(colour = factor(generation)))
})
ggplot(fdf) +
layerfun +
facet_wrap(~year, nrow = 5) +
xlim(-4, 4)
ggsave("statfuns.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment