Ggplot2 and cumsum ()

I have a set of UNIX and URI timestamps, and I'm trying to build a cumulative number of requests for each URI. I managed to do this for one URI at a time using a dummy column:

x.df$count <- apply(x.df,1,function(row) 1) # Create a dummy column for cumsum x.df <- x.df[order(x.df$time, decreasing=FALSE),] # Sort ggplot(x.df, aes(x=time, y=cumsum(count))) + geom_line() 

However, in my case, this would be approximately 30 graphs.

ggplot2 allows you to display multiple lines in a single graph (I copied this piece of code from here ):

 ggplot(data=test_data_long, aes(x=date, y=value, colour=variable)) + geom_line() 

The problem is that in this way cumsum() will be considered further.

Does anyone have any ideas?

+6
source share
1 answer

Here's the test data that uses plyr transform to calculate the cumulative sum first, and then apply this data to build using ggplot2 :

 set.seed(45) DF <- data.frame(grp = factor(rep(1:5, each=10)), x=rep(1:10, 5)) DF <- transform(DF, y=runif(nrow(DF))) # use plyr to calculate cumsum per group of x require(plyr) DF.t <- ddply(DF, .(grp), transform, cy = cumsum(y)) # plot require(ggplot2) ggplot(DF.t, aes(x=x, y=cy, colour=grp, group=grp)) + geom_line() 

enter image description here

+8
source

All Articles