将自定义坡度和截距添加到R中的geom_平滑

将自定义坡度和截距添加到R中的geom_平滑,r,ggplot2,R,Ggplot2,我正在尝试添加一条具有自定义截距和坡度的线。我知道我可以使用geom_abline,但线条超出了绘图的边距。 我有以下数据 >table intent observed true 0 0.00 0.07 .1-.3 0.19 0.19 .4-.6 0.51 0.41 .7-.9 0.79 0.48 1 1.00 0.53 这是dput() 这是我目前的解决方案 table %>% ggplot(ae

我正在尝试添加一条具有自定义截距和坡度的线。我知道我可以使用
geom_abline
,但线条超出了绘图的边距。 我有以下数据

>table 
intent observed true
      0     0.00 0.07
  .1-.3     0.19 0.19
  .4-.6     0.51 0.41
  .7-.9     0.79 0.48
      1     1.00 0.53
这是
dput()

这是我目前的解决方案

table %>% 
  ggplot(aes(y=true,x=observed))+
  geom_point()+
  geom_smooth(method = lm,se=F,color="black",lty=2,size=1/2)+
  geom_abline(intercept=0.07, slope=0.599,size=1/2)


问题在于
geom_abline
是一种参考线。因此,它超出了接近0的绘图边距,并且在x轴上超过0.8时不完全可见,这与
geom_smooth
相反,它在绘图区域中保持一条直线。如何使我的
geom\u线
geom\u平滑中工作
,使其适合打印区域。

您可以使用
geom\u段()

用线性方程计算
yend
参数:y=0.07+x0.599

0.07 + 0.599
[1] 0.669

你能不能
dput()
你的数据?更新了我的答案,请检查。如果你在上面的代码中添加
geom\u abline(截距=0.07,斜率=0.599,大小=1/2)
,你会看到这些线不会相互重叠。太好了!我还添加了
+scale_y__continuous(limits=c(0.7))
,这样末端将位于x=1
library(ggplot2)
  ggplot(table, aes(y = true, x = observed)) + 
  geom_point() + 
  geom_smooth(method = lm, se = F, color = "black", lty = 2, size = 1 / 2) + 
  geom_segment(x = 0, y = 0.07, xend = 1, yend = 0.669, size = 1 / 2) +
  scale_y_continuous(limits = c(0, 0.7))
0.07 + 0.599
[1] 0.669