如何解决ggplot中R中的趋势线
mpg %>%
mutate(Color=ifelse(class=='2seater','2seater','Other')) %>%
ggplot(aes(displ,hwy,colour = Color)) +
geom_point() +
scale_color_manual(values = c("2seater" = "#992399","Other" = "#000000"))
为此,我试图添加一条适用于所有类别的趋势线,因为如果我添加一条趋势线 geom_smooth(method="lm")
,它会分别为 2 个我不想要的座位绘制
解决方法
在对 colour
的调用中使用 group = 1
覆盖 geom_smooth
美学,即分组美学。
library(tidyverse)
mpg %>%
mutate(Color=ifelse(class=='2seater','2seater','Other')) %>%
ggplot(aes(displ,hwy,colour = Color)) +
geom_point() +
scale_color_manual(values = c("2seater" = "#992399","Other" = "#000000")) +
geom_smooth(aes(group = 1),method = "lm",formula = y ~ x)
,
geom_smooth
继承了 ggplot 的 aes
参数。您可以将“颜色”移至 geom_point,或将 inherit.aes = F
传递至 geom_smooth。
mpg %>%
mutate(Color=ifelse(class=='2seater',hwy)) +
geom_point(aes(,colour = Color)) +
scale_color_manual(values = c("2seater" = "#992399","Other" = "#000000")) + geom_smooth(method = 'lm')
#or:
mpg %>%
mutate(Color=ifelse(class=='2seater',"Other" = "#000000")) + geom_smooth(method = 'lm',inherit.aes = F,aes(displ,hwy))
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。