Nucore
Nucore

Reputation: 69

Draw regression in a hexbinplot

hexbin already has an option to create a regression line, which is type="r".

Unfortunately, it seems like you can't make the regression line thicker or change the color, that's why you can't see the line well. That's why I have to create one myself.

My question is, how I can plot a selfmade regression line with hexbinplot, so it lays at the same place like the regression line of hexbinplot, which I get with type="r"?

library(hexbin)

    hexbinplot(TS.GW.fieldmean.zoo$GW.2~TS.MW.fldmean.1000.zoo$MW.fldm.2, main = "Scatterplot: Tägliches Mittel",
               xlab="name1 [m/s]",ylab="name2 [m/s]",style="nested.centroids", type="r")
    hbin <- hexbin(TS.GW.fieldmean.zoo$GW.2,TS.MW.fldmean.1000.zoo$MW.fldm.2)
    hvp <- hexViewport(hbin)
    reg <- lm(TS.GW.fieldmean.zoo$GW.2~TS.MW.fldmean.1000.zoo$MW.fldm.2)
    hexVP.abline(hvp,reg$coefficients[1], reg$coefficients[2], col = "red", lty = 1, lwd = 2)

Here is the plot of the selfmade regression line in red and the black line, I get while using type="r":

image

Upvotes: 0

Views: 1612

Answers (1)

Nucore
Nucore

Reputation: 69

Found the answer myself. It's so easy:

hexbinplot(yy~xx, main = "(text) Scatterplot: text",
           xlab="wind",ylab="other wind",style="nested.centroids", type=c("r"), col.line = "yourcolor", lwd="3")

Upvotes: 1

Related Questions