Home > Mobile >  R/ggplot2: geom_function & facet_wrap with multiple sets of parameters per facet
R/ggplot2: geom_function & facet_wrap with multiple sets of parameters per facet

Time:10-09

I would like to use ggplot2::geom_function() to plot functions for a set of given parameters from a data frame. These should then be organized into facets by a "year" parameter. There are solutions to similar problems on StackOverflow (input data frame

What I would like to have is an output that looks like this:

output plot

Here's a minimal working example including a test data frame:

# libraries
library(ggplot2) # for plotting
library(dplyr)   # for filtering data and pipes 


# Input data
fantasy_df <- data.frame(
  centralYear = c(2017, 2017, 2017, 2017, 2016, 2016, 2016, 2016, 2015, 2015, 2015, 2015, 2014, 2014, 2014, 2014, 2013, 2013, 2013),
  generation  = c(1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3),
  mu          = c(123.6, 188.7, 234.5, 269.6, 122.6, 188.4, 232.5, 269.6, 117.3, 187.1, 233.2, 271.3, 117.3, 187.3, 232.8, 271.6, 118.4, 193.9, 246.7),
  sigma       = c(14.6, 14.6, 14.6, 14.6, 14.8, 14.8, 14.8, 14.8, 15.1, 15.1, 15.1, 15.1, 15.4, 15.4, 15.4, 15.4, 17.5, 17.5, 17.5),
  lambda      = c(0.06, 0.44, 0.34, 0.15, 0.07, 0.46, 0.30, 0.17, 0.07, 0.46, 0.33, 0.15, 0.08, 0.45, 0.33, 0.14, 0.09, 0.53, 0.37) 
)

# Plot function & colours
ndist_function <- function(x, mu, sigma, lam) {
  lam * dnorm(x, mu, sigma)
}

colours <- c("blue", "red", "green", "violet")


# Simple single plot
singleYear_df <- fantasy_df[1:4, ]
  
fantasy_df %>%
  ggplot()  
  geom_function(fun = ndist_function,
                args = list(singleYear_df[1, 3], singleYear_df[1, 4], singleYear_df[1, 5]),
                colour = colours[1], lwd = 1.5)  
  geom_function(fun = ndist_function,
                args = list(singleYear_df[2, 3], singleYear_df[2, 4], singleYear_df[2, 5]),
                colour = colours[2], lwd = 1.5)  
  geom_function(fun = ndist_function,
                args = list(singleYear_df[3, 3], singleYear_df[3, 4], singleYear_df[3, 5]),
                colour = colours[3], lwd = 1.5)  
  geom_function(fun = ndist_function,
                args = list(singleYear_df[4, 3], singleYear_df[4, 4], singleYear_df[4, 5]),
                colour = colours[4], lwd = 1.5)  
  xlim(0, 300) 
  ylab("Density")



# Complex wrapped plot
gen1 <- filter(fantasy_df, generation == 1)
gen2 <- filter(fantasy_df, generation == 2)
gen3 <- filter(fantasy_df, generation == 3)
gen4 <- filter(fantasy_df, generation == 4)
  
ggplot(data=fantasy_df)  
  geom_function(fun = ndist_function,
                args = list(gen1$mu, gen1$sigma, gen1$lambda),
                colour = colours[1], lwd = 1.5) 
  geom_function(fun = ndist_function,
                args = list(gen2$mu, gen2$sigma, gen2$lambda),
                colour = colours[2], lwd = 1.5) 
  geom_function(fun = ndist_function,
                args = list(gen3$mu, gen3$sigma, gen3$lambda),
                colour = colours[3], lwd = 1.5) 
  geom_function(fun = ndist_function,
                args = list(gen4$mu, gen4$sigma, gen4$lambda),
                colour = colours[4], lwd = 1.5) 
  facet_wrap(~centralYear, nrow=5) 
  xlim(0, 300)

The single plot for a given year looks fine:

single plot, geom_function

But the combined plot using facet_wrap does not:

combined plot, geom_function and facet_wrap

This is obviously not what I'd like to have. It seems the same function is plotted in every facet. Maybe there's also a different solution than using facet_wrap.

Any help would be very much appreciated!

CodePudding user response:

Adpating enter image description here

  • Related