简体   繁体   English

R中的累积图

[英]Cumulative plot in R

I try to make a cumulative plot for a particular (for instance the first) column of my data (example): 我尝试为数据的特定列(例如第一列)绘制累积图(示例):

 1   3
 2   5
 4   9
 8  11
12  17
14  20
16  34
20  40

Than I want to overlap this plot with another cumulative plot of another data (for example the second column) and save it as a png or jpg image. 比我想将此图与另一个数据的另一个累积图(例如第二列)重叠,并将其另存为png或jpg图像。 Without using the vectors implementation "by hand" as in Cumulative Plot with Given X-Axis because if I have a very large dataset i can't be able to do that. 如果没有像在具有给定X轴的累积图中那样“手动”使用矢量实现,因为如果我有一个非常大的数据集,我将无法做到这一点。

I try the follow simple commands: 我尝试以下简单命令:

A <- read.table("cumul.dat", header=TRUE)

Read the file, but now I want that the cumulative plot is down with a particular column of this file. 读取文件,但是现在我希望该文件的特定列的累积图向下。 The command is: 该命令是:

cdat1<-cumsum(dat1)

but this is for a particular vector dat1 that I need to take from the data array (cumul.dat). 但这是针对我需要从数据数组(cumul.dat)中获取的特定向量dat1

Thanks 谢谢

I couldn't follow your question so this is a shot in the dark answer based on key words I did get: 我无法回答您的问题,因此这是我根据得到的关键词得出的黑暗答案:

m <- read.table(text=" 1   3
 2   5
 4   9
 8  11
12  17
14  20
16  34
20  40")

library(ggplot2)

m2 <- stack(m)
qplot(rep(1:nrow(m), 2), values, colour=ind, data=m2, geom="step")

在此处输入图片说明

EDIT I decided I like this approach bettwe: 编辑我决定我喜欢这种方法:

library(ggplot2)
library(reshape2)

m$x <- seq_len(nrow(m))
m2 <- melt(m, id='x')
qplot(x, value, colour=variable, data=m2, geom="step")

I wasn't quite sure when the events were happening and what the observations were. 我不确定事件何时发生以及观察结果如何。 I'm assuming the events are just at 1,2,3,4 and the columns represent sounds of the different groups. 我假设事件仅发生在1,2,3,4,而各列代表不同组的声音。 If that's the case, using Lattice I would do 如果是这样,使用莱迪思我会做

require(lattice)
A<-data.frame(dat1=c(1,2,4,8,12,14,16,20), dat2=c(3,5,9,11,17,20,34,40))
dd<-do.call(make.groups, lapply(A, function(x) {data.frame(x=seq_along(x), y=cumsum(x))}))
xyplot(y~x,dd, groups=which, type="s", auto.key=T)

Which produces 哪个产生

在此处输入图片说明

With base graphics, this can be done by specifying type='s' in the plot call: 对于base图形,可以通过在plot调用中指定type='s'来完成:

matplot(apply(A, 2, cumsum), type='s', xlab='x', ylab='y', las=1)

阶梯图

Note I've used matplot here, but you could also plot the series one at a time, the first with plot and the second with points or lines . 注意,我在这里使用了matplot ,但是您也可以一次绘制一个序列,第一个使用plot ,第二个使用pointslines plot

We could also add a legend with, for example: 我们还可以添加图例,例如:

legend('topleft', c('Series 1', 'Series 2'), bty='n', lty=c(1, 3), col=1:2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM