I have a dataframe "count" that look like this:
# Date Code Number_of_events
#1 01-04 022003 5
#2 01-06 022003 9
#3 01-08 022003 3
#4 01-11 022003 2
#5 01-13 022003 5
#...
#3754 03-11 396001 4
#3755 03-16 396001 4
#3756 03-21 396001 17
#3757 03-22 396001 23
#3758 03-23 396001 3
That I got as result of aggregate a df by Date and Code:
count<-aggregate(.~ Date+Code,data=df,FUN=sum)
I want to do a barplot of the number of events (y) versus the date (x) in which each code is a series (hence a colored bar). Knowing that the events don't happened in the exact same dates.
Could anybody help me with this? Thank you
You could use the ggplot2
package, which contains a convenient geom_bar()
function.
Simulate some data:
# simulate some data
N <- 100
df <- data.frame(Date = sample(seq(as.Date("2000/1/1"), by = "month", length.out = 48), N, T),
Code = sample(c(022003, 396001, 441002), size = N, replace = T),
Number_of_events = rpois(N, 5))
#aggregate
count <- aggregate(.~ Date+Code,data=df,FUN=sum)
This is for the plotting:
library(ggplot2)
ggplot(count, aes(x=Date, y=Number_of_events)) +
geom_bar(aes(fill=as.factor(Code)), color="black", stat="identity", position="stack")