Here is a ggplot
solution. You can use ggplot
s baked in statistics capabilities, which work great except in this case require a little bit of messing around to get what you're looking for (the xlim
and coord_cartesian
business):
library(ggplot2)
library(reshape2)
dtf.mlt <- melt(dtf[-(1:2)], id.vars="Week") # data in long format
ggplot(subset(dtf.mlt, value=="Yes")) +
stat_bin(aes(x=Week, color=variable), geom="line", position="identity", binwidth=1) +
ylim(c(0, 7)) + xlim(1, 9) + coord_cartesian(xlim=c(0, 9))
If you're willing to calculate stats yourself, then it becomes easier on the plot side. Here we use aggregate
to count the number of yeses for each week/variable combo:
dtf.agg <- aggregate(dtf.mlt$value, dtf.mlt[c("Week", "variable")], FUN=function(x) sum(x == "Yes"))
ggplot(dtf.agg) + geom_line(aes(x=Week, y=x, color=variable))
Notice that by pre-calculating the data we didn't have to mess around as much to get the plot to look like what we want it to (including the x scales).