2017-02-17 45 views
0

我想爲幾個變量獲取一些基本的條形圖。 運行少量代碼時,我會根據需要獲取輸出圖。但對於某些人來說,輸出不正確,圖形是單色(灰色)。 無法弄清楚它是什麼錯誤。我附上了第二張圖的輸出。 請讓我知道我要去哪裏錯了。我對R編程非常新穎。R圖中沒有變色

的數據集MD loooks這樣

State Year  Desc  Amt 
TN  2014  Won  158 
OK  2015  Lost 175 
WA  2013  Won  145 
OG  2015  Lost 174 
IL  2014  Won  165 

library(ggplot2) 

#Metric for AB */ 
AB <- ddply(MD,c("State","Year", "Desc"), 
      function (MD){data.frame( Total_Lo=nrow(MD), Total_Amt=sum(MD$Lo_Amt), Avg_Amt=mean(MD$Lo_Amt))}) 


#Loan Amount metric for States 
A <-ddply(AB,c("State", "Desc"), 
      function(AB){data.frame(Number_A=sum(AB$Total_Lo),Total_Amt_A=sum(AB$Total_Amt), Avg_Amt_A=sum(AB$Total_Amt)/sum(AB$Total_Lo))}) 

#Loan Amount Metric for Years 
B <-ddply(AB,c("Year" , "Desc"), 
      function(AB){data.frame(Number_B=sum(AB$Total_Lo),Total_B=sum(AB$Total_Amt),Avg_B=sum(AB$Total_Amt)/sum(AB$Total_Lo))}) 

#Getting proper output 
qplot(State, data = A, 
     fill=State, geom = "bar", 
     weight=Total_Amt_,ylab="Total Amount", 
     main = "Total Amount for all ") 

#Getting output but no color 
qplot(Year, data = B, fill=_Year, 
     geom = "bar",weight=Number_B,ylab="Total Count ", 
     main = " Number by Year") 

#No proper output 
qplot(State, data = AB, fill=Year, 
     geom = "bar",weight=Total_Number_Lo, 
     ylab="Total Number", main = "Number each state ") 
+0

不可重複,對象'MD'丟失。使用'dput(MD)'並粘貼結果 – GGamba

+0

我仍然無法重現最後的情節,我不得不修復一些代碼。閱讀關於在R中製作[偉大的,可重現的示例](http://stackoverflow.com/a/5963610/1261281) – GGamba

回答

0

你必須給到fill審美的引用,然後通過scale_fill_manual指定顏色給它:

qplot(Year, data = B, fill='FILL_AES', 
     geom = "bar",weight=Number_B,ylab="Total Count ", 
     main = " Number by Year") + 
    scale_fill_manual(values = c('FILL_AES' = 'blue')) 

注意不打算創建完全成熟的圖,它只是用於快速原型設計和測試。如果您需要自定義您的圖表,請使用並學習ggplot

ggplot(B, aes(x = Year, y = Number_B)) + 
    geom_bar(stat = 'identity', fill = 'blue') + 
    ggtitle('Number by year')