Draw line segments through vertically stacked points on a scatter plot

Viewed 1597

I have a fairly large data set that is very similar to the following example:

a <-data.frame(x=sort(c(replicate(4,c(1:4,5:8,9:12,13:16,17:20,21:25)))),y=c(rnorm(100)),z=c(replicate(20,"a"),replicate(20,"b"),replicate(20,"c"),replicate(20,"d"),replicate(20,"e")))

# install and enable ggplot2, if necessary
# install.packages('ggplot2') 
library(ggplot2)

ggplot(a, aes(x=x, y=y, col=z)) + geom_point(size=3)

example plot

I would like to draw color-matched vertical line segments through each set of stacked points, but only through the points. abline() won't do the trick, in this case. I suspect that segments() or lines() need to be used, but I haven't a clue how to automate this process without manually entering each start/end point.

UPDATE

Revisiting the finalized code:

a <-data.frame(x=sort(c(replicate(4,c(1:4,5:8,9:12,13:16,17:20,21:24)))),y=c(rnorm(96)),z=c(replicate(24,"a"),replicate(24,"b"),replicate(24,"c"),replicate(24,"d")))

ggplot(a, aes(x=x, y=y, col=z)) + 
geom_point(size=3) +
stat_summary(aes(color=z), fun.ymin = min, fun.ymax = max, fun.y = max, show.legend = F)

Here's what happens when I change the point size to 1:

size=1

How do I control the size of all the points?

2 Answers
Related