extend nonlinear regression lines

141 views
Skip to first unread message

Elizabeth Webb

unread,
Nov 15, 2012, 7:18:07 PM11/15/12
to ggp...@googlegroups.com
Hi all-

There has got to be an easy answer to this.  I would like to plot my data in ggplot2 along with an exponential line.  

In the basic R code, it looks like this:


f=function(x,alpha,beta){alpha*exp(beta*x)}

x=warming$T5cm

plot(warming$umoles60~x,data=warming)

curve(f(x,alpha= 2.7223,beta=0.21834),add=TRUE,col="red")


In ggplot2 I am trying to use the following code:

ggplot() +

geom_point(data=warming, aes(x=SoilTavg, y=umoles60),color="red",shape=1) +

geom_point(data=control, aes(x=SoilTavg, y=umoles60),color="blue",shape=1) +

geom_line(aes(x = x, y =1.3203*exp(0.21978*x)), colour = "blue", fullrange=T, ) +

geom_line(aes(x = x, y =2.7071*exp(0.2001*x)), colour = "red", fullrange=T)


unfortunately this code only makes the lines from -3 to 0 instead of -10 to 0 as I would like.  I cannot figure out a way to extend the lines.  I have tried these sites

http://perfdynamics.blogspot.com/2010/09/confidence-bands-for-universal.html

http://kohske.wordpress.com/2010/12/25/draw-function-without-data-in-ggplot2/

but I am a little too new at R to get them to work.


The whole reason I need to do this in ggplot2 is because when I export my R figure as a pdf or jpeg, etc, the colors are washed out.  When I export ggplot files, this does not happen.  Any idea why?


Thank you in advance,

Elizabeth

Dennis Murphy

unread,
Nov 15, 2012, 9:14:44 PM11/15/12
to Elizabeth Webb, ggp...@googlegroups.com
Hi:

Here's one way you could do it, following the hint shown in Kohske's blog post:

DF <- data.frame(x = runif(30, -5, -1), y = runif(30, 0, 3))
DF2 <- data.frame(x = seq(-10, 0, by = 0.1))

ggplot(DF, aes(x = x)) +
    geom_point(aes(y = y)) +
    stat_function(data = DF2, aes(x = x), 
                  fun = function(x) 2.7223 * exp(0.21834 * x), color = "blue") +
    stat_function(data = DF2, aes(x = x),
                  fun = function(x) 1.3203 * exp(0.21978 * x), color = "red") +
    stat_function(data = DF2, aes(x = x),
                  fun = function(x) 2.7071 * exp(0.2001 * x), color = "orange")

Dennis

--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

Ben Bond-Lamberty

unread,
Nov 15, 2012, 8:27:52 PM11/15/12
to Elizabeth Webb, ggp...@googlegroups.com
Elizabeth,

If you have a sample data set to share, we'll be able to give you a
much better answer. But basically, you can do something like this:

df <- data.frame(x=1:5,y=1:5)
model <- data.frame(x=-5:10,y=exp(-5:10/10))
ggplot()+geom_point(data=df,aes(x=x,y=y))+geom_line(data=model,aes(x=x,y=y),color="blue")

Also note, re your sample code below, I'd really recommend assembling
everything in a single data frame so you can do something like
+geom_point( aes( color=treatment ) ).

Ben
Reply all
Reply to author
Forward
0 new messages