Fornax-A
Fornax-A

Reputation: 1032

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. 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.

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

Thanks

Upvotes: 0

Views: 2180

Answers (3)

jbaums
jbaums

Reputation: 27388

With base graphics, this can be done by specifying type='s' in the plot call:

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

step plot

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.

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)

Upvotes: 1

Tyler Rinker
Tyler Rinker

Reputation: 109844

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

enter image description here

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

Upvotes: 2

MrFlick
MrFlick

Reputation: 206167

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. 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

enter image description here

Upvotes: 1

Related Questions