broccoli
broccoli

Reputation: 4846

ggplot2 dotted lines for a subsection of a plot

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?

Upvotes: 4

Views: 4229

Answers (1)

Dan M.
Dan M.

Reputation: 1616

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)))

Upvotes: 2

Related Questions