如何在ggplot中为变量着色而不创建多条回归线? [英] How can I colour variables in a ggplot without creating multiple regression lines?

查看:36
本文介绍了如何在ggplot中为变量着色而不创建多条回归线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个ggplot,我想用它们来自的样条为数据点着色.但是,当我使用 colour = transect 参数执行此操作时,我也会为每个样条线返回一条回归线:

I'm creating a ggplot and I want to colour data points by the transect they came from. However when I do this using the colour=transect argument I end up with a regression line for each transect as well:

这是我的代码:

ggplot(data=leaf.data, 
       aes(x=distance.from.ecotone..m., y=mean.herbivory....,colour=transect)) +
  geom_point() +
  geom_smooth(method = "lm", na.rm = TRUE, fullrange= TRUE, aes=(group=1))+
  labs(x="Distance from Ecotone (m)", y="Mean Herbivory per Tree (%)",
       title="Herbivory as a Function of Distance from an Ecotone")

推荐答案

这可以通过将 color 设置为 geom_point 层的局部外观来实现:

This could be achieved by making color a local asthetic of the geom_point layer:

library(ggplot2)

set.seed(42)
leaf.data <- data.frame(
  distance.from.ecotone..m. = runif(30, 0, 30),
  mean.herbivory.... = runif(30, -5, 15),
  transect = factor(sample(1:5, 30, replace = TRUE))
)

ggplot(data=leaf.data, aes(x=distance.from.ecotone..m., y=mean.herbivory....)) +
  geom_point(aes(colour=transect)) +
  geom_smooth(method = "lm", na.rm = TRUE, fullrange= TRUE)+
  labs(x="Distance from Ecotone (m)", y="Mean Herbivory per Tree (%)",
       title="Herbivory as a Function of Distance from an Ecotone")
#> `geom_smooth()` using formula 'y ~ x'

这篇关于如何在ggplot中为变量着色而不创建多条回归线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆