Q:

Mark in ggplot when the observations go below a certain number

I have a dataset where 
example <- data.frame(
  Country = rep(c("A", "B"), each = 12),
  IP = c(55,56,59,63,67,69,69,73,74,74,79,87,0,22,24,26,26,31,37,41,43,46,46,47),
  Mean_st = c(46,47,49,50,53,55,53,57,60,57,58,63,0,19,20,21,22,25,26,28,29,30,31,31)
)

ggplot(example) + 
  geom_line(aes(x = IP, y = Mean_st, color = Country), size = 2) +
  geom_vline(xintercept = 73) +
  geom_vline(xintercept = 42)

I need to mark where the number of observations is below a certain number (let's say less than 5). I can find that point in my spreadsheet for each of the countries (73 and 42), and use geom_vline like in the example, but is there a way of finding that point directly in ggplot without the need of checking the spreadsheet?

A:

You could do something like:
n_below <- 5
ggplot(example) + 
  geom_line(aes(x = IP, y = Mean_st, color = Country), size = 2) + 
  geom_vline(xintercept = sort(example$IP[example$Country == "A"][n_below])) + 
  geom_vline(xintercept = sort(example$IP[example$Country == "B"][n_below]))

