【问题标题】:Plot dashed regression line with geom_smooth in ggplot2在 ggplot2 中用 geom_smooth 绘制虚线回归线
【发布时间】:2015-12-12 03:12:56
【问题描述】:

我在 ggplot2 中有一个简单的绘图,并想添加一条虚线回归线。到目前为止,我有:

library(ggplot2)
ggplot(mtcars, aes(x = hp, y = mpg)) +
  geom_point() +
  geom_smooth(method = "lm", se = FALSE) +
  theme_bw()

返回我想要的,但用实线:

我想让这条线变成虚线。我想我应该使用scale_linetype_manual(),但我的尝试很老套。

一个简单的问题,但我找不到重复的问题。

【问题讨论】:

  • linetype 是 geom_smooth 理解的美学之一。所以,你可以调整使用geom_smooth(method = "lm", se = FALSE, linetype="dashed")
  • @Jota 做到了。您可以将其发布为答案吗?

标签: r ggplot2


【解决方案1】:

根据帮助页面(参见?geom_smooth),线型是 geom_smooth 理解的美学之一。

所以,你可以调整使用geom_smooth(method = "lm", se = FALSE, linetype="dashed")

library(ggplot2)
ggplot(mtcars, aes(x = hp, y = mpg)) +
  geom_point() +
  geom_smooth(method = "lm", se = FALSE, linetype = "dashed") +
  theme_bw()

【讨论】:

  • 也适用于linetype = 2。要查看不同类型的线条,请尝试linetype = 3linetype = 4 等。
猜你喜欢
  • 2018-10-15
  • 1970-01-01
  • 2017-07-02
  • 1970-01-01
  • 2014-07-06
  • 2015-01-15
  • 2013-06-05
  • 2020-10-07
  • 1970-01-01
相关资源
最近更新 更多