r - 如何使用 ggplot2 在 R 中为多个 y 系列制作线性拟合线?
问题描述
这是我第一次涉足 ggplot2,我遇到了困难。我正在尝试针对递增的 x 轴绘制两个随机数序列,同时显示两者的线性回归。到目前为止,我已经成功绘制了散点图,但回归线不断抛出错误。我知道这是可能的,但我错过了执行这个想法的一些东西。我正在运行 RStudio 桌面版 1.3.1056,加载了 tidyverse 的 Water Lily。
我知道这可以显示散点图(如果建议,我愿意接受更优雅的变体):
ggplot(a, aes(x = Datapoint, y = value, color = variable)) + # Setup
geom_point(aes(y = Series1, col = 'Series1')) + # Series 1 plot
geom_point(aes(y = Series2, col = 'Series2')) + # Series 2 plot
labs(title = 'example', xlab = 'Datapoint', ylab = 'Datapoint Value') # Title and axes labels
我也知道如果只使用一个 y 系列,这可以显示线性回归线:
ggplot(a) +
aes(x = Datapoint, y = value, color = variable) +
geom_point()
当我尝试将geom_smooth()
或添加geom_smooth(method = lm)
到主块时,我最终收到“FUN 中的错误(X[[i]],...):找不到对象'值'”消息。例如,这个:
ggplot(a, aes(x = Datapoint, y = value, color = variable)) + # Setup
geom_point(aes(y = Series1, col = 'Series1')) + # Series 1 plot
geom_point(aes(y = Series2, col = 'Series2')) + # Series 2 plot
labs(title = 'example', xlab = 'Datapoint', ylab = 'Datapoint Value') + # Title and axes labels
geom_smooth(method = lm)
结果是:
> ggplot(a, aes(x = Datapoint, y = value, color = variable)) + # Setup
+ geom_point(aes(y = Series1, col = 'Series1')) + # Series 1 plot
+ geom_point(aes(y = Series2, col = 'Series2')) + # Series 2 plot
+ labs(title = 'example', xlab = 'Datapoint', ylab = 'Datapoint Value') + # Title and axes labels
+ geom_smooth(method = lm)
Error in FUN(X[[i]], ...) : object 'value' not found
我寻找灵感的一些地方包括:
- 使用 ggplot2 对 R 中的数据集进行多元线性回归
- https://www.sixhat.net/how-to-plot-multpile-data-series-with-ggplot.html
- http://www.sthda.com/english/wiki/ggplot2-scatter-plots-quick-start-guide-r-software-and-data-visualization
- http://www.cookbook-r.com/Graphs/Scatterplots_(ggplot2)/
我相当肯定这应该是一个简单的问题,但我还不明白。我错过了什么?
我正在使用的数据文件托管在这里:https ://github.com/davidmvermillion/Chart_Comparisons/blob/master/Seeded_Values_for_Comparison_Project.csv
这是我当前的 R 文件:https ://github.com/davidmvermillion/Chart_Comparisons/blob/master/ggplot2Demo.R
谢谢!
解决方案
试试这个例子。我相信接近你想要的。请下次包含数据,以便使用dput()
. 您的数据中似乎缺少某些变量,或者您放置了错误的名称。这个例子可能是一个很好的起点(还包括一些使用来自 github 的真实数据的解决方案):
library(tidyverse)
#Data
data("iris")
#Code for data and plot
iris %>%
ggplot(aes(x=Sepal.Length,y=Sepal.Width,group=Species,color=Species))+
geom_point()+
geom_smooth(method = 'lm',se=F)
输出:
或者,如果您想要构面(每个组的图),请尝试下一个代码:
#Code for data and plot 2
iris %>%
ggplot(aes(x=Sepal.Length,y=Sepal.Width,group=Species,color=Species))+
geom_point()+
geom_smooth(method = 'lm',se=F)+
facet_wrap(.~Species)
输出:
在 github 上探索您的数据之后,也许您正在寻找这个(提示:将数据重塑为长期保留的 DataPoint):
#Code for data and plot 3
df %>% pivot_longer(-Datapoint) %>%
ggplot(aes(x=Datapoint,y=value,color=name,group=name))+
geom_point()+
geom_smooth(method = 'lm',se=F)
输出:
或者更好的解决方案:
#Code for data and plot 4
df %>% pivot_longer(-Datapoint) %>%
ggplot(aes(x=Datapoint,y=value,color=name,group=name))+
geom_point()+
geom_smooth(method = 'lm')+
facet_wrap(.~name)
输出:
使用的一些数据:
#Data
df <- structure(list(Datapoint = 1:50, Series1 = c(37L, 7L, 26L, 27L,
91L, 77L, 58L, 87L, 58L, 13L, 62L, 91L, 18L, 18L, 23L, 61L, 90L,
26L, 2L, 54L, 27L, 30L, 52L, 39L, 3L, 37L, 32L, 43L, 28L, 6L,
50L, 50L, 71L, 45L, 37L, 19L, 84L, 61L, 46L, 51L, 39L, 95L, 16L,
27L, 28L, 89L, 54L, 98L, 98L, 61L), Series2 = c(25L, 88L, 65L,
5L, 28L, 51L, 29L, 83L, 10L, 98L, 52L, 26L, 68L, 64L, 3L, 6L,
39L, 53L, 96L, 15L, 40L, 24L, 65L, 27L, 84L, 13L, 83L, 43L, 14L,
65L, 76L, 95L, 15L, 100L, 5L, 62L, 92L, 58L, 10L, 32L, 9L, 83L,
41L, 99L, 46L, 32L, 19L, 1L, 13L, 39L)), class = "data.frame", row.names = c(NA,
-50L))
推荐阅读
- javascript - 如何重置使用 edit-json-file 编辑的文件
- javascript - 如何在另一个脚本结束后调用 javascript 函数来执行
- pointers - 交换和元组赋值的基本查询
- python - 检查 Cassandra 的结果集是否为空(Python)
- sql - SQL Server CREATE TABLE 语句
- jpa - OpenJPA 在生产数月后解析查询错误
- scala - 从计算着色器写入持久映射的 SSBO 失败
- python - 如何从父类实例化子类
- c# - 从 Dialogflow 在 Azure 中接收 webhook
- css - 如何在 Chrome iOS 上无缓存刷新