Take the 2-minute tour ×

I have the following data frame

z = data.frame(x = seq(1,10),y = c(1,2,2,3,2,15,2,3,4,2))

To get a simple line plot is straight forward. For example this works.

p = ggplot() + geom_line(data=z,aes(x,y))

I now want to call out the fact that the data point with value 15 is an outlier. To do this, I would like to make the line connecting 5,2 to 6,15 and 7,2 dotted. Can this be done somehow in ggplot2?

share|improve this question
I tried making a variable linetype=rep(1,10); linetype[5:7]=2 and feeding this in to aes(x,y,linetype=linetype) in your geom_line but got an error saying that linetype must be constant within each line. Hence I think you'll have to draw 3 separate lines: from 1 to 5 (black), 5 to 7 (dotted), and 7 to 10 (black). But let's see, I could be wrong. –  mathematical.coffee Feb 25 '12 at 0:24
@mathematical.coffee What about geom_path? –  Tyler Rinker Feb 25 '12 at 0:34
The three separate lines was the approach I was going to take. –  broccoli Feb 25 '12 at 2:31

1 Answer 1

up vote 2 down vote accepted

You could make two lines, one dotted for all data, then one solid that excludes the outlier point. This seems to work:

ggplot() + geom_line(data=z,aes(x,y), linetype="dotted") + geom_line(data=z, aes(x, replace(y, y==15, NA)))
share|improve this answer

Your Answer


