2012-07-13 73 views
0

我與數據幀的工作稱爲df創建ggplot棧酒吧和趨勢線

Date variable Value 
    1/1/2012 teamA  10 
    1/1/2012 teamA  10 


    1/1/2012 teamB 10 

    1/1/2012 teamC  15 

    1/2/2012 teamA 25 

    1/2/2012 teamB  30 

    1/2/2012 teamC  20 

並號召total

 Date  Total 
    1/1/2012 50 
    1/2/2012 70 

我想創建一個ggplot堆棧VAR第二數據幀和在同樣的圖表上繪製趨勢線,如下所示:

ggplot(df,aes(x=Date, y=Value, fill=(variable))) + 
     geom_bar(stat="identity") + 
     theme_bw() + 
     opts(title = "Team Performance") + 
     xlab("Date") + ylab("Score") + 
     geom_smooth(data=Total, 
        aes(Date,Total,group=1), 
        method="lm", size=2, color="darkblue") 

我得到這個錯誤:

Error in eval(expr, envir, enclos) : object 'variable' not found 

當我這樣做本身:

ggplot(df,aes(x=Date, y=Value, fill=(variable))) + 
geom_bar(stat="identity") + theme_bw() 

它的工作原理

variable對象肯定是存在的,任何想法,我在做什麼錯在這裏?

+1

在'ggplot()'中級聯到每層的美學。這意味着'fill = variable'預計在'geom_smooth'中,我猜''Total'中沒有什麼叫'variable'。將'fill = variable'移動到'geom_bar'或者在'geom_smooth'中用'fill = NULL'取消映射。 – joran 2012-07-13 20:25:06

回答

1

美學映射到ggplot()級聯到每一層。

這意味着fill = variable預計geom_smooth,我猜0123:Total沒有什麼叫variable。請將fill = variable移至geom_bar,或將geom_smoothfill = NULL取消映射。