2014-07-03 104 views
-1

我想繪製一些跨實驗編碼的數據。我搜索了stackoverflow以最終弄清楚如何以我想要的順序排序實驗和代碼級別,而不是按字母順序排列。現在,情節似乎正在下降我的「4級」因素。R ggplot刪除一個因子

看着Cookbook for R告訴我默認的顏色應該是什麼。我的情節展示了4因素配色方案,而不是5,而「4級」因素變灰。它也從傳說中被拋棄了。這對另一組眼睛來說可能是顯而易見的,但經過漫長的一天分析後,我很難過。任何幫助將非常感激!我已經在下面包含了我的代碼。

更新:如果我從Codes因子和$ + scale_fill_discrete(breaks = c(「No Comment」,「Level 1」,「Level」)中刪除「levels = c(...),ordered = TRUE) 2「,」Level 3「,」Level 4「))$,它從ggplot函數中恢復所有的因子,但是按字母順序排列。

謝謝!

Y1Max<-data.frame(Codes=factor(c("No Comment","Level 1","Level 2", "Level 3", "Level 4","No Comment", "Level 1","Level 2", "Level 3", "Level 4","No Comment","Level 1","Level 2", "Level 3", "Level 4"),levels=c("No Comment","Level 1", "Level 2", "Level 3","Level4"),ordered=TRUE), 
Experiment=factor(c("Exp1","Exp1","Exp1","Exp1","Exp1","Exp2","Exp2","Exp2","Exp2","Exp2","Exp3","Exp3","Exp3","Exp3","Exp3"),levels=c("Exp1","Exp2","Exp3"),ordered=TRUE), 
Num=c(0.008,0,.517,.442,.033,.022,.39,.375,.191,.022,.041,.215,.314,.289,.14)) 

pY1Max<-ggplot(data=Y1Max,aes(x=Experiment,y=Num,colour=Codes,fill=Codes)) + 

geom_bar(stat="identity",position=position_dodge())+ 
theme(legend.position="bottom")+xlab("Experiment - Year 1") + 
ylab("Num")+ 
scale_y_continuous(limits=c(0,0.6))+ 
scale_fill_discrete(breaks=c("No Comment","Level 1","Level 2", "Level 3", "Level 4")) 
+0

你看看你的數據幀繪製過嗎?我想你會發現問題 – rawr

+0

專門'表(Y1Max $ Codes)' – MrFlick

回答

3

在你的代碼的水平= C的第一行(「無可奉告」 ......「級別4」),你的水平和4.在你的因素有空間之間有空間。這就是爲什麼NA​​正在進入你的數據。看看這個

library(ggplot2) 
Y1Max<-data.frame(Codes=factor(c("No Comment","Level 1","Level 2", "Level 3", "Level 4","No Comment", "Level 1","Level 2", "Level 3", "Level 4","No Comment","Level 1","Level 2", "Level 3", "Level 4"),l![j][1]evels=c("No Comment","Level 1", "Level 2", "Level 3","Level 4"),ordered=TRUE), 
        Experiment=factor(c("Exp1","Exp1","Exp1","Exp1","Exp1","Exp2","Exp2","Exp2","Exp2","Exp2","Exp3","Exp3","Exp3","Exp3","Exp3"),levels=c("Exp1","Exp2","Exp3"),ordered=TRUE), 
        Num=c(0.008,0,.517,.442,.033,.022,.39,.375,.191,.022,.041,.215,.314,.289,.14)) 

pY1Max<-ggplot(data=Y1Max,aes(x=Experiment,y=Num,fill=Codes)) + 

     geom_bar(stat="identity",position=position_dodge())+ 
     theme(legend.position="bottom")+xlab("Experiment - Year 1") + 
     ylab("Num")+ 
     scale_y_continuous(limits=c(0,0.6))+ 
     scale_fill_brewer(palette="Set1") 
pY1Max 

image

+0

[臉掌]謝謝! – user3799848