Two-outcome dotplot by panel variable over several models (lattice dotplot in R)

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

  •  19-10-2022
  •  | 
  •  

Question

I am trying to produce a dotplot over two variables (matched, unmatched) over several categories as below. The following code roughly does what I want (using dummy data):

library(lattice)

# Generate dummy data that resembles my real data
dp0 <- data.frame(expand.grid(covariate=c("var1","var2"), semester=1:5,
                              treatment=LETTERS[1:3]), matched=runif(30),
                              unmatched=runif(30))

# Plot matched and unmatched (percentages) by several semester-level covariates
# across several treatments (models):
dotplot(semester ~ matched + unmatched | treatment + covariate, data=dp0)

It produces the following:

Two-outcome dotplot by covariate-semester over treatment

Column labels A, B, and C are all just fine (not necessary to be replicated across rows nor with horrid peach color, but acceptable as is), but row labels (var2, var1) are intended to be less important than column labels. I envision something that looks like this:

       |    A    |    B    |    C    |
var1:  |                             |
.... 1 | x  o    |   o   x |  ox     |
.... 2 |   ox    | x  o    |  o    x |
.... 3 |o     x  |     o  x| x o     |
.... 4 | x     o | x o     |  o  x   |
.... 5 |   o x   |     x o |    ox   |
       |         |         |         |
var2:...

I tried to mess with group, for instance, but that does not help. This graph is correct as given only it is misleading in the manner it is displayed (in other words, this is a display issue, not a data issue). What are the concepts I should be searching for to accomplish this? Any thoughts appreciated.

Final graph based on @JPC's answer:

ma <- c("Unmatched", "Matched")
ma <- factor(ma, levels=ma, ordered=TRUE)
ggplot(dp0, aes(y=semester)) + geom_point(aes(x=unmatched, shape=ma[1], color=ma[1])) +
    geom_point(aes(x=matched, shape=ma[2], color=ma[2])) + facet_grid(covariate~treatment) +
    xlim(-1, 1) + labs(x=NULL,y=NULL) + scale_shape_discrete(name="") +
    scale_colour_discrete(name="", guide="legend") + theme(legend.position="bottom")

Final Plot

Was it helpful?

Solution

rjturn, I realized you're trying to do this with lattice, but figured I'd share the ggplot2 version. I find ggplot2 do be more beautiful and simpler to understand since there is an underlying logic of layers.

qplot(x=matched+unmatched, y=semester,data=dp0)+facet_grid(covariate~treatment)

enter image description here

You might also want to look into the reshape library, it is very useful in combination with ggplot2. See below for a simplification of your graphing code.

library(reshape2)
flatdp0<-melt(data=dp0,id=c("covariate","semester","treatment"))

qplot(data=flatdp0,x=value, y=semester,color=variable, shape=variable)+
  facet_grid(covariate ~ treatment)+labs(color="",shape="",x="",y="")+
  theme(legend.position="bottom",axis.text.x=element_text(angle=90))

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top