[R] Help with displaying multiple data elements on one graph

Ross Ihaka ihaka at stat.auckland.ac.nz
Mon Jan 7 07:47:25 CET 2002


Sean O'Neill wrote:
> I'm interested if anyone can help me to the R to display multiple data 
> elements on one graph - just a simple graph, nothing fancy.  I can't 
> seem to find an example of this.  An example of what I'm looking for is, 
> say, a CPU graph which graphs user, system, and idle time as a 
> cumulative stack representing each data element as a different color.  
> See the following URL for an example of this graph generated by RRDtool 


Here's a sketch which may or may not help.  (It looks like you are
drawing a plot made up of bars - you could do something similar
with polygons.)  I would

- - - s n i p - - h e r e - - -

# First create some fake data.  The data will be in the form
# of a matrix, with each row summing to 100.

# Start by generating a matrix of random uniforms and adding 4.
x <- matrix(runif(200), nc=4) + 4

# Compute the row sums and normalise each row by its sum.
rowsums <- apply(x, 1, "sum")
y <- 100 * apply(x, 2, "/", rowsums)

# Now replace each row by its cummulative sums
# The t() is needed to get the right shape
z <- t(apply(y, 1, "cumsum"))

# Get the "x" values to plot against (YMMV).
xvals <- 1:nrow(z)

# Ok, we're ready to plot.
# Start a new plot and set up the plot window.
# The xaxs= and yaxs= remove the 6% padding at the plot edges.
plot.new()
plot.window(xlim = c(0, nrow(z)), ylim=c(0, 100),
      xaxs="i", yaxs = "i")

# Now draw the bars for each time interval.
for(i in 4:1)
   rect(xvals - 1, 0, xvals, z[,i], col = i + 1, border = NA)

# Finally add the cross-hatching grid and axes.
# I've rotated the labels on the y axis so they are horizontal.
abline(h = seq(5, 95, by = 5))
abline(v = seq(5, 45, by = 5))
axis(1)
axis(2, las=1)
box()

-- 
Ross Ihaka                         Email:  ihaka at stat.auckland.ac.nz
Department of Statistics           Phone:  (64-9) 373-7599 x 5054
University of Auckland             Fax:    (64-9) 373-7018
Private Bag 92019, Auckland
New Zealand

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list