首页 > 解决方案 > Create lines parallel to my `geom_smooth(method = lm)` line

问题描述

library(tidyverse)
ggplot(mpg, aes(cty, hwy)) + 
  geom_point() + 
  geom_smooth(method = lm)

I would like two purple lines to run parallel to my geom_smooth(method = lm) line. Each line should be 10 units away from the geom_smooth(method = lm) line. One line would be above, and the other line below the geom_smooth(method = lm) line.

How do I accomplish this?

标签: rggplot2

解决方案


正如这个答案所暗示的那样,似乎没有一种自然的方式来转移一条线。然后我们可以做的是使用geom_smooth具有不同偏移量的多个:

ggplot(mpg, aes(cty, hwy)) + 
  geom_point() + 
  lapply(c(-10, 0, 10), function(o)
    geom_smooth(method = lm, formula = y + o ~ x))

在此处输入图像描述


推荐阅读