smooth绘制虚线回归线

smooth绘制虚线回归线

本文介绍了用ggplot2中的geom_smooth绘制虚线回归线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

pre $ library $ g $ b $ ggplot ))+
geom_point()+
geom_smooth(method =lm,se = FALSE)+
theme_bw()

返回我想要的内容,但带有一条实线:

),linetype是geom_smooth理解的美学之一。

因此,您可以调整为使用 geom_smooth(method =lm,se = FALSE,linetype =dashed)
$ p $ library(ggplot2)
ggplot(mtcars,aes(x = hp,y = mpg)) +
geom_point()+
geom_smooth(method =lm,se = FALSE,linetype =dashed)+
theme_bw()


I have a simple plot in ggplot2 and want to add a dashed regression line. So far I have:

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

Which returns what I want, but with a solid line:

I want to make the line dashed. I think I should use scale_linetype_manual() but my attempts have been hacky.

A simple question, but I couldn't find a duplicate.

解决方案

As per the help page (see ?geom_smooth), linetype is one of the aesthetics geom_smooth understands.

So, you can adjust to use 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()

这篇关于用ggplot2中的geom_smooth绘制虚线回归线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 20:26