Re: error when using facet_wrap with qplot and ggplot

4,403 views
Skip to first unread message

John Kane

unread,
Jul 4, 2012, 4:04:28 PM7/4/12
to ggp...@googlegroups.com

 facet_wrap(~ zz) ? 

On Tuesday, July 3, 2012 3:54:46 PM UTC-4, dhaval wrote:
Hi,

(i'm not sure my previous post worked, sorry for additional information)

I'm new to ggplot2 and can't get the following to work after reading help topics about it and trying different combinations of command

> names(braw)
[1] "AVG_PAYMENT"     "PASSING"         "SPECIALTY_GROUP" "MONTH"          
> qplot(PASSING, AVG_PAYMENT, data = braw, geom="bar", stat="identity", fill = SPECIALTY_GROUP, position="dodge")
> qplot(PASSING, AVG_PAYMENT, data = braw, geom="bar", stat="identity", fill = SPECIALTY_GROUP, position="dodge")+facet_wrap(factor(braw$MONTH))
Error in layout_base(data, vars, drop = drop) : 
  At least one layer must contain all variables used for facetting
> qplot(PASSING, AVG_PAYMENT, data = braw, geom="bar", stat="identity", fill = SPECIALTY_GROUP, position="dodge")+facet_wrap(factor(MONTH))
Error in factor(MONTH) : object 'MONTH' not found
> qplot(PASSING, AVG_PAYMENT, data = braw, geom="bar", stat="identity", fill = SPECIALTY_GROUP, position="dodge")+facet_wrap(factor(MONTH))
Error in factor(MONTH) : object 'MONTH' not found
> qplot(PASSING, AVG_PAYMENT, data = braw, geom="bar", stat="identity", fill = SPECIALTY_GROUP)+facet_wrap(factor(braw$MONTH))
Error in layout_base(data, vars, drop = drop) : 
  At least one layer must contain all variables used for facetting


I also tried using ggplot directly, again with no success
> p <- ggplot(braw, aes(PASSING, AVG_PAYMENT))+geom_bar()+facet_wrap(braw$SPECIALTY_GROUP~)
Error: unexpected ')' in "p <- ggplot(braw, aes(PASSING, AVG_PAYMENT))+geom_bar()+facet_wrap(braw$SPECIALTY_GROUP~)"
> p <- p + facet_wrap(factor(braw$MONTH))
> p
Error in layout_base(data, vars, drop = drop) : 
  At least one layer must contain all variables used for facetting


Dennis Murphy

unread,
Jul 4, 2012, 6:06:16 PM7/4/12
to dhaval, ggp...@googlegroups.com
Hi:

I'd suggest that you post a small reproducible example (data + code)
that illustrates the problem. It is often the case that you can get
the plot you want by pre-processing the data first.

On Tue, Jul 3, 2012 at 12:54 PM, dhaval <nucle...@gmail.com> wrote:
> Hi,
>
> (i'm not sure my previous post worked, sorry for additional information)
>
> I'm new to ggplot2 and can't get the following to work after reading help
> topics about it and trying different combinations of command
>
>> names(braw)
> [1] "AVG_PAYMENT" "PASSING" "SPECIALTY_GROUP" "MONTH"
>> qplot(PASSING, AVG_PAYMENT, data = braw, geom="bar", stat="identity", fill
>> = SPECIALTY_GROUP, position="dodge")
>> qplot(PASSING, AVG_PAYMENT, data = braw, geom="bar", stat="identity", fill
>> = SPECIALTY_GROUP, position="dodge")+facet_wrap(factor(braw$MONTH))
> Error in layout_base(data, vars, drop = drop) :
> At least one layer must contain all variables used for facetting

This seems to be an informative error message: are your data nested?


Dennis

>> qplot(PASSING, AVG_PAYMENT, data = braw, geom="bar", stat="identity", fill
>> = SPECIALTY_GROUP, position="dodge")+facet_wrap(factor(MONTH))
> Error in factor(MONTH) : object 'MONTH' not found
>> qplot(PASSING, AVG_PAYMENT, data = braw, geom="bar", stat="identity", fill
>> = SPECIALTY_GROUP, position="dodge")+facet_wrap(factor(MONTH))
> Error in factor(MONTH) : object 'MONTH' not found
>> qplot(PASSING, AVG_PAYMENT, data = braw, geom="bar", stat="identity", fill
>> = SPECIALTY_GROUP)+facet_wrap(factor(braw$MONTH))
> Error in layout_base(data, vars, drop = drop) :
> At least one layer must contain all variables used for facetting
>
>
> I also tried using ggplot directly, again with no success
>> p <- ggplot(braw, aes(PASSING,
>> AVG_PAYMENT))+geom_bar()+facet_wrap(braw$SPECIALTY_GROUP~)
> Error: unexpected ')' in "p <- ggplot(braw, aes(PASSING,
> AVG_PAYMENT))+geom_bar()+facet_wrap(braw$SPECIALTY_GROUP~)"
>> p <- p + facet_wrap(factor(braw$MONTH))
>> p
> Error in layout_base(data, vars, drop = drop) :
> At least one layer must contain all variables used for facetting
>
>
> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example:
> https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2

Brandon Hurr

unread,
Jul 5, 2012, 3:00:12 AM7/5/12
to Dennis Murphy, dhaval, ggp...@googlegroups.com
I'm sure this has to do with his use of braw$Month instead of simply Month

braw$Month is a vector and there is nothing to connect it to the other data... 

adam.l...@pnc.com

unread,
Jul 5, 2012, 10:17:00 AM7/5/12
to Dhaval Adjodah, ggp...@googlegroups.com
Dhaval,

Are you trying to facet a date format? If so, this is a limitation in ggplot2, and probably explains the error message.




Adam Loveland



From: Dhaval Adjodah <nucle...@gmail.com>
To: Brandon Hurr <brando...@gmail.com>
Cc: Dennis Murphy <djm...@gmail.com>, ggp...@googlegroups.com
Date: 07/05/2012 10:09 AM
Subject: Re: error when using facet_wrap with qplot and ggplot
Sent by: ggp...@googlegroups.com





Hi,

Thanks for the reply. 

1. As you can see from the original code, I did try without the braw$ and it still does not work.
2. Dennis: this is all the code I am using (these simple interactive lines. My data consists of four simple columns,  "AVG_PAYMENT"     "PASSING"         "SPECIALTY_GROUP" "MONTH"  as shown in names(braw).. My dataset does not contain any 
'data.frame': 9 obs. of  4 variables:
 $ AVG_ALL_KEYCOUNT: num  [I can't share the data, sorry]
 $ STD_ALL_KEYCOUNT: num  
 $ PASSING         : Factor w/ 3 levels 
 $ PROVIDER_LEVEL  : Factor w/ 3 levels 
 $ MONTH          : POSIXct, format: 
Thanks,

Dhaval

I've been trying more and more. The supplied code from the ggplo2 graphics book works for facet grid, but somehow it does not work on this dataset...

Thanks,

Dhaval
--

Dhaval Adjodah
MIT Class of 2011 - Bachelors in Physics & International Development

MIT Class of 2013 - Masters in Technology and Policy

--

--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example:
https://github.com/hadley/devtools/wiki/Reproducibility

To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options:
http://groups.google.com/group/ggplot2


The contents of this email are the property of PNC. If it was not addressed to you, you have no legal right to read it. If you think you received it in error, please notify the sender. Do not forward or copy without permission of the sender. This message may contain an advertisement of a product or service and thus may constitute a commercial electronic mail message under US Law. The postal address for PNC is 249 Fifth Avenue, Pittsburgh, PA 15222. If you do not wish to receive any additional advertising or promotional messages from PNC at this e-mail address, click here to unsubscribe. https://pnc.p.delivery.net/m/u/pnc/uni/p.asp
By unsubscribing to this message, you will be unsubscribed from all advertising or promotional messages from PNC. Removing your e-mail address from this mailing list will not affect your subscription to alerts, e-newsletters or account servicing e-mails.


Brandon Hurr

unread,
Jul 5, 2012, 10:28:31 AM7/5/12
to adam.l...@pnc.com, Dhaval Adjodah, ggp...@googlegroups.com
That's definitely the problem and is a prime example of why a reproducible example is paramount. Even if you can't give the real data, give us fake data of the same format and size.

?cut.POSIXt

Should be able to make a factor from your POSIX dates and then plot them. 
Reply all
Reply to author
Forward
Message has been deleted
Message has been deleted
0 new messages