I have added two straight lines in ggplot(in addition to other lines) for min_safety_stock and max_safety_stock. Now I want to show label for these two lines with their corresponding values of min_safety_stock days and max_safety_stock_days. How do I do that?
Asked
Active
Viewed 470 times
-2
-
3Hi Suhail, please make your question [reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – NelsonGon Jun 02 '19 at 08:06
1 Answers
2
Manually, you could add notes using annotate
:
ggplot(mtcars, aes(wt, mpg)) +
geom_point() +
geom_hline(yintercept = 27) +
annotate("text", x = 2.5, y = 27, label = "This is at 27", vjust = -0.5)
If you're doing a few or want to make it more automatic, you could use a function:
add_label_line <- function(y, x, text = paste("This line is at", y)) {
list(geom_hline(yintercept = y),
annotate("text", x = x, y = y, label = text, vjust = -0.5)
)
}
ggplot(mtcars, aes(wt, mpg)) +
geom_point() +
add_label_line(c(25, 28, 31), 2.5)

Jon Spring
- 55,165
- 4
- 35
- 53