How to get events per month barplot in R

# generate some random data

dat = data.frame(timestamp = Sys.time() + sort(round(runif(1000, (24*3600), (2*365*24*3600)))))

# create new columns to identify the month and year
dat$month = strftime(dat$time, “%b”)
dat$year = strftime(dat$time, “%Y”)

# count the timestamps per month for each year
library(plyr)
timestamps_month = count(dat, vars = c(“month”,”year”))

# create the plot graph
library(ggplot2)
ggplot(data = timestamps_month) + geom_bar(aes(x = month, y = freq, fill = year), stat=”identity”, position = “dodge”)

 

Here is the result:

Rplot

 

Comments are closed.