【问题标题】:Determing slope of multiple lines in the same geom_point graph in ggplot2在ggplot2中确定同一geom_point图中多条线的斜率
【发布时间】:2019-01-09 08:36:49
【问题描述】:

Columns and first rows of code 我在ggplot2 的同一个geom_point 图中有几个不同的geom_smooth(method="glm") 行。我正在寻找确定每条线的回归方程,包括斜率方程。我找到了similar post,但我仍然遇到一些问题。我的代码是:

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

提前致谢!

【问题讨论】:

  • 请附上类似帖子的链接,以供参考。查看您的数据是什么样子也会很有帮助,请提供示例。
  • 这里是链接:stackoverflow.com/questions/7549694/… 我还添加了数据的图像。对不起,第一次在这个网站上发布海报。谢谢
  • 您要准确添加哪些行?您可以根据需要添加任意数量的geom_smooth() 层。我不清楚你的问题到底是什么。
  • 我已经用该代码添加了行,我正在寻找确定每行的斜率以进行进一步的统计分析。我不能使用 r2 值,因为每行之间的样本量非常不同
  • 也就是说,您想要回归方程的详细信息?

标签: r ggplot2 glm


【解决方案1】:

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

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));                 
}

我们可以将其应用于手动定义的数据子集。可能有一种更智能的方法可以更自动地将其应用于多个组,但由于难以自动化智能标签位置,这可能就足够了。

library(ggplot2); library(dplyr)
ggplot(mtcars, aes(x=wt, y=mpg, 
                   col=as.factor(cyl), linetype=as.factor(cyl))) + 
  geom_point() + 
  geom_smooth(data = mtcars, 
              method ="glm", alpha = 0, show.legend = FALSE, linetype = 'solid') +
  annotate("text", x = 3, y = 30, label = lm_eqn(mtcars %>% filter(cyl == 4)), parse = TRUE) +
  annotate("text", x = 4.3, y = 20, label = lm_eqn(mtcars %>% filter(cyl == 6)), parse = TRUE) +
  annotate("text", x = 4, y = 12, label = lm_eqn(mtcars %>% filter(cyl == 8)), parse = TRUE)

【讨论】:

    【解决方案2】:

    应用 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")
    

    请务必注意,这些回归方程的位置将根据 YearsPostReleasePercentNative 的范围进行修改。此外,如果 FieldTypes 包含三个以上的级别,则必须添加相应的 annotate() 调用,自定义级别名称。

    【讨论】:

    • 我已更改注释以包含我的 Fieldtype 的所有五个级别。我运行该函数,它成功地将一个函数添加到我的全局环境中。然后我尝试运行更新的 ggplot 代码行并且没有创建图表,我只在控制台中看到以下内容: lm.fit 中的错误(x,y,offset = offset,singular.ok =singular.ok,.. .) : 0 (non-NA) case 有什么想法吗?
    • 您的数据中有 NA 值吗?
    • 我刚刚使用 is.na 仔细检查了 NA 并且可以确认我的数据中没有 NA 值。有没有一种方法可以让我在网站上上传我的数据以确定错误的来源?
    猜你喜欢
    • 1970-01-01
    • 2020-07-22
    • 1970-01-01
    • 2021-01-15
    • 2011-11-20
    • 1970-01-01
    • 1970-01-01
    • 2014-08-26
    • 1970-01-01
    相关资源
    最近更新 更多