One easy solution is just to change your hline
call to this: geom_hline(aes(yintercept=threshold), alpha=0.3) +
.
The problem is, that would draw 150 lines on your plot (150 being the number of rows in the y
data.frame). Maybe that's ok with you, because the lines would mostly be stacked on top of each other and you would really only see four lines, in their correct locations.
However, here is another solution where I create a smaller auxiliary data.frame. This is a common approach in ggplot2. Notice how the new data.frame is specified as the data source inside the geom_hline
call.
hline_dat = data.frame(Petal.Width.Range=c("Narrow", "Narrow", "Wide", "Wide"),
Petal.Length.Range=c("Short", "Long", "Short", "Long"),
threshold=c(2, 2.5, 3.1, 4))
p = ggplot(y, aes(Sepal.Length,Sepal.Width)) +
geom_point(alpha=0.5) +
geom_hline(data=hline_dat, aes(yintercept=threshold), colour="salmon") +
facet_grid(Petal.Width.Range ~ Petal.Length.Range)
ggsave("plot.png", plot=p, height=4, width=6)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…