Rのggplot2で回帰直線の作図

ggplotで複数のグラフを重ねるときはlayer()を使う


データ ev.csv
car manufacturer price distance
アリア 日産 540 470
サクラ 日産 240 180
リーフe+ 日産 525 450
Model 3 テスラ 536 565
Model Y テスラ 583 595
e-208 プジョー 460 395
e-2008 プジョー 565 380
MX-30EV マツダ 451 256
ekクロス EV 三菱 254 180
Honda e ホンダ 495 259
ソルテラ スバル 594 487
UX300e レクサス 580 367
EQS メルセデス・ベンツ 1578 700
EQA メルセデス・ベンツ 782 423
C40 Recharge ボルボ 659 484
XC40 Recharge ボルボ 639 484
500e フィアット 473 335
iX BMW 1160 455

以下では2種類の方法で回帰直線を作図している(両者は重なっているので一つの直線しか表示されない)
方法1:デフォルトのstat_smooth(method='lm)を使う
方法2:lm()コマンドで回帰係数を求める

d <- read.csv('ev.csv')

model <- lm(distance ~ price, d)
b <- model$coefficients[1]
a <- model$coefficients[2]

x <- 1:1600
y <- a*x + b
df <- data.frame(x,y)

library(ggplot2)
ggplot()+
  layer(data = d,
        mapping=aes(x=price,y=distance,color=manufacturer),
        geom='point',
        stat='identity',
        position='identity'
  )+
  layer(data = d, 
        mapping=aes(x=price,y=distance),
        geom='line',
        stat='smooth',
        position='identity',
        params = list(method = "lm", se = F)
  )+
  layer(data = df,
        mapping=aes(x=x,y=y),
        geom='line',
        stat='identity',
        position='identity',
        )+
  theme_gray (base_family = "HiraKakuPro-W3")