2016-08-12 26 views
0

我想繪製兩列原始數據(我已經使用融合將它們合併爲一個數據框),然後爲每個添加單獨的錯誤欄。但是,我想爲每列的原始數據製作一對顏色,而錯誤欄則製作另一組顏色,但似乎無法使其工作。我得到的陰謀是在下面的鏈接。我想爲原始數據和錯誤欄使用不同的顏色對。出於說明的目的,下面編碼一個簡單的可再現的例子。如何更改stat_summary()中的顏色

dat2.m<-data.frame(obs=c(2,4,6,8,12,16,2,4,6),variable=c("raw","raw","raw","ip","raw","ip","raw","ip","ip"),value=runif(9,0,10)) 

    c <- ggplot(dat2.m, aes(x=obs, y=value, color=variable,fill=variable,size = 0.02)) +geom_jitter(size=1.25) + scale_colour_manual(values = c("blue","Red")) 

    c<- c+stat_summary(fun.data="median_hilow",fun.args=(conf.int=0.95),aes(color=variable), position="dodge",geom="errorbar", size=0.5,lty=1) 

    print(c) 

[1]: http://i.stack.imgur.com/A5KHk.jpg

+0

你能發佈一個可重複的例子嗎?或者告訴我們情節中出了什麼問題? – jdobres

+0

劇情沒有錯,我只是不確定如何讓誤差線成爲與原始數據不同的顏色對。 – user85727

+0

我可以發佈一個可重複的例子,當然 – user85727

回答

0

一個解決此方法是使用geom_pointstat_summary重複調用。使用這些函數的參數data將數據集的子集提供給每個調用,並將顏色屬性設置爲aes()之外。這是重複性的,有點擊敗ggplot的緊湊性,但它會。

c <- ggplot(dat2.m, aes(x = obs, y = value, size = 0.02)) + 
    geom_jitter(data = subset(dat2.m, variable == 'raw'), color = 'blue', size=1.25) + 
    geom_jitter(data = subset(dat2.m, variable == 'ip'), color = 'red', size=1.25) + 
    stat_summary(data = subset(dat2.m, variable == 'raw'), fun.data="median_hilow", fun.args=(conf.int=0.95), color = 'pink', position="dodge",geom="errorbar", size=0.5,lty=1) + 
    stat_summary(data = subset(dat2.m, variable == 'ip'), fun.data="median_hilow", fun.args=(conf.int=0.95), color = 'green', position="dodge",geom="errorbar", size=0.5,lty=1) 

print(c) 

enter image description here

0

爲了記錄:我認爲這是一個非常,非常糟糕的主意。除非你有一個至關重要的用例,否則我認爲你應該重新檢查你的計劃。

但是,您可以通過添加一組新變量來避開它,並在末尾填充一個空格。你會/需要玩的傳說,但這應該工作(雖然它肯定是醜陋的):

dat2.m<- data.frame(obs=c(2,4,6,8,12,16,2,4,6),variable=c("raw","raw","raw","ip","raw","ip","raw","ip","ip"),value=runif(9,0,10)) 


c <- ggplot(dat2.m, aes(x=obs, y=value, color=variable,fill=variable,size = 0.02)) +geom_jitter(size=1.25) + scale_colour_manual(values = c("blue","Red","green","purple")) 

c<- c+stat_summary(fun.data="median_hilow",fun.args=(conf.int=0.95),aes(color=paste(variable," ")), position="dodge",geom="errorbar", size=0.5,lty=1) 

print(c)