r - cumulative plot using ggplot2 -
i'm learning use ggplot2 , looking smallest ggplot2 code reproduces base::plot result below. i've tried few things , ended being horrendously long, i'm looking smallest expression , ideally have dates on x-axis (which not there in plot below).
df = data.frame(date = c(20121201, 20121220, 20130101, 20130115, 20130201), val = c(10, 5, 8, 20, 4)) plot(cumsum(rowsum(df$val, df$date)), type = "l")
try this:
ggplot(df, aes(x=1:5, y=cumsum(val))) + geom_line() + geom_point() 
just remove geom_point() if don't want it.
edit: since require plot data such x labels dates, can plot x=1:5 , use scale_x_discrete set labels new data.frame. taking df:
ggplot(data = df, aes(x = 1:5, y = cumsum(val))) + geom_line() + geom_point() + theme(axis.text.x = element_text(angle=90, hjust = 1)) + scale_x_discrete(labels = df$date) + xlab("date") 
since you'll have more 1 val "date", can aggregate them first using plyr, example.
require(plyr) dd <- ddply(df, .(date), summarise, val = sum(val)) then can proceed same command replacing x = 1:5 x = seq_len(nrow(dd)).
Comments
Post a Comment