4

I am working on some viscosity experiments and I'm trying to make an Eyring plot with ν vs. θ. When I create the plot with ggplot2 I can't get my model displayed.

These are the values used:

> theta [1] 25 30 35 40 45 > nu [1] 1.448462 1.362730 1.255161 1.167408 1.083005 

Here I create the plot with my values from above:

plot <- ggplot()+ geom_point(mapping = aes(theta, nu), colour = "#0072bd", size = 4, shape = 16)+ theme_bw()+ labs( x = expression(paste(theta, " ", "[°C]")), y = expression(paste("ln(", nu, ")", " ", "[mPa*s]")))+ ylim(0, 10)+ xlim(0, 100) 

That's what the plot looks like.

Now, I add my model with geom_smooth()

plot + geom_smooth( method = "nls", method.args = list(formula = nu~a*exp(b/theta), start=list(a=1, b=0.1))) 

But nothing happens... Not even an error message and the plot looks just the same as before.

I also tried to put the formula directly as a geom_smooth() argument and the start values as well,

plot + geom_smooth( method = "nls", formula = nu~a*exp(b/theta), start=list(a=1, b=0.1)) 

but then I get the

Error:Unknown parameter: start

Can anyone find the mistake I'm making?

Thanks in advance!

Cheers

EDIT

When separating the aesthetics mapping,

plot <- ggplot()+ aes(theta, nu)+ geom_point(colour = "#0072bd", size = 4, shape = 16)+ theme_bw()+ labs( x = expression(paste(theta, " ", "[°C]")), y = expression(paste("ln(", nu, ")", " ", "[mPa*s]")))+ ylim(0, 10)+ xlim(0, 100) 

I get the following error (and still nothing changes):

Warning message:

1: In min(x) : no non-missing arguments to min; returning Inf 2: In max(x) : no non-missing arguments to min; returning -Inf 3: Computation failed in stat_smooth(): $ operator is invalid for atomic vectors

6
  • 1
    What happens when you try nls outside of ggplot? Commented Apr 13, 2016 at 20:44
  • @oshun nls outside seems to work: > nls(nu~a*exp(b/theta), start=list(a=1, b=0.1)) gives me the following: Nonlinear regression model model: nu ~ a * exp(b/theta) data: parent.frame() a b 0.6153 22.7767 residual sum-of-squares: 0.008136 Number of iterations to convergence: 5 Achieved convergence tolerance: 3.11e-06 Commented Apr 13, 2016 at 20:50
  • 1
    Put your aesthetics mapping separately instead of including it in geom_point, i.e. ggplot()+aes(theta, nu)+geom_point(colour = "#0072bd", size = 4, shape = 16)+.... Then you'll get your error message. Commented Apr 13, 2016 at 20:53
  • If I'm remembering correctly, I think formula in geom_smooth expects you to refer to x and y rather than the actual names of the variables Commented Apr 13, 2016 at 20:56
  • @lukeA thanks for the hint. See the edited post for the result. Commented Apr 13, 2016 at 21:11

2 Answers 2

7

You have several things going on, many of which were pointed out in the comments.

Once you put your variables in a data.frame for ggplot and define you aesthetics either globally in ggplot or within each geom, the main thing going on is that the formula in geom_smooth expects you to refer to y and x instead of the variable names. geom_smooth will use the variables you mapped to y and x in aes.

The other complication you will run into is outlined here. Because you don't get standard errors from predict.nls, you need to use se = FALSE in geom_smooth.

Here is what your geom_smooth code might look like:

geom_smooth(method = "nls", se = FALSE, method.args = list(formula = y~a*exp(b/x), start=list(a=1, b=0.1))) 

And here is the full code and plot.

ggplot(df, aes(theta, nu))+ geom_point(colour = "#0072bd", size = 4, shape = 16)+ geom_smooth(method = "nls", se = FALSE, method.args = list(formula = y~a*exp(b/x), start=list(a=1, b=0.1))) + theme_bw()+ labs( x = expression(paste(theta, " ", "[°C]")), y = expression(paste("ln(", nu, ")", " ", "[mPa*s]")))+ ylim(0, 10) + xlim(0, 100) 

enter image description here Note that geom_smooth won't fit outside the range of the dataset unless you use fullrange = TRUE instead of the default. This may be pretty questionable if you only have 5 data points.

ggplot(df, aes(theta, nu))+ geom_point(colour = "#0072bd", size = 4, shape = 16)+ geom_smooth(method = "nls", se = FALSE, fullrange = TRUE, method.args = list(formula = y~a*exp(b/x), start=list(a=1, b=0.1))) + theme_bw()+ labs( x = expression(paste(theta, " ", "[°C]")), y = expression(paste("ln(", nu, ")", " ", "[mPa*s]")))+ ylim(0, 10) + xlim(0, 100) 

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

2

I just wrote this answer as @lukeA made the comment.

df<- data.frame(theta = c(25, 30, 35, 40, 45), nu = c( 1.448462, 1.362730, 1.255161, 1.167408, 1.083005)) myModel <- nls(nu~a*exp(b/theta), data=df, start=list(a=1, b=0.1)) myPredict <- expand.grid(theta = seq(5, 100, by =0.1)) #expand.grid here in case your model has more than one variable #Caution, extrapolating well beyond the data myPredict$fit <- predict(myModel, newdata= myPredict) plot + geom_line(data = myPredict, aes(x= theta, y= fit)) 

enter image description here

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.