r - 如何使用 ggplot2 在 R 中为多个 y 系列制作线性拟合线?

标签 r ggplot2 plot

这是我第一次涉足 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) 添加到主 block 时,我最终得到一个“Error in 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

我寻找灵感的地方包括:

我相当确定这应该是一个简单的问题,但我还不明白。我错过了什么?

我正在使用的数据文件托管在这里: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)

输出:

enter image description here

或者,如果您想要分面(每个组的图),请尝试下一个代码:

#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)

输出:

enter image description here

在 github 上探索您的数据之后,也许您正在寻找这个(提示:将数据 reshape 为长期保存的数据点):

#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)

输出:

enter image description here

或者更好的多面解决方案:

#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)

输出:

enter image description here

使用的一些数据:

#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))

关于r - 如何使用 ggplot2 在 R 中为多个 y 系列制作线性拟合线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64213270/

相关文章:

python - 在使用 pandas plot 方法创建的图表上格式化 x 轴

Python Matplotlib - 显示与标绘点相关的刻度线

r - R 中用拼写正确的单词替换拼写错误的单词的函数?

r - 优化 R 中的 Apply() While()

r - facet_grid问题: input string 1 is invalid in this locale?

r - 如何创建具有负轴和正轴的散点图?

python - Pandas 绘制 3 个变量的图

r - 防止意外的时区转换

r - 如何使用数据框中的当前行值检查前一行值

r - 在 R 中以数据无关的方式添加文本注释