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
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@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@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 [[alternative HTML version deleted]]
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/