I have this following data frame and I calculate a fit line (p) based on logarithmic values of X and Y.
library(ggplot2)
df <- data.frame (Xvalue=c(0.05, 1, 300, 500, 800),
Yvalue=c(90, 100, 103, 105, 92))
fit_df <- lm(log(Yvalue) ~ log(Xvalue), data = df)
p <- coef(fit_df)
Standarderror <- summary(fit_df )$coefficients
Next I want to show the scatter plot of the data, as well as the fitted line, in a logarithmic scale for both X and Y axis. However, this does not work, as the following code just generates the scatter plot and not the fit line.
ggplot() +
scale_x_log10() +
scale_y_log10() +
geom_point(data = df, aes(x = Xvalue, y = Yvalue), colour = "blue", size = 5) +
geom_abline(intercept = 95.5375152316, slope = 0.007552 , color = "red")
There should be a problem in the geom_abline, because as I remove the logarithmic scale of the graph, it shows the line (of course the line here is not correct, but the point is that ggplot can show it).
ggplot() +
geom_point(data = df, aes(x = Xvalue, y = Yvalue), colour = "blue", size = 5) +
geom_abline(intercept = 95.5375152316, slope = 0.007552 , color = "red")
Now I have a couple of questions regarding this code:
- What's the problem with geom_abline? How can I should the fit line in the logarithmic axis?
- How can I add ribbons to the fit line, to show how well my data is around the fit line? I assume standard error of the fit as added to below and above the line, right?
- General question: I want to repeat this procedure for a couple of more datasets, and show the final result in one graph. For this purpose, is it better to seperate the data (as I did now) and then just add different layers in ggplot or is there a smarter way?


You don't need to precalculate the intercept and slope. Instead, use
geom_smooth, being sure you setmethod = 'lm'The easiest way to do this is probably to plot
log(Xvalue)againstlog(Yvalue). That way, your formula for thelmis simplyy ~ x. To give yourself logarithmic axes, you can make breaks at the log of the values you want to see on each axis, and simply label them as the non-logged values.