r - 如何在不创建多条回归线的情况下为 ggplot 中的变量着色?
问题描述
我正在创建一个 ggplot,我想通过它们来自的横断面为数据点着色。但是,当我使用colour=transect
参数执行此操作时,我最终也会为每个样带生成一条回归线:
这是我的代码:
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
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'
推荐阅读
- jupyter-notebook - vscode jupyter notebook报错Error: Session cannot generate requests
- extjs - 在 ComboBox MultiSelect extjs 上加载数据
- flutter - 如何在颤动中固定 SSL 证书的公钥?
- php - PHP WebDriver - JavaScriptError: Document was unloaded
- c# - Parallel.Foreach 对并发字典中一个键的值(并发包)
- android - 如何在 Android Studio 的 Scoped Storage 中移动或重命名 Android 11 中的图像、视频文件
- sql-server - Spring批量从SQL服务器读取并写入Cassandra
- reactjs - Material-UI - 我可以选择性地启用按钮波纹吗?
- python - 停止显示适合模型的报告
- python - Discord bot 回复其消息(python)