Frage

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 ?

War es hilfreich?

Lösung

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

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top