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)
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 )
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 )
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")
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)
Comments
Post a Comment