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

John Kane jrkrideau at inbox.com
Fri Mar 22 22:36:47 CET 2013


Thanks, that shows that I am not making one of my "really stupid" mistakes.  

What is really annoying is that I can find examples on the web that work just fine and I cannot se how my example is that different.  I even tried changing from using as.Date() to POSIXct() and to POSIXlt in case the dates were the problem but with no luck.

I think it's time for dinner here so I will have another look at it tommorrow --hopefully someone will see the problem.

Thanks again.

John Kane
Kingston ON Canada


> -----Original Message-----
> From: sarah.goslee at gmail.com
> Sent: Fri, 22 Mar 2013 17:29:42 -0400
> To: jrkrideau at inbox.com
> Subject: Re: [R] ggplot2 will not draw a rectangle. Error: ggplot2
> doesn't know how to deal with data of class XXX"
> 
> I get "year not found" and we've exceeded my ability to debug ggplot
> code. I hope someone else chimes in: I'd like to know what the answer
> is too.
> 
> 
> Sarah
> 
> On Fri, Mar 22, 2013 at 5:01 PM, John Kane <jrkrideau at inbox.com> wrote:
>> 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
>> 
>> 
>>> -----Original Message-----
>>> From: sarah.goslee at gmail.com
>>> Sent: Fri, 22 Mar 2013 16:49:34 -0400
>>> To: jrkrideau at inbox.com
>>> Subject: Re: [R] ggplot2 will not draw a rectangle. Error: ggplot2
>>> doesn't know how to deal with data of class XXX"
>>> 
>>> Hi John,
>>> 
>>> This bit of your code:
>>> 
>>> geom_rect(data=rect
>>> 
>>> Your reproducible example doesn't create rect, and rect() already
>>> exists, so geom_rect() is trying to treat a function as data.
>>> 
>>> Sarah
>>> 
>>> 
>>> On Fri, Mar 22, 2013 at 4:42 PM, John Kane <jrkrideau at inbox.com> wrote:
>>>> What am I missing?  When I run the code below I get the error message
>>>> "Error: ggplot2 doesn't know how to deal with data of class function"
>>>> 
>>>> Googling suggests a message of "Error: ggplot2 doesn't know how to
>>>> deal
>>>> with data of class XXX" is not uncommon but I don't see why I am
>>>> getting
>>>> a "function" error unless I am using some reserved word?
>>>> 
>>>> ##=============Start 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))
>>>> 
>>>> 
>>>>   p  <-  ggplot(fcs, aes(year, federal.ps )) + geom_line()
>>>> 
>>>>   p  +  geom_rect(data=rect, aes(xmin=xmin, xmax = xmax, ymin=-Inf,
>>>> ymax
>>>> = Inf),
>>>>                   fill='red', alpha=0.2)
>>>> ##=============End Code==========================
>>>> 
>>>> sessionInfo()
>>>> R version 2.15.3 (2013-03-01)
>>>> Platform: i686-pc-linux-gnu (32-bit)
>>>> 
>>>> locale:
>>>>  [1] LC_CTYPE=en_CA.UTF-8       LC_NUMERIC=C
>>>>  [3] LC_TIME=en_CA.UTF-8        LC_COLLATE=en_CA.UTF-8
>>>>  [5] LC_MONETARY=en_CA.UTF-8    LC_MESSAGES=en_CA.UTF-8
>>>>  [7] LC_PAPER=C                 LC_NAME=C
>>>>  [9] LC_ADDRESS=C               LC_TELEPHONE=C
>>>> [11] LC_MEASUREMENT=en_CA.UTF-8 LC_IDENTIFICATION=C
>>>> 
>>>> attached base packages:
>>>> [1] stats     graphics  grDevices utils     datasets  methods   base
>>>> 
>>>> other attached packages:
>>>> [1] ggplot2_0.9.3
>>>> 
>>>> loaded via a namespace (and not attached):
>>>>  [1] colorspace_1.2-1   dichromat_2.0-0    digest_0.6.3
>>>> grid_2.15.3
>>>>  [5] gtable_0.1.2       labeling_0.1       MASS_7.3-23
>>>> munsell_0.4
>>>>  [9] plyr_1.8           proto_0.3-10       RColorBrewer_1.0-5
>>>> reshape2_1.2.2
>>>> [13] scales_0.2.3       stringr_0.6.2
>>>> 
>>>> 
> 
> 
> --
> Sarah Goslee
> http://www.functionaldiversity.org

____________________________________________________________
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



More information about the R-help mailing list