Combining facet_grid() with geom_rect()

1,100 views
Skip to first unread message

Paulo Eduardo Cardoso

unread,
Dec 16, 2009, 7:21:16 PM12/16/09
to ggplot2
I'm able to add geom_rect layer to a simple bar plot but when I facet
() it, I get an error message.

example:

ggplot() +
geom_bar(data=Data,aes(x=factor(H),y=V),stat="identity") + facet_grid
(A~.,scales="free_y") +
geom_rect(aes(NULL, NULL, xmin = inicio, xmax = fim, fill = P), ymin =
0, ymax = max(Data$V),data = DT) +
scale_fill_manual(values =alpha(c("blue","red"), 0.2))

Error: Casting formula contains variables not found in molten data: A

> str(Data)
'data.frame': 48 obs. of 3 variables:
$ H: int 0 0 1 1 2 2 3 3 4 4 ...
$ A : int 2008 2009 2008 2009 2008 2009 2008 2009 2008 2009 ...
$ V : num 0 3 0 0 0 0 1 1 2 0 ...
>

> str(DT)
'data.frame': 3 obs. of 3 variables:
$ P: Factor w/ 2 levels "L","Pl": 2 1 2
$ inicio : num 0 8.5 18.5
$ fim : num 8.5 18.5 24.5
>

JiHO

unread,
Dec 16, 2009, 7:53:35 PM12/16/09
to Paulo Eduardo Cardoso, ggplot2
On Thu, Dec 17, 2009 at 01:21, Paulo Eduardo Cardoso
<pauloed...@gmail.com> wrote:
> ggplot() +
> geom_bar(data=Data,aes(x=factor(H),y=V),stat="identity") + facet_grid
> (A~.,scales="free_y") +
> geom_rect(aes(NULL, NULL, xmin = inicio, xmax = fim, fill = P), ymin =
> 0, ymax = max(Data$V),data = DT) +
> scale_fill_manual(values =alpha(c("blue","red"), 0.2))
>
> Error: Casting formula contains variables not found in molten data: A

facet_grid needs to have data to facet on and since facetting is
global, the data should be in the ggplot() call.

ggplot(Data) +
geom_bar(data=Data,aes(x=factor(H),y=V),stat="identity") +
facet_grid(A~.,scales="free_y") +


geom_rect(aes(NULL, NULL, xmin = inicio, xmax = fim, fill = P), ymin =
0, ymax = max(Data$V),data = DT) +
scale_fill_manual(values =alpha(c("blue","red"), 0.2))

should work.

JiHO
---
http://maururu.net

Reply all
Reply to author
Forward
0 new messages