2012-02-12 64 views
3

我已經嘗試了一些東西,但無法獲取我的圖創建網格。我想要的是一張堆積滿的圖表,但我不斷弄錯我無法弄清楚的錯誤。如果我的數據只有一年(週期),這是有效的。無法正確獲取條形圖

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 

但是,如果我有超過一年,我得到y沒有發現錯誤。

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

+2

沒有一個可重複的例子,我不能提供幫助,但至少你絕對不應該像'x = c2000 $ RealCode'那樣映射美學。你應該做'x = RealCode'。 – joran 2012-02-12 05:26:43

+0

改變一切只是列名解決了我的問題。謝謝! – brian 2012-02-12 12:59:02

回答

4

由於我的評論似乎是正確的答案,我將其添加爲一個答案。

您不應該使用x = c2000$RealCode映射美學。而是在ggplot調用開始時指定數據框,然後審美映射應該簡單地指定該數據框中列的名稱。所以你可能想把它們全部轉換成x = RealCode這樣的東西。

否則ggplot會在構建情節時會對在何處尋找東西感到困惑。