问题
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