geom_smooth does not appear on ggplot - r

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

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)
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)

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))

Related

Problem with plotting regression line from spline in ggplot2

I have a dataset where I have bacterial carriage by age group. I used the bs spline function to model bacterial carriage by age.
fit2 <- lm(percentage ~ bs(Age_midpoint, knots = c(0.177, 1, 1.460, 2.585, 5, 8.75, 37.45)), data = Complete_data)
fit2
age <-range(Complete_data$Age_midpoint)
Age_grid <- seq(from=min(age), to = max(age))
pred2 <-predict(fit2, newdata = list(Age_midpoint = Age_grid), se = T)
se_bands = with(pred2, cbind("upper" = fit+2*se.fit, "lower" = fit-2*se.fit))
I now want to plot the line that describes fit2 in the following plot:
Fig1 <- ggplot(Complete_data, aes(x= Age_midpoint, y = percentage)) + geom_point(shape=1, aes( size = denominator)) + xlab("Average age") + ylab("Pneumococcal Carriage") + theme_light()
I am able to plot the line by itself using this:`
ggplot() + geom_line(aes(x = Age_midpoint.grid, y=pred2$fit), color = "red") +
geom_ribbon(aes(x = Age_midpoint.grid, ymin=se_bands[, "lower"], ymax = se_bands[, "upper"]), alpha = 0.3) + xlim(age)
But when I try to plot the line in the scatterplot I get a warning
Fig1 + geom_line(aes(x = Age_midpoint.grid, y=pred2$fit), color = "red") +
geom_ribbon(aes(x = Age_midpoint.grid, ymin=se_bands[, "lower"], ymax = se_bands[, "upper"]), alpha = 0.3) + xlim(age)
Error in geom_line(aes(x = Age_midpoint.grid, y = pred2$fit), color = "red") :
ℹ Error occurred in the 2nd layer.
Caused by error in `check_aesthetics()`:
! Aesthetics must be either length 1 or the same as the data (403)
✖ Fix the following mappings: `x` and `y`
How can I plot the line in my scatterplot?
I looked online for solution, but I only found information about histograms.

How to find slopes of multiple regression?

I'm making a plot of several linear regressions and I would like to find the slope of each of them. The problem is that I don't find how to do it in my case.
Like you can see on my plot, I'm testing the weight as a function of the temperature, a quality (my two colors) and quantity (my facet wrap).
My code for this plot is that :
g = ggplot(donnees_tot, aes(x=temperature, y=weight, col = quality))+
geom_point(aes(col=quality), size = 3)+
geom_smooth(method="lm", span = 0.8,aes(col=quality, fill=quality))+
scale_color_manual(values=c("S" = "aquamarine3",
"Y" = "darkgoldenrod3"))+
scale_fill_manual(values=c("S" = "aquamarine3",
"Y" = "darkgoldenrod3"))+
scale_x_continuous(breaks=c(20,25,28), limits=c(20,28))+
annotate("text", x= Inf, y = - Inf, label =eqn, parse = T, hjust=1.1, vjust=-.5)+
facet_wrap(~quantity)
g
Also, if you have a tips to write them on my plot, I would be really grateful !
Thank you
By using the ggpmisc package, I've had these lines to my code and it works !
stat_poly_line() +
stat_poly_eq(aes(label = paste(after_stat(eq.label),
after_stat(rr.label), sep = "*\", \"*"))) +

Transfer calculated value from stat_smooth to other geom like linerange

I have a question about ggplot2.
I want to connect data point with ols result via vertical line, like the code listed below.
Can I transfer ..y.., the value calculated by stat_smooth, to geom_linerange directly?
I tried stat_smooth(..., geom = "linerange", mapping(aes(ymin=pmin(myy, ..y..), ymax=pmax(myy,..y..)) but it is not the result I want.
library(ggplot2)
df <- data.frame(myx = 1:10,
myy = c(1:10) * 5 + 2 * rnorm(10, 0, 1))
lm.fit <- lm("myy~myx", data = df)
pred <- predict(lm.fit)
ggplot(df, aes(myx, myy)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE) +
geom_linerange(mapping = aes(ymin = pmin(myy, pred),
ymax = pmax(myy, pred)))
stat_smooth evaluates the values at n evenly spaced points, with n = 80 by default. These points may not coincide with the original x values in your data frame.
Since you are calculating predicted values anyway, it would probably be more straightforward to add that back to your data frame and plot all geom layers based on that as your data source, for example:
df$pred <- pred
ggplot(df, aes(myx, myy)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE) +
geom_linerange(aes(ymin = myy, ymax = pred))

Stat_function on ggplot doesn't work

I have a problem with ggplot, especially with stat_function.
I want to plot my distribution, then I want to calculate the Chi-square theoretical distribution and plot it on my actual plot.
Without ggplot, I made this with a simple lines()
res.lk <- apply (as.matrix(baf_matrix$res.fst), 2, FUN = function (x) baf_matrix$res.fst*4 / (mean(baf_matrix$res.fst)))
hist(res.lk, probability = T, breaks = 100,main = "LK distribution",
xlab ="LK")
x = c(0:as.integer(max(res.lk)))
lines(x,dchisq(x,df=4),lwd=3,col="orange")
Now to make it with ggplot I do this:
Y = as.data.frame(res.lk)
ggplot(data = Y , aes( x = Lk )) + geom_histogram( binwidth=.1, color="black", fill="white" ) + stat_function( fun = dchisq(c(0:as.integer(max(res.lk))),df=4), lwd = 3, col = "orange") + theme_bw()
And I get this error : Warning message:
Computation failed in stat_function():
'what' must be a function or character string
This is what my data looks like :
The Lk distribution
I'm trying to fix it but I didn't find how. Can somebody help me please?? Thank you a lot in advance.
Note: it would really help if you included example data for us to work with.
The argument fun has to be a function. You're passing a vector. Also, because the distribution line only depends on a single value in the data, it'd be better to use annotate:
xseq <- seq(max(Y$res.lk))
ggplot(data = Y, aes(x = Lk)) +
geom_histogram(binwidth = .1, color="black", fill="white") +
annotate(
geom = "line",
x = xseq,
y = dchisq(xseq, df = 4),
width = 3,
color = "orange"
) +
theme_bw()

Geom_smooth: When groups added, number of iterations exceeded maximum of 50?

I am pretty new in modelling. I have three groups of data (by period), which I want to display by lines over scatter plot.
I figured out how to put my method and formula in geom_smooth, and I am able to display a single line.
However, when I want to add lines per group, which could be accomplished by ggplot(.., aes(..,group = period)), I've got back a Warning:
Warning message:
Computation failed in `stat_smooth()`:
number of iterations exceeded maximum of 50
and the line is not displayed.
My working code:
ggplot(tab, aes(x=distance, y=grad)) + #
geom_point() + theme_bw() +
geom_smooth(method = "nls",
formula = y ~ a*x^(-b),
method.args = list(start=c(a=20, b=0.01)), #
se = F)
results:
Code providing error (with added group = period in aes), and not displaying lines per group:
ggplot(tab, aes(x=distance, y=grad, group = period)) + #
geom_point() + theme_bw() +
geom_smooth(method = "nls",
formula = y ~ a*x^(-b),
method.args = list(start=c(a=20, b=0.01)), #
se = F)
Do you have some ideas how can I increase the number of iteration in ggplot2 by geom_smooth function?
I found some information to increase number of iteration by control=nls.control(maxiter=200) https://stat.ethz.ch/pipermail/r-help/2006-June/107606.html relative to R base modelling, but I can't find solution or directions for ggplot2.
Based on #Axeman comment, I added the control=nls.control(maxiter=200) to the
method.args = list(start=c(a=20, b=0.01),
control=nls.control(maxiter=200))
The whole script is thus:
ggplot(tab, aes(x=distance, y=grad, group = period, col = period)) + #
geom_point(col = "grey") + theme_bw() +
geom_smooth(method = "nls",
formula = y ~ a*x^(-b),
method.args = list(start=c(a=20, b=0.01),
control=nls.control(maxiter=200)), #
se = F)
And the result is:

Resources