Distinguishing a second categorical variable by color in a stripchart across groups
For this example, I generated a dummy data set containing repeated measures --- I created data frame in wide format first (
y
) and then used the
reshape()
function to reshape the data to long format (
longy
). The stripchart shows
weight
across
time
with color of points designating
sex
. I used the
add
argument to add points from the second stripchart to the plot of the first stripchart.
y <- data.frame(id = 1:10, sex = sample(c("M", "F"), size = 10,
replace = TRUE),
weight.t1 = sample(150:250, size = 10),
weight.t2 = sample(150:250, size = 10),
weight.t3 = sample(150:250, size = 10))
longy <- reshape(y, varying = list(paste("weight.t", 1:3, sep="")),
direction = "long", idvar = "id")
# Stripchart:
with(subset(longy, sex == "M"),
stripchart(weight.t1 ~ time, method = "jitter", ylim = c(150, 250),
vertical = TRUE,xlab="Time", ylab = "Weight", col = "red", pch = 20))
with(subset(longy, sex == "F"),
stripchart(weight.t1 ~ time, method = "jitter",
vertical = TRUE, col = "blue", pch = 20, add = TRUE))
par(xpd = NA)
legend(3, 270, legend = c("Male", "Female"), col = c("red", "blue"), pch = 20)