Suggestions for constructing ggplot2 useable dataset from list of matrices and vectors in R -


i wish make ggplot2 graphics functional data. often, functional data stored different matrices. thus, goal swift reshaping of data. example, below rough-pass taking fda:::growth dataset in r, list of 3 objects: 1 matrix boys ('hgtm'), 1 matrix girls (hgtf), , vector of common ages measured.

copied manual fda:

growth: berkeley growth study data

description

a list containing heights of 39 boys , 54 girls age 1 18 , ages @ collected.

format

this list "growth" contains following components:

hgtm 31 39 numeric matrix giving heights in centimeters of 39 boys @ 31 ages.

hgtf 31 54 numeric matrix giving heights in centimeters of 54 girls @ 31 ages.

age numeric vector of length 31 giving ages @ heights measured.

details

the ages not equally spaced.

how can 1 more succinctly through reshaping in functional data analysis more make plots using ggplot2?

## take list 'growth' fda package , make dataframe 'gg.growth' require(fda); require(ggplot2) gg.growth <- with(growth, data.frame(age=rep(age, ncol(hgtf)+ncol(hgtm)),                                      hgt=c(hgtf,hgtm),                                      sex=c(rep("female", length(age)*ncol(hgtf)),                                            rep("male"  , length(age)*ncol(hgtm)))))  ## add 'id' variable subsetting gg.growth$id <- with(growth,rep(1:(ncol(hgtf)+ncol(hgtm)), each=length(age)))  ## make ggplot ggplot(subset(gg.growth, id %in% c(1,40,88,93 )),         aes(x=age, y=hgt, colour=sex, group=id), alpha=.5) + geom_line() 

reshape2 powerful type of thing:

library(reshape2) growth.mlt <- melt(growth[-3])  # don't need 3rd element since in rownames 

that's it. can plot:

ggplot(growth.mlt, aes(x=var1, y=value, group=var2)) +   geom_line() + facet_wrap(~ l1) 

here plot height vs. age males , females separately:

enter image description here

clearly plot benefit better labeling, idea.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -