If I would like to aggregate the data by month, an approach is the following:
library(dplyr)
library(lubridate)
set.seed(2017)
options(digits=4)
(expenses <- data_frame(
date=seq(as.Date("2016-01-01"), as.Date("2016-12-31"), by=1),
amount=rgamma(length(date), shape = 2, scale = 20)))
Then I summarized them by month like this:
expenses %>% group_by(month=floor_date(date, "month")) %>%
summarize(amount=sum(amount))
I would like to plot an histogram of the variable amount
for each month. How could I do it?
Extract the month value from date
and using facets you can show histogram for every month in separate plots.
library(dplyr)
library(ggplot2)
expenses %>%
arrange(date) %>%
mutate(month = format(date, '%b %Y'),
month = factor(month, unique(month))) %>%
ggplot() + aes(amount) +
geom_histogram(bins = 10) +
facet_wrap(~month)