[R] ReShape chicks example - line plots

hadley wickham h.wickham at gmail.com
Mon Jul 6 21:28:30 CEST 2009


On Mon, Jul 6, 2009 at 8:22 PM, Mark Knecht<markknecht at gmail.com> wrote:
> Hi,
>   In the examples from the ReShape package there is a simple example
> of using melt followed by cast that produces a smallish amount of
> output about the chicks database. Here's the code:
>
> library(reshape)
>
> names(ChickWeight) <- tolower(names(ChickWeight))
> chick_m <- melt(ChickWeight, id=2:4, na.rm=TRUE)
> DietResults <- cast(chick_m, diet + chick ~ time)
> DietResults
>
>   My challenge is to extract an plot only a portion of this data.
>
>   I would like to plot the data for each chick that participated in
> diet 1 only. Assume that the numbered column names (0,2,4, ...)
> represent time on the diet and will be the X axis. Y values on the
> plot will be the value in the table. (chick weight) Y maximum should
> be larger than the max value in the diet 1 portion of the table.
> Additionally if a chick's number is even I would like to plot it's
> results in green, if it's odd then plot in red. The plot should use a
> line type so that in the general case I could trace an individual
> chick's progress on the diet. I don't care if I use plot vs any other
> command that would make a plot with colored lines. I would *prefer*
> that the code discovers where in DietResults the column entitled "0"
> is as I don't know where the beginning of the data will be based on
> how many variables I bin for in cast.

Generally, I think it's easier to work with longitudinal data with
time as its own column.  It makes plotting and analysis much easier:

library(ggplot2)
qplot(time, value, data = chick_m, group = chick,
  colour = as.numeric(as.character(chick)) %% 2, geom = "line")

It's far easier to see what's going on:

 * on the x-axis, time
 * on the y-axis, value (weight)
 * grouped by chick

Hadley


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




More information about the R-help mailing list