79722833

Date: 2025-08-01 17:58:16
Score: 0.5
Natty:
Report link

Several years later, a simpler solution using geom_function() and only ggplot2 (version 3.5.2).

The approach is again the same as in this answer

# add variable to split by combination of year and generation
fantasy_df$yearxgen <- paste(fantasy_df$centralYear, fantasy_df$generation, sep = 'x')
# turn generation into a facto
fantasy_df$generation <- as.factor(fantasy_df$generation)

# initialize plot
gp <- ggplot()
# loop over combined variable, limit data to one combination of year and
# generation, plot scaled density with those params
for (i in unique(fantasy_df$yearxgen)){
  gp <- gp + geom_function(data = fantasy_df[fantasy_df$yearxgen == i,], 
                           fun = function(x, mu, sigma, lambda) lambda * dnorm(x, mu, sigma), 
                           args = list(mu = fantasy_df[fantasy_df$yearxgen == i,'mu'], 
                                       sigma = fantasy_df[fantasy_df$yearxgen == i,'sigma'], 
                                       lambda = fantasy_df[fantasy_df$yearxgen == i,'lambda']), 
                           xlim = c(0,400), aes(color = generation))
}
# split by variable
gp <- gp + facet_grid(centralYear ~ .)
# show plot
print(gp)
ggsave('figure.png')

enter image description here

Reasons:
  • Probably link only (1):
  • Long answer (-1):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: Steph_B