Reputation: 35
I've tried a number of things but I can't get my plot to create a grid. What I want is a stacked filled chart but I keep getting errors I can't figure out. If my data only has year (Cycle) this works.
p2000 <- ggplot(c2000,aes(x=c2000$RealCode, y=c2000$Amount, fill=c2000$Party, colors=c2000$Party)) +
geom_bar(position="fill",colour="black") + cbgFillPalette + xlab("") + ylab("") + opts(title="2000")+
opts(legend.position = "none")+geom_hline(yintercept = .5, colour = "black")+
facet_grid(. ~ Cycle)
p2000
But if I have more than one year, I get y not found error.
c2000[1:5,]
RealCode Cycle Party Amount
1 A 2000 D 5581676
2 B 2000 D 2547396
3 C 2000 D 6867211
4 D 2000 D 2839314
5 E 2000 D 6255726
> p2000 <- ggplot(c2000,aes(x=c2000$RealCode, y=c2000$Amount, fill=c2000$Party, colors=c2000$Party)) +
+ geom_bar(position="fill",colour="black") + cbgFillPalette + xlab("") + ylab("") + opts(title="2000")+
+ opts(legend.position = "none")+geom_hline(yintercept = .5, colour = "black")+
+ facet_grid(. ~ Cycle)
> p2000
Error in pmin(y, 0) : object 'y' not found
> p2000 <- ggplot(c2000,aes(x=c2000$RealCode, y=c2000$Amount, fill=c2000$Party, colors=c2000$Party)) +
+ geom_bar(position="fill",colour="black") + cbgFillPalette + xlab("") + ylab("") + opts(title="2000")+
+ opts(legend.position = "none")+geom_hline(yintercept = .5, colour = "black")+
+ facet_grid(. ~ c2000$Cycle)
> p2000
Error in layout_base(data, cols, drop = drop) :
At least one layer must contain all variables used for facetting
This is what one plot looks like:
https://i.sstatic.net/AGdvV.jpg
Upvotes: 3
Views: 3349
Reputation: 173577
Since my comment appeared to be the correct answer, I'll add it as an answer.
You shouldn't be mapping aesthetics using x = c2000$RealCode
. Rather, you specify your data frame at the start of the ggplot
call, and then the aesthetic mappings should simply specify the name of columns in that data frame. So you probably want to convert them all to things like x = RealCode
.
Otherwise ggplot
will get confused about where to look for things when building the plot.
Upvotes: 4