R question. Plotting a response for distinct combinations of 2 factors. Hopefully with 'histogram' from Lattice Package

StackOverflow https://stackoverflow.com/questions/4999118

  •  14-11-2019
  •  | 
  •  

Suppose I have a response variable (we'll call 'Y') and 2 factors (Factor A with levels A1 and A2, and Factor B with levels B1 and B2)

Can I use the 'histogram' function in the lattice package to plot the response for (A1 and B1) against the response for (A2 and B2)?

I know

histogram(~y|FactorA*FactorB)

will plot all 4 combinations. But what if I only want those two?

Just wanted to thank everyone at this site for all the help!

有帮助吗?

解决方案

One way would be to make a new variable with the four combinations and use the subset command.

FactorAB <- factor(paste(FactorA, FactorB, sep=""))
histogram(~y|FactorAB, subset=FactorAB %in% c("A1B1", "A2B2"))

其他提示

There should be a simpler way of doing this, but here is a hack that should get you pretty near what you want.

# sample data
dat <- data.frame(Y=rpois(100,20),A=factor(c(rep("A1",50),rep("A2",50))),B=factor(c(rep("B1",50),rep("B2",50))))
dat$B <- sample(dat$B)

# create blank (colourless) histogram
p <- histogram(~Y|A*B,dat,col=0,border=0)
# subset and print blank panels
p[1,]
# draw data from desired panels onto blank "template"
trellis.focus("panel",1,1)
do.call("panel.histogram",trellis.panelArgs(p,1)[1:5])
trellis.focus("panel",1,2)
do.call("panel.histogram",trellis.panelArgs(p,4)[1:5])
trellis.unfocus()

All that is left is to change the lower strip on the upper plot. Calling trellis.focus("strip",1,2) will put it in focus, and some suitable other call should be able to change it, but I can't find what that would be. It may overplot however.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top