ggplot2: Group histogram data by year

旧时模样 提交于 2019-12-13 09:48:47

问题


I have a bunch of data with a YYYY-MM-DD date attached to it, and I'm having trouble getting a single bar for each year. In other words, all data from 2014 showing up under a bar for 2014.

The dates were converted to the YYYY-MM-DD format using

df1$Close.Date <- as.Date(df1$Close.Date, "%m/%d/%Y")

Here's my histogram formula that doesn't group correctly

ggplot(df1, aes(Close.Date, fill=Stage)) + geom_bar()

I've tried messing around with breaks and binwidth with no success


回答1:


I fixed it by: adding this line df1$yr <- strftime(df1$Close.Date, "%Y") and then replacing Close.Date with yr in the ggplot line.



来源:https://stackoverflow.com/questions/31842008/ggplot2-group-histogram-data-by-year

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!