0
df%>%
  group_by(approved_date)%>%
  summarise(rev=sum(gmv))%>%
  ggplot(aes(x = approved_date, y = rev)) + 
    geom_line() + 
    geom_smooth(method = 'auto', se = FALSE) + 
    labs(x = 'Date', y = 'Revenue', title = 'Revenue by Date') +
    scale_y_continuous(labels = function(x) format(x, scientific = FALSE)) +
    stat_peaks(colour = "red", span = NULL) + 
    stat_valleys(colour = "blue", span = NULL) + 
    geom_text(aes(label = round(rev, 0)),
              vjust = "inward", 
              hjust = "inward",
              show.legend = FALSE,
              check_overlap = TRUE)

I have this code which on running labels all values of Local Maxima and Minima. I want only the value of Global Maximum and Global Minimum.How to do that?

Pedro J. Aphalo
  • 5,796
  • 1
  • 22
  • 23

1 Answers1

0

As the code in the question cannot be run for lack of data, I show an example slightly modified from the package User Guide. In this case this different example should be enough to work out the solution.

library(ggpmisc)
ggplot(lynx, as.numeric = FALSE) + geom_line() + 
  stat_peaks(colour = "red") +
  stat_peaks(geom = "text", colour = "red", vjust = -0.5, 
             check_overlap = TRUE, span = NULL) +
  ylim(-100, 7300)

enter image description here

In other words geom "text" should be passed as argument to stat_peaks() as well as span = NULL to get a single label. If you add geom_text() directly, peaks are not selected but instead all values stored in the variable mapped to the label aesthetic are added to the plot.

Pedro J. Aphalo
  • 5,796
  • 1
  • 22
  • 23
  • I used above a different example so that I can verify that the code in my answer actually works. In general it is best to include in questions the simplest example that demonstrates the problem but than can be run, which usually means making a small data set of your own available or using data included in R or a package in your code. – Pedro J. Aphalo Dec 03 '19 at 16:51