Search code examples
rggplot2glmpoisson

ggplot2 and geom_ribbon: Error in eval(expr, envir, enclos) : object 'Freq' not found


I am running Poisson generalized linear models on my count data, and plotting data and the fitted model with ggplot.

My data:

structure(list(YR = c(1960, 1961, 1962, 1963, 1964, 1965, 1966, 
1967, 1968, 1969, 1970, 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), Freq = c(0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 0L, 
0L, 0L, 2L, 0L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 4L, 1L, 2L, 4L, 5L, 
3L, 2L, 5L, 2L, 14L, 6L, 5L, 5L, 10L, 13L, 10L, 5L, 8L, 7L, 6L, 
10L, 12L, 14L, 2L, 16L, 15L)), .Names = c("YR", "Freq"), row.names = 58:114, class = "data.frame")

Here's the script, first the model, the parameters and the plot using geom_ribbon:

mod <- glm(Freq~YR, data = sub9, family = "poisson")

pred.df <- data.frame(YR = seq(min(sub9$YR), max(sub9$YR), length.out = 100))
pred <- predict(mod, newdata = pred.df, se.fit = TRUE)
pred.df$count <- exp(pred$fit)
pred.df$countmin <- exp(pred$fit - 2 * pred$se.fit)
pred.df$countmax <- exp(pred$fit + 2 * pred$se.fit)

ggplot(sub9,aes(x=YR,y=Freq)) +
  scale_y_continuous(limits=c(0,75),breaks=c(10,20,30,40,50,60,70),expand=c(0,0)) +
  scale_x_continuous(limits=c(1960,2018),breaks=c(1960,1965,1970,
                                              1975,1980,1985,1990,1995,
                                              2000,2005,2010,2015)) +
  geom_point() +
  geom_ribbon(data = pred.df, aes(ymin = countmin, ymax = countmax), alpha=0.3) +
  geom_line(data = pred.df) +
  xlab(" ") + ylab("Count")

After first having successfully run this procedure on one data set, I get an error message when trying the same procedure with a new and similar data set. Error message:

Error in eval(expr, envir, enclos) : object 'Freq' not found

The only thing I have done is to replace the name of the data frame and changed the name of the dependent variable from "count" to "Freq", in accordance with the data frame. What am I doing wrong?


Solution

  • This should work (you need to specify y as count in the aes):

    ggplot(sub9,aes(x=YR,y=Freq)) +
      scale_y_continuous(limits=c(0,75),breaks=c(10,20,30,40,50,60,70),expand=c(0,0)) +
      scale_x_continuous(limits=c(1960,2018),breaks=c(1960,1965,1970,
                                                      1975,1980,1985,1990,1995,
                                                      2000,2005,2010,2015)) +
      geom_point() +
      geom_ribbon(data = pred.df, aes(y=count, ymin = countmin, ymax = countmax), alpha=0.3) +
      geom_line(data = pred.df,aes(x=YR,y=count)) +
      xlab(" ") + ylab("Count")
    

    enter image description here