-1

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?

s_baldur
  • 29,441
  • 4
  • 36
  • 69
David
  • 15
  • 11
  • One option would be to use separate geoms with different visual appearance (color, linetype, alpha) for the data above the threshold, and the data below your threshold. May make sense to use a geom_point for this if it's going to be single events with low observation counts, rather than long stretches with low observation counts. – caw5cv Mar 13 '18 at 12:37
  • 1
    I dont quite understand your problem. You problem statement is confusing without a sample dataset. – Wenlong Liu Mar 13 '18 at 12:45
  • 1
    When asking for help, you should include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Mar 13 '18 at 12:52
  • Thank you to @snoram for the previous editing. Sorry for the confusing question, I hope it is more clear now. – David Mar 13 '18 at 15:31

1 Answers1

1

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]))
s_baldur
  • 29,441
  • 4
  • 36
  • 69
  • Thank you @snoram. Your solution is getting closer but it is marking the first 5 values. I have tried with n_below <- tail(IP,5) with no success. – David Mar 13 '18 at 17:13