无法正确获取条形图到网格

时间:2012-02-12 05:18:21

标签: r ggplot2

我尝试了很多东西,但是我无法获得创建网格的情节。我想要的是一个堆积填充的图表,但我不断得到我无法弄清楚的错误。如果我的数据只有年份(周期),则可以使用。

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

但如果我有一年以上的时间,我就会发现错误。

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

这是一个情节的样子:

http://imgur.com/EnzFa

1 个答案:

答案 0 :(得分:4)

由于我的评论似乎是正确答案,我会将其添加为答案。

你不应该使用x = c2000$RealCode来映射美学。而是在ggplot调用开始时指定数据框,然后美学映射应该只指定该数据框中列的名称。因此,您可能希望将它们全部转换为x = RealCode

之类的内容

否则ggplot会对构建情节时在哪里寻找东西感到困惑。