[R] ReShape chicks example - line plots

David Winsemius dwinsemius at comcast.net
Mon Jul 6 21:37:30 CEST 2009


I think the offered solution was reading values down the columns  
rather than keeping the row values together. I think you need to a)  
supply a proper "x" and b) transpose:

matplot(x = as.numeric(names(diet.s)[3:14]), y= t(diet.s[,-(1:2)]),
          type='l', xlab="Days ???", ylab="Weights in (..) ???",
          col = as.numeric(as.character(diet.s$chick)))

Solves the NA plotting concern as well, which was being misinterpreted.

-- 
David Winsemius
On Jul 6, 2009, at 2:57 PM, Mark Knecht wrote:

> Wow Henrique! Close in only 2 lines. Very, very cool.
>
> There seem to be a couple of problems:
>
> 1) It seems that none of the chicks are showing up at X=0 on my plot
> even though they all have data in column 0.
>
> 2) For chicks with NA's it appears that matplot continues their line
> to the right. Is there an option not to plot NAs? No data should, in
> my view, result in the line stopping. Maybe a black dot or something
> at the end if it's easy but that's not required. (I.e. - sniff - the
> chick died!) ;-)
>
> 3) Possibly related to #2, it seems that a number of chicks aren't
> showing up until X=2 or 4.
>
> I guess matplot is smart enough to figure out the whole range based on
> all the data before it plots anything? In my database red will finish
> on the right below 0, green above, although they may travel above and
> below on their path to the finish. I suspect matplot would have no
> problems with that.
>
> Very good though. Gives me something to study and try out on my  
> larger set.
>
> Thanks, and I look forward to seeing how to fix this.
>
> Cheers,
> Mark
>
>
> On Mon, Jul 6, 2009 at 11:35 AM, Henrique  
> Dallazuanna<wwwhsd at gmail.com> wrote:
>> Try this:
>>
>> diet.s <- subset(DietResults, diet == 1)
>> matplot(diet.s[,-(1:2)],
>>         type='l',
>>         col = ifelse(as.numeric(as.character(diet.s$chick)) %% 2,  
>> 'red',
>> 'green'))
>>
>> On Mon, Jul 6, 2009 at 3: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.
>>>
>>>   Hopefully this is relatively straight forward to do. If it isn't
>>> then maybe I can understand why and do something better with cast to
>>> make it easy. My real data set is much larger and has a lot of
>>> different bins for both selection, colorizing and line type but  
>>> after
>>> using ReShape it looks similar to this one so this plot would be
>>> helpful to me.
>>>
>>> Thanks,
>>> Mark
>>>
>>> ______________________________________________
>>> R-help at r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide
>>> http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>>
>>
>>
>> --
>> Henrique Dallazuanna
>> Curitiba-Paraná-Brasil
>> 25° 25' 40" S 49° 16' 22" O
>>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

David Winsemius, MD
Heritage Laboratories
West Hartford, CT




More information about the R-help mailing list