0

this is my data frame:

year <- c("2010", "2011" , "2012", "2014")
contribution<-c(67885.7, 134488.9, 97661.8, 79957.9)
df<-data.frame(year, contribution)

and I'm trying to plot a histogram using ggplot2, but the thing is: I wanted the x-axis to be the years on "c", and the y-axis to be the amounts in "contribution" and I can't just do it on ggplot.

I wanted it to be like this: http://oi66.tinypic.com/106hqp0.jpg

I tried this Formatting histogram x-axis when working with dates using R but it just didn't work.. Would you have an advice for me?

Community
  • 1
  • 1
  • _"the years on "c""_: I don't understand what you mean by that. Also you want a barplot, not a histogram. – Axeman Sep 11 '16 at 19:31
  • 1
    Can you show the code you have tried? Anything wrong with `ggplot(df, aes(year, contribution)) + geom_bar(stat = 'identity')`? – Axeman Sep 11 '16 at 19:33
  • 1
    Or perhaps you're looking for something like `year <- lubridate::parse_date_time(c("2010", "2011" , "2012", "2014"), "%Y")`. – Axeman Sep 11 '16 at 19:37
  • Yes, you're right Axeman, I was in need of a barplot and your answer helped me with that.. I was using some other bizarre stuff, thanks a lot! – Guilherme Heiden Sep 11 '16 at 19:51

1 Answers1

1

Use barplot. And because you are using summary data, specify stat= 'identity'

year <- c("2010", "2011" , "2012", "2014")
contribution<-c(67885.7, 134488.9, 97661.8, 79957.9)
df<-data.frame(year, contribution)


require(scales) # to change numbers from e to be readable
ggplot(df,aes(year,contribution))+
  geom_bar(stat='identity',fill=colors()[128])+
  scale_y_continuous(labels = comma)
Elmahy
  • 392
  • 2
  • 15
  • 1
    Thanks a lot guys! I'm still learning the etiquette for this kind of help, anyways, wish you feel how much grateful I am to both of you :)) – Guilherme Heiden Sep 11 '16 at 22:04