Skip to content
Prev 320136 / 398503 Next

ggplot2 will not draw a rectangle. Error: ggplot2 doesn't know how to deal with data of class XXX"

Ah , thanks Sarah
So that's why the error message changed!  I was getting different one earlier.
 I had defined rect earlier and apparently, when stripping down the code I negelected to include it in the example.  Renamed rect as rectlib 

Now what I get is "Error in eval(expr, envir, enclos) : object 'federal.ps' not found" which is what I was getting ealier although at one point I was getting "year not found".
See revised code.

library(ggplot2)
  fcs  <-  structure(list(year = structure(c(954478800, 986014800, 1017550800, 
           1049086800, 1080709200, 1112245200, 1143781200, 1175313600, 1206936000, 
           1238472000, 1270008000, 1301544000, 1333166400), class = c("POSIXct", 
          "POSIXt"), tzone = ""), federal.ps = c(211925L, 223933L, 237251L, 
           242737L, 244158L, 243971L, 249932L, 254622L, 263114L, 274370L, 
           282955L, 282352L, 278092L)), .Names = c("year", "federal.ps"), 
           class = "data.frame", row.names = c(NA, -13L))
  
  rectlib  <-  data.frame (xmin  = as.POSIXct("2000-03-31", "%Y-%m-%d"),
                           xmax = as.POSIXct("2006-10-31", "%Y-%m-%d"))
  
  p  <-  ggplot(fcs, aes(year, federal.ps )) + geom_line()
  
  p  +  geom_rect(data=rectlib, aes(xmin=xmin, xmax = xmax, ymin=-Inf, ymax = Inf), 
                  fill='red', alpha=0.2)
  ###===================End Code==================================================

John Kane
Kingston ON Canada
____________________________________________________________
GET FREE SMILEYS FOR YOUR IM & EMAIL - Learn more at http://www.inbox.com/smileys
Works with AIM?, MSN? Messenger, Yahoo!? Messenger, ICQ?, Google Talk? and most webmails