Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
669 views
in Technique[技术] by (71.8m points)

r - Adding loess regresion line on a hexbin plot

I have been trying to find method to add a loess regression line on a hexbin plot. So far I do not have any success... Any suggestions?

My code is as follow:

bin<-hexbin(Dataset$a, Dataset$b, xbins=40)

plot(bin, main="Hexagonal Binning",
     xlab = "a", ylab = "b",
     type="l")

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I would suggest using ggplot2 to build the plot.

Since you didn't include any example data, I've used the palmerpenguins package dataset for the example below.

library(palmerpenguins) # For the data
library(ggplot2)        # ggplot2 for plotting



ggplot(penguins, aes(x = body_mass_g, 
                     y = bill_length_mm)) + 
  geom_hex(bins = 40) + 
  geom_smooth(method = 'loess', se = F, color = 'red') 

Created on 2021-01-05 by the reprex package (v0.3.0)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...