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

Rui Barradas ruipbarradas at sapo.pt
Fri Mar 22 23:41:28 CET 2013


Hello,

You're forgeting to tell gfeom_rect that the x and y aesthetics are 
already set elsewhere. You must include NULL, NULL as the first two 
arguments:

p + geom_rect(data = rectlib,
	aes(NULL, NULL, xmin = xmin, xmax = xmax, ymin = -Inf, ymax = Inf),
	fill='red', alpha=0.2)


Hope this helps,

Rui Barradas

Em 22-03-2013 21:01, John Kane escreveu:
> 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
>>>
>>>
>>> John Kane
>>> Kingston ON Canada
>>>
>>>
>>
>> --
>> 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
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list