my.fun <- function(x,y){y^2+x^2 -1} x<-seq(-1.5,1.5,length=1000) y<-seq(-1.5,1.5,length=1000) z<-outer(x,y,my.fun) contour(x,y,z,level=0)
That will produce a graph that looks like the figure on the left, whereas the figure on the right was created with the function I wrote called imp.solve().
my.fun <- function(x,y){y^2+x^2 -1} to.plot <- imp.solve(x,y,my.fun) #Plot with ggplot ggplot(to.plot,aes(x=x,y=y))+geom_point()+xlim(range(x))+ylim(range(y))
the output from the outer() function and returns a dataframe you can easily plot in ggplot2. It can handle most polynomial functions, although I’ll leave you in charge of ordering your own output for line plots in scenarios with multiple x’s at the same y point. Here’s another example with a cubic function. \[x^3+y^3-1=0\]
An ecological example
So why does this matter in ecology? Often you might want to create a surface plot for transitions between stable states and unstable states in with a discrete time equation. Here’s an example from my own work. I’m trying to recreate a figure such as this from a paper by Aviles 1999. Here is the figure at right. The function is a modified Ricker equation: \[N_{t+1}={N_t}^{(1+\gamma)}e^{(-c+N_t)}e^r\] Here gamma is the allee effect term, c is the strength of density dependence and r is the population growth rate. Both gamma and r are bifurcation points and we can use an implicit function to draw the boundaries between say stability and extinction at a constant c. So while implicit functions aren’t the most common thing to come across in ecology, they do arise with some mathematical models. In this example the transition between stability and extinction is given by the equation: \[ce^{(\gamma - r)}-\gamma=0\] Holding c constant at .01, the following code will generate our figure.
##########First set up the parameters to vary over######## y<-seq(0.01,.9,length=1000) x<-seq(-2,8,length=1000) ###################Define the function############ my.fun <- function(x,y){.01*exp((y-x)/y)-y } to.plot <- imp.solve(x,y,my.fun) ggplot(to.plot,aes(x=x,y=y))+geom_line()This is the same as the the first line in the above figure from the paper. You can find the full code for this function over at my github page as Imp.solve.R. So while a previously quick and dirty solution existed in R, here is an improved version with greater plotting flexibility. Happy implicit function graphing!