Is it possible to do Dynamic Call to ggplot stat_function?

101 views Asked by At

This is some snippets of How to draw a ggplot curve over a mixture gaussian model

ggplot(mix_example) + geom_histogram(aes(x = x, y = ..density..)) + 
  stat_function(geom = "line", fun = fun_prop, color="red",
                args = list(mean = comp_1[1], sd = comp_1[2], 
                proportion = proportions[1])) +
  stat_function(geom = "line", fun = fun_prop, color="green",
                args = list(mean = comp_2[1], sd = comp_2[2], 
                proportion = proportions[2]))+
  stat_function(geom = "line", fun = fun_prop, color="blue",
                args = list(mean = comp_3[1], sd = comp_3[2], 
                proportion = proportions[3]))

enter image description here

The Snippets above add 3 stat function to draw a cluster line over a density of mixture distribution, the argument (args) come from flexmix model which consist mean and standard deviation for each cluster of mixture distribution, I would like to expand this to an "n degree" so that it applies to plot an n lines of n clusters of mixture distribution. But I wonder is there any approach to do it without adding stat_function one by one

1

There are 1 answers

0
Justin Landis On BEST ANSWER

In ggplot2 there is a S3 method ggplot_add (responsible for how elements/layers are added) called ggplot_add.list. This will add each element of the list to the plot. So you could write a wrapper:

multi_stat <- function(.obj, .prop, .color){
  mapply(function(x, prop, color){
    stat_function(
      geom = "line",
      fun = fun_prop,
      color = color,
      args = list(mean = x[1], sd = x[2], proportion = prop))},
    x = .obj, prop = .prop, color = .color, SIMPLIFY = F)
}
      

ggplot(mix_example) + 
  geom_histogram(aes(x = x, y = ..density..)) +
  multi_stat(list(comp_1, comp_2, comp_3),
             proportions,
             c("red","green","blue"))