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]))
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
In
ggplot2
there is a S3 methodggplot_add
(responsible for how elements/layers are added) calledggplot_add.list
. This will add each element of the list to the plot. So you could write a wrapper: