1

I am facing a probably pretty easy-to-solve issue: adding a log- curve to a scatter plot. I have already created the corresponding model and now only need to add the respective curve/line.

The current model is as follows:

### DATA
SpStats_urbanform <- c (0.3702534,0.457769,0.3069843,0.3468263,0.420108,0.2548158,0.347664,0.4318018,0.3745645,0.3724192,0.4685135,0.2505839,0.1830535,0.3409849,0.1883303,0.4789871,0.3979671)

co2 <- c (6.263937,7.729964,8.39634,8.12979,6.397212,64.755192,7.330138,7.729964,11.058834,7.463414,7.196863,93.377393,27.854284,9.081405,73.483949,12.850917,12.74407)

### Plot initial plot
plot (log10 (1) ~ log10 (1), col = "white", xlab = "PUSHc values", 
      ylab = "Corrected  GHG emissions [t/cap]", xlim =c(0,xaxes), 
      ylim =c(0,yaxes), axes =F)

axis(1, at=seq(0.05, xaxes, by=0.05),  cex.axis=1.1)
axis(2, at=seq(0, yaxes, by=1), cex.axis=1.1 )


### FIT
fit_co2_urbanform <- lm (log10(co2) ~ log10(SpStats_urbanform)) 


### Add data points (used points() instead of simple plot() bc. of other code parts)
points (co2_cap~SpStats_urbanform, axes = F, cex =1.3)

Now, I've already all the fit_parameters and are still not able to construct the respective fit-curve for co2_cap (y-axis)~ SpStats_urbanform (x-axis)

Can anyone help me finalizing this little piece of code ?

plannapus
  • 18,529
  • 4
  • 72
  • 94
  • 2
    You are more likely to get a helpful response if you provide a reproducible example. Here are a few tips on how to proceed. http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Roman Luštrik Jan 24 '14 at 13:32
  • Since you can retrieve the slope and intercept from your `fit_co2_urbanform` object, just use them to plot `y<- intercept +slope*log10(x)` with the `x`-values of your choice. Side note: you're going to get in trouble some day if you use 'o' instead of '0' for your "carbondioxide" variable names. – Carl Witthoft Jan 24 '14 at 14:24
  • Hey - thanks so much for your help. I have changed the example - it is now a running program. HOPEFULLY ANYONE CAN ADD THE DESIRED LOG_CURVE... THANKS SO MUCH !!!!! – user3232090 Jan 29 '14 at 14:27

1 Answers1

-1

First, if you want to plot in a log-log space, you have to specify it with argument log="xy":

plot (co2~SpStats_urbanform, log="xy")

Then if you want to add your regression line, then use abline:

abline(fit_co2_urbanform)

enter image description here

Edit: If you don't want to plot in a log-log scale then you'll have to translate your equation log10(y)=a*log10(x)+b into y=10^(a*log10(x)+b) and plot it with curve:

f <- coefficients(fit_co2_urbanform)
curve(10^(f[1]+f[2]*log10(x)),ylim=c(0,100))
points(SpStats_urbanform,co2)

enter image description here

plannapus
  • 18,529
  • 4
  • 72
  • 94
  • Hi, thanks ! This is clear to me. However, I exactly do NOT want to plot the linear regression line, but to add the LOGARITHMIC CURVE, which is resulting from the relationship between both variables. Any ideas? THANKS !!!! – user3232090 Jan 29 '14 at 15:11