ggplot2 - How to change points and add a regression to a cloudplot (using R)? -


to make clear i'm asking i've created easy example. step 1 create data:

gender <- factor(rep(c(1, 2), c(43, 41)), levels = c(1, 2),labels = c("male", "female")) numberofdrugs <- rpois(84, 50) + 1 geneticvalue <- rpois(84,75) death <- rpois(42,50) + 15 y <- data.frame(death, numberofdrugs, geneticvalue, gender) 

so these random dates merged 1 data.frame. these dates i'd plot cloud can differ between males , females , add 2 simple regressions (one females , 1 males). i've started, couldn't point want be. please see below i've done far:

require(lattice) cloud(y$death~y$numberofdrugs*geneticvalue) 

cloud plot in basic form

xmale <- subset(y, gender=="male") xfemale <- subset(y, gender=="female")  death.lm.male <- lm(death~numberofdrugs+geneticvalue, data=xmale) death.lm.female <- lm(death~numberofdrugs+geneticvalue, data=xfemale) 

how can make different points males or females when using cloud command (for example blue , pink points instead of blue crosses) , how can add 2 estimated models cloud graph?

any thought appreciated! ideas!

answer first half of question, "how can make different points males or females when using cloud command (for example blue , pink points insted of blue crosses)?"

 cloud( death ~ numberofdrugs*geneticvalue , groups=gender, data=y ) 

grouped cloud plot

the meta-answer may involve non-3d visualization. perhaps can use lattice or ggplot2 split data small multiples? more comprehensible , easier add regression results.

splom( ~ data.frame( death, numberofdrugs, geneticvalue ), groups=gender, data=y ) 

splom

the default splom panel function panel.pairs, , modify add regression line without enormous amount of trouble.

ggplot2 regressions within plot matrix easily, can't colors work.

pm <- plotmatrix( y[ , 1:3], mapping = aes(color=death) ) pm + geom_smooth(method="lm") 

plotmatrix

and finally, if want cloudplot regression plane, here's way using scatterplot3d package. note changed data have little more interesting structure see:

numberofdrugs <- rpois( 84, 50 ) + 1 geneticvalue <- numberofdrugs + rpois( 84, 75 ) death <- geneticvalue + rpois( 42, 50 ) + 15 y <- data.frame( death, numberofdrugs, geneticvalue, gender )  library(scatterplot3d)  pts <- as.numeric( as.factor(y$gender) ) + 4 s <-scatterplot3d( y$death, y$numberofdrugs, y$geneticvalue, pch=pts, type="p", highlight.3d=true ) fit <- lm( y$death ~ y$numberofdrugs + y$geneticvalue ) s$plane3d(fit) 

scatterplot3d regression plane


Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -