Guilherme Heiden Guilherme Heiden - 2 months ago 7
R Question

How to plot an histogram, x-axis=year on ggplot?

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?

Answer

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)