0

I am trying to get some basic bar graphs for a few variables. While running few codes, I am getting the output graphs as desired. But for some, the output is not proper and the graphs are mono-color(gray). Unable to figure out what error it is. I have attached the output for the 2nd graph. Kindly let me know where I am going wrong. I am pretty new to R programming.

The data set MD loooks like this

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 ")
GGamba
  • 13,140
  • 3
  • 38
  • 47
Andy
  • 1
  • 1
  • Not reproducible, object `MD` is missing. Use `dput(MD)` and paste the result – GGamba Feb 17 '17 at 23:58
  • I still can't reproduce the last plot, and I had to fix some code. Read about making a [great, reproducible example in R](http://stackoverflow.com/a/5963610/1261281) – GGamba Feb 18 '17 at 01:00

1 Answers1

0

You have to give a reference to the fill aesthetic and then assign a color to it via 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'))

Note that qplot is not intended to create fully fledged graphs, it's just for fast prototyping and testing. If you need to customize your graph pls use and learn ggplot:

ggplot(B, aes(x = Year, y = Number_B)) +
  geom_bar(stat = 'identity', fill = 'blue') +
  ggtitle('Number by year')
GGamba
  • 13,140
  • 3
  • 38
  • 47