[R] ReShape chicks example - line plots

Mark Knecht markknecht at gmail.com
Mon Jul 6 23:45:53 CEST 2009


On Mon, Jul 6, 2009 at 12:28 PM, hadley wickham<h.wickham at gmail.com> wrote:
> 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

Hi Hadley,
   I'll keep the longitudinal idea in mind going forward.

   I really like the basic look of qplot and since you wrote it I
suspect you know everything about it inside out. My issues with this
specific plot were:

1) I think it's displaying all the chicks, not just the chicks on diet
1. I presume that this is because you chose group=chick from chick_m
but when I went to the qplot help page I didn't find group documented.
Is that an oversight in the help file or does that come in some other
way?

To get just diet=1 do I need to melt/cast the data first as in these
other methods I'm trying out?


2) It is making some very strange choices for the legend, printing
as.numeric(as.character(chick)) %% 2 and showing numbers 0, 0.2, 0.4,
0.6, 0.8 & 1. I presume you see the same thing?

When I added the color checking back in I see I chose the colors
backwards (as below) or at least the colors and the words are mixed up
so having the legend is a good idea.

qplot(time, value, data = chick_m, group = chick,
	geom = "line",
 	colour = ifelse(as.numeric(as.character(chick)) %% 2, 'red', 'green'))

Thanks,
Mark




More information about the R-help mailing list