Add custom slope and geom_smooth interception to R

I am trying to add a line with custom intercept and tilt. I know what I can use geom_abline, but the line exceeds the graph fields. I have the following data.

>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

Here dput()

structure(list(intent = structure(c(4L, 1L, 2L, 3L, 5L), .Label = c(".1-.3", 
".4-.6", ".7-.9", "0", "1"), class = "factor"), observed = c(0, 
0.19, 0.51, 0.79, 1), true = c(0.07, 0.19, 0.41, 0.48, 0.53)), row.names = c(NA, 
-5L), class = "data.frame", .Names = c("intent", "observed", 
"true"))

This is my current solution.

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)

enter image description here

The problem is what geom_ablineis the type of reference line. As such, it exceeds the edges of the graph by about 0 and does not fully appear below 0.8 along the x axis, in contrast to geom_smoothholding the line in the graph area. How can I do my work geom_ablinein geom_smoothorder to fit the area of ​​the schedule.

+4
source share
1 answer

You can use geom_segment():

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))

Calculate the parameter yendusing the linear equation: y = 0.07 + x0.599

0.07 + 0.599
[1] 0.669
+1
source

Source: https://habr.com/ru/post/1689804/


All Articles