What is the best way to generate a dotplot with two factors like this, preferably using standard R plots (not ggplot) and from a 2x2 data frame. The horizontal lines should be means. I have tried cleveland dot charts, but cannot figure out how to get two data series' and have the dots jittered:
Asked
Active
Viewed 9,086 times
3
-
4You could show us what you've tried (so that the Q doesn't sound like "do work for me"). We wouldn't mind some fake data to work with, either. – Roman Luštrik Sep 14 '11 at 15:22
-
So what's so bad about ggplot and/or lattice? It may take a little time to learn a few new functions, but your chartmaking will be far easier in the long run. – Carl Witthoft Sep 14 '11 at 17:05
1 Answers
12
The following piece of code should do the trick:
set.seed(1)
t1 = rnorm(10); t2 = rnorm(10, 2)
t1_g2 = rnorm(10, 4);t2_g2 = rnorm(10)
##Don't print the axes labels
par(ann=FALSE)
##Plot first set of data.
##Need to check for sensible ranges
##Use the jitter function to spread data out.
plot(jitter(rep(0,10),amount=0.2), t1,
xlim=range(-0.5,3.5), ylim=range(-3,8),
axes=FALSE,frame.plot=TRUE)
points(jitter(rep(1,10), amount=0.2), t1_g2, col=2)
points(jitter(rep(2,10), amount=0.2), t2)
points(jitter(rep(3,10), amount=0.2), t2_g2, col=2)
##Add in the y-axis
axis(2, seq(-4,8,by=2))
##Add in the x-axis labels
mtext("Treatment 1", side = 1, at=0.5)
mtext("Treatment 2", side = 1, at=2.5)
##Add in the means
segments(-0.25, mean(t1), 0.25, mean(t1))
segments(0.75, mean(t1_g2), 1.25, mean(t1_g2))
segments(1.75, mean(t2), 2.25, mean(t2))
segments(2.75, mean(t2_g2), 3.25, mean(t2_g2))
##Add in the legend
legend(0, 8, c("Group 1", "Group 2"), col=1:2, pch=1)
which gives:

csgillespie
- 59,189
- 14
- 150
- 185