ggplot2和cumsum() [英] ggplot2 and cumsum()

查看:171
本文介绍了ggplot2和cumsum()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组UNIX时间戳和URI,我试图绘制每个URI请求的累计计数。我设法用一个虚拟列一次为一个URI做到这一点:

  x.df $ count < -  apply( x.df,1,function(row)1)#为cumsum创建一个虚拟列
x.df < - x.df [order(x.df $ time,decrease = FALSE)]#Sort
ggplot(x.df,aes(x = time,y = cumsum(count)))+ geom_line()

但是,在我的情况下,这大概会产生30个图。

ggplot2确实允许您将多行图表绘制成一个图表(我复制了这一段代码从这里):

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

问题在于,这样, cumsum()会依靠和



有没有人有想法?

解决方案

测试数据使用 plyr tran sform 首先计算累计和,然后使用 ggplot2

< pre $ 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)))

#使用plyr计算每组x
的cumsum需要(plyr)
DF.t< - ddply(DF,。(grp),transform,cy = cumsum(y))

#plot
require(ggplot2)
ggplot(DF.t,aes(x = x,y = cy,color = grp,group = grp))+ geom_line()


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?

解决方案

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()

这篇关于ggplot2和cumsum()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆