ggplot2 and cumsum()

徘徊边缘 提交于 2020-01-12 08:00:06

问题


I have a set of UNIX timestamps and URIs and I'm trying to plot the cumulative count of requests for each URI. I managed to do that 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, that would make roughly 30 plots in my case.

ggplot2 does allow you to plot multiple lines into one plot (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, this way, cumsum() would count on and on.

Does anybody have an idea?


回答1:


Here's a test data which uses plyr's transform to calculate the cumulative sum first and then apply that data to plot 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()



来源:https://stackoverflow.com/questions/15768099/ggplot2-and-cumsum

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!