Skip to content
Back to formatted view

Raw Message

Message-ID: <f8e6ff050809151659k56eca768w4db9bc5baeab7c7@mail.gmail.com>
Date: 2008-09-15T23:59:41Z
From: Hadley Wickham
Subject: How to plot a matrix of intervals
In-Reply-To: <850040.57523.qm@web45316.mail.sp1.yahoo.com>

On Mon, Sep 15, 2008 at 5:06 PM, Stacey Burrows <stacey.burrows at yahoo.ca> wrote:
> Dear R-users,
>
> I have some nonstandard data set which I would like to plot but don't know how to do it in R. The data is in a matrix where the rows represent samples and the columns represent locations. The entries of the matrix are 0's and 1's, where 1 represents an event and 0 represents a non-event. e.g.
>
> aberrations <- matrix(rbinom(1000, 1, 0.8), nrow=20, ncol=50, dimnames=list(c(paste("sample",1:20,"")), c(1:50)))
>
> In addition, consecutive 1's in a given sample represents intervals of events.
>
> With this matrix I would like to produce a plot with the samples on the y-axis and the locations on the x-axis. A black dot should be plotted for any event and consecutive black dots should be connected by a line to represent intervals.
>
> How can I produce such a plot in R?

Here's one approach:

install.packages("ggplot2")

library(reshape)
am <- melt(aberations)
am$value[am$value == 0] <- NA

library(ggplot2)
qplot(X2 * value, X1, data=am) + geom_path()

Converting the zeros to missing values means that when we multiply the
x position by value, it will be missing if the value is 0, causing
breaks in the lines and the absences of points.  I think this same
basic approach will work for base graphics and lattice graphics too.

Hadley

-- 
http://had.co.nz/