Skip to content
Prev 268672 / 398502 Next

Alternative and more efficient data manipulation

Hi:

As previously mentioned, the reshape package (in particular, melt())
is an alternative to make.groups(), although they serve the same
purpose in this example:

dat <- data.frame(`x1`=runif(6, 0, 125),
                 `x2`=runif(6, 50, 75),
                 `x3`=runif(6, 0, 100),
                 `x4`=runif(6, 0, 200),
                 date = as.Date(c("2009-09-25","2009-09-28","2009-10-02",
                                  "2009-10-07","2009-10-15","2009-10-21")),
                 yy= head(letters,2), check.names=FALSE)

# id variables are not melted, but carried along in parallel
library('reshape')
mdat <- melt(dat, id = c('date', 'yy'))
# Create a new variable in the data frame rather than try to
# create it in the plot call
mdat <- within(mdat, val = as.numeric(substr(variable, 2, 2)))

# Here are a couple of plot examples that appear to be close
# to what you want to do:

# library('lattice')
# library('ggplot2')

# Create a key for dates
mkey <- list(space = 'top', columns = 2,
             title = 'Date', cex.title = 1,
             text = list(as.character(unique(mdat$date))),
             points = list(pch = 16, col = 1:6),
             lines = list(lty = 1, col = 1:6))
xyplot(value ~ val | yy, data = mdat, groups = date,
       pch = 16, col = 1:6, col.line = 1:6,
       type = c('p', 'l'), key = mkey)

# Similar type of plot in ggplot2 (legend at right instead)
ggplot(mdat, aes(x = val, y = value, colour = factor(date))) +
     geom_point(size = 2.5) + geom_line(aes(group = date), size = 1) +
     facet_wrap( ~ yy, ncol = 2) +
     scale_colour_brewer('Date', pal = 'Dark2')

HTH,
Dennis
On Mon, Aug 15, 2011 at 4:57 PM, Sam Albers <tonightsthenight at gmail.com> wrote: