R 在ggplot2中确定同一几何点图中多条直线的斜率

R 在ggplot2中确定同一几何点图中多条直线的斜率,r,ggplot2,glm,R,Ggplot2,Glm,我在ggplot2中的相同geom\u点中有几条不同的geom\u平滑(method=“glm”)线。我想确定每条线的回归方程,包括斜率方程。我找到了一份工作,但我仍然有一些问题。我的代码是: native <- read.csv("native.gather.C4C5C6C7.csv") ggplot(native, aes(x=YearsPostRelease, y=PercentNative, col=FieldType, linetype=FieldType)) + g

我在
ggplot2
中的相同
geom\u点中有几条不同的
geom\u平滑(method=“glm”)
线。我想确定每条线的回归方程,包括斜率方程。我找到了一份工作,但我仍然有一些问题。我的代码是:

native <- read.csv("native.gather.C4C5C6C7.csv")

ggplot(native, aes(x=YearsPostRelease, y=PercentNative, col=FieldType, linetype=FieldType)) + 
    geom_point(size=0.7) + 
    geom_smooth(data = native, 
                method ="glm", alpha = 0, show.legend = FALSE, linetype = 'solid') +
    scale_x_continuous(breaks = c(0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55)) +
    scale_y_continuous(limits = c(0, 100), 
                       breaks = c(0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100)) + 
    ggtitle("Percent Native Through Time")

native这里有一种使用定义的
lm_eqn
的方法。您可能遇到问题,因为您的数据与函数的预期输入不匹配。我在这里使用了
mtcars
,因为我没有你的数据,探索了mpg和wt之间的关系。下面,请注意我正在调查的关系的定制

lm_eqn <- function(df){
  m <- lm(mpg ~ wt, df);
  eq <- substitute(italic(mpg) == a + b %.% italic(wt)*","~~italic(r)^2~"="~r2, 
                   list(a = format(coef(m)[1], digits = 2), 
                        b = format(coef(m)[2], digits = 2), 
                        r2 = format(summary(m)$r.squared, digits = 3)))
  as.character(as.expression(eq));                 
}

这里有一种使用定义的
lm_eqn
的方法。您可能遇到问题,因为您的数据与函数的预期输入不匹配。我在这里使用了
mtcars
,因为我没有你的数据,探索了mpg和wt之间的关系。下面,请注意我正在调查的关系的定制

lm_eqn <- function(df){
  m <- lm(mpg ~ wt, df);
  eq <- substitute(italic(mpg) == a + b %.% italic(wt)*","~~italic(r)^2~"="~r2, 
                   list(a = format(coef(m)[1], digits = 2), 
                        b = format(coef(m)[2], digits = 2), 
                        r2 = format(summary(m)$r.squared, digits = 3)))
  as.character(as.expression(eq));                 
}

应用Jon在上面所做的贡献,您可以按如下方式为您的数据自定义此函数

同样,很难完全了解底层数据是什么样子,但是让我们假设字段FieldType包含三个因素:BSSFields、CSSFields和DSSFields

# Load data
library(tidyverse)
native <- read.csv("native.gather.C4C5C6C7.csv")

# Define function
lm_eqn <- function(df){
  m <- lm(PercentNative ~ YearsPostRelease, df);
  eq <- substitute(italic(native) == a + b %.% 
italic(YearsPostRelease)*","~~italic(r)^2~"="~r2, 
                   list(a = format(coef(m)[1], digits = 2), 
                        b = format(coef(m)[2], digits = 2), 
                        r2 = format(summary(m)$r.squared, digits = 3)))
  as.character(as.expression(eq));                 
}

# Plot data
ggplot(native, aes(x = YearsPostRelease, 
                   y = PercentNative, 
                   col = FieldType, 
                   linetype = FieldType)) +
  geom_point(size=0.7) + 
  geom_smooth(data = native, 
              method ="glm", alpha = 0, show.legend = FALSE, linetype = 'solid') +
  scale_x_continuous(breaks = c(0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55)) +
  scale_y_continuous(limits = c(0, 100), 
                     breaks = c(0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100)) + 
  annotate("text", x = 3, y = 30, 
           label = lm_eqn(native %>% filter(FieldType == "BSSFields")), parse = TRUE) +
  annotate("text", x = 4, y = 20, 
           label = lm_eqn(native %>% filter(FieldType == "CSSFields")), parse = TRUE) +
  annotate("text", x = 5, y = 10, 
           label = lm_eqn(native %>% filter(FieldType == "DSSFields")), parse = TRUE)
  ggtitle("Percent Native Through Time")
#加载数据
图书馆(tidyverse)

native应用Jon在上面的贡献,您可以按如下方式为您的数据自定义此函数

同样,很难完全了解底层数据是什么样子,但是让我们假设字段FieldType包含三个因素:BSSFields、CSSFields和DSSFields

# Load data
library(tidyverse)
native <- read.csv("native.gather.C4C5C6C7.csv")

# Define function
lm_eqn <- function(df){
  m <- lm(PercentNative ~ YearsPostRelease, df);
  eq <- substitute(italic(native) == a + b %.% 
italic(YearsPostRelease)*","~~italic(r)^2~"="~r2, 
                   list(a = format(coef(m)[1], digits = 2), 
                        b = format(coef(m)[2], digits = 2), 
                        r2 = format(summary(m)$r.squared, digits = 3)))
  as.character(as.expression(eq));                 
}

# Plot data
ggplot(native, aes(x = YearsPostRelease, 
                   y = PercentNative, 
                   col = FieldType, 
                   linetype = FieldType)) +
  geom_point(size=0.7) + 
  geom_smooth(data = native, 
              method ="glm", alpha = 0, show.legend = FALSE, linetype = 'solid') +
  scale_x_continuous(breaks = c(0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55)) +
  scale_y_continuous(limits = c(0, 100), 
                     breaks = c(0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100)) + 
  annotate("text", x = 3, y = 30, 
           label = lm_eqn(native %>% filter(FieldType == "BSSFields")), parse = TRUE) +
  annotate("text", x = 4, y = 20, 
           label = lm_eqn(native %>% filter(FieldType == "CSSFields")), parse = TRUE) +
  annotate("text", x = 5, y = 10, 
           label = lm_eqn(native %>% filter(FieldType == "DSSFields")), parse = TRUE)
  ggtitle("Percent Native Through Time")
#加载数据
图书馆(tidyverse)

本地请包括类似帖子的链接,以供参考。看看你的数据是什么样子也会很有帮助,请提供一个示例。这里是链接:我还添加了数据的图像。很抱歉,第一次在这个网站上张贴海报。谢谢你到底想添加哪些行?您可以添加任意数量的
geom_smooth()
层。我不清楚你的问题到底是什么。我已经用代码添加了行,我想确定每行的斜率,以便进行进一步的统计分析。我不能使用r2值,因为每行之间的样本大小非常不同。换句话说,你想要回归方程的详细信息吗?请包括类似帖子的链接,这有助于参考。看看你的数据是什么样子也会很有帮助,请提供一个示例。这里是链接:我还添加了数据的图像。很抱歉,第一次在这个网站上张贴海报。谢谢你到底想添加哪些行?您可以添加任意数量的
geom_smooth()
层。我不清楚你的问题到底是什么。我已经用代码添加了行,我想确定每行的斜率,以便进行进一步的统计分析。我不能使用r2值,因为每行之间的样本大小非常不同。换句话说,您想要回归方程的详细信息吗?我已更改注释,以包括我的Fieldtype的所有五个级别。我运行该函数,它成功地将函数添加到我的全局环境中。然后,我尝试运行更新的ggplot代码行,但没有创建任何图形,我在控制台中只看到以下内容:lm.fit中的错误(x,y,offset=offset,singular.ok=singular.ok,…):0(非NA)案例有什么想法吗?你的数据中有NA值吗?我刚刚用is.NA重新检查了NA,可以确认我的数据中没有NA值。是否有一种方法可以将数据上传到网站上,以确定错误的来源?我已将注释更改为包含我的Fieldtype的所有五个级别。我运行该函数,它成功地将函数添加到我的全局环境中。然后,我尝试运行更新的ggplot代码行,但没有创建任何图形,我在控制台中只看到以下内容:lm.fit中的错误(x,y,offset=offset,singular.ok=singular.ok,…):0(非NA)案例有什么想法吗?你的数据中有NA值吗?我刚刚用is.NA重新检查了NA,可以确认我的数据中没有NA值。有没有一种方法可以让我上传数据到网站上,以确定错误的来源?