脚本无法运行(terms.formula(formula,data = data)中的错误:'data'参数的类型错误

时间:2019-02-13 00:29:59

标签: r function ggplot2 regression ggpmisc

我已经多次运行了该脚本,并且一直工作到今天早上,突然产生了错误消息:

  

(术语错误。formula(formula,data = data):'data'参数为   错误的类型。

我没有做任何更改,我需要找出为什么它突然似乎不起作用的原因。以前对类似问题的解答没有帮助。

我的数据:

DPUT(harvest2)
structure(list(Year = c(1971, 1972, 1973, 1974, 1975, 1976, 1977, 
1978, 1979, 1980, 1981, 1982, 1983, 1984, 1985, 1986, 1987, 1988, 
1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 
2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 
2011, 2012, 2013, 2014, 2015, 2016), Count = c(750, 757, 592, 
693, 667, 757, 719, 670, 733, 796, 923, 921, 944, 1010, 825, 
762, 825, 844, 809, 830, 768, 823, 749, 675, 700, 637, 708, 697, 
754, 694, 636, 717, 786, 731, 769, 732, 710, 610.5, 593, 529, 
664, 788, 731, 644, 653, 656), SexRat = c(1.91812865497076, 2.34567901234568, 
1.69178082191781, 1.46766169154229, 1.30396475770925,     
1.4364406779661, 1.32098765432099, 1.48584905660377, 1.5906976744186, 
1.91414141414141, 1.48905109489051, 1.61382113821138, 1.52380952380952, 
1.87777777777778, 1.75438596491228, 1.6695652173913, 1.81566820276498, 
1.79295154185022, 1.85024154589372, 1.75446428571429, 1.83163265306122, 
1.92857142857143, 1.76635514018692, 1.5, 2.26190476190476,     1.76704545454545,
2.38125, 1.80924855491329, 2.33333333333333, 1.81182795698925, 
2.20446096654275, 2.02790697674419, 2.1140350877193, 2.05, 2.20183486238532, 
1.90983606557377, 2.02262443438914, 1.75116279069767, 1.86842105263158, 
1.87951807228916, 2.08542713567839, 2.01724137931034, 1.95833333333333, 
1.81165919282511, 2.12135922330097, 1.97260273972603)), class = "data.frame", 
row.names = c(NA, -46L))

我的脚本:

# Function for the equation

lm_eqn = function(df){
  m = lm(y ~ poly(x, 3), df) #3rd degree polynomial
  eq <- substitute(italic(y) == a + b %.% italic(x)*","~~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 = 4)))

  as.character(as.expression(eq))
}

# Make the plot

library(ggplot2)
ggplot(harvest2, aes(x = Year, y = Count)) +  
  scale_y_continuous(minor_breaks = seq(500, 1100, by = 50), 
                     breaks = seq(500, 1100, by = 100),
                     limits = c(500, 1100), expand = c(0, 0)) +  
  scale_x_continuous(minor_breaks = seq(1970, 2018, by = 1), 
                     breaks = seq(1970, 2018, by = 5), limits = c(1970, 2018)) +
  geom_point(stat = 'identity', size=2) +
  stat_smooth(method = "lm", se = TRUE, fill = NA, size = 1.3,
              formula = y ~ poly(x, 3, raw = TRUE), col = "red") +
  annotate("text", x = 1975, y = 1075, label = lm_eqn(df), 
           hjust = 0, size = 3.5, parse = TRUE) +
  xlab(" ") + 
  ylab("Count") +
  theme_light() +
  ggtitle(" ")

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:2)

如何使用stat_poly_eq包中的ggpmisc?如果要将方程式和R2分成两行,请参见this

library(ggplot2)
library(ggpmisc)

# define formula
formula1 <- y ~ poly(x, 3, raw = TRUE)

ggplot(harvest2, aes(x = Year, y = Count)) +
  scale_y_continuous(
    minor_breaks = seq(500, 1100, by = 50), breaks = seq(500, 1100, by = 100),
    limits = c(500, 1100), expand = c(0, 0)) +
  scale_x_continuous(
    minor_breaks = seq(1970, 2018, by = 1), breaks = seq(1970, 2018, by = 5),
    limits = c(1970, 2018)) +
  geom_point(stat = "identity", size = 2) +
  stat_smooth(
    method = "lm", se = TRUE, fill = NA, size = 1.3,
    formula = formula1, col = "red") +
  # show the equation and R2
  stat_poly_eq(aes(label = paste(..eq.label.., ..rr.label.., sep = "~~~~")),
    label.x.npc = "left", label.y.npc = "top",
    formula = formula1, parse = TRUE, size = 5) +
  xlab(" ") + ylab("Count") +
  theme_light() +
  ggtitle(" ")

reprex package(v0.2.1.9000)于2019-02-12创建

答案 1 :(得分:0)

@Tung的答案解决了这个问题,Tung的注释暗示了原始代码中的错误。我在下面对此进行扩展。

最可能的问题是在下面的语句中使用了df

annotate("text", x = 1975, y = 1075, label = lm_eqn(df), hjust = 0, size = 3.5, parse = TRUE)

df是参数的名称,而不是所需的参数,这应该是方程式要使绘制的曲线与传递给ggplot的数据相同:

annotate("text", x = 1975, y = 1075, label = lm_eqn(df = harvest2), hjust = 0, size = 3.5, parse = TRUE)

错误和可能的错误方程系数/ R ^ 2值将根据搜索路径中是否存在名为df的变量以及其中存储了什么对象而有所不同。

@Tung的代码通过仅定义一次模型公式并将其存储在变量中以及仅将数据作为参数传递一次,从而确保方程和绘制曲线之间不存在不匹配。