如何在ggplot上绘制直方图,x轴=年份?

时间:2016-09-11 19:22:26

标签: r ggplot2

这是我的数据框:

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

我正在尝试使用ggplot2绘制直方图,但问题是:我希望x轴是“c”上的年份,而y轴是“贡献”中的数量和我不能只在ggplot上做到这一点。

我希望它是这样的: http://oi66.tinypic.com/106hqp0.jpg

我尝试了这个Formatting histogram x-axis when working with dates using R,但它没有用。你能给我一个建议吗?

1 个答案:

答案 0 :(得分:0)

使用条形图。而且由于您使用的是摘要数据,请指定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)