I have a stock price dataframe (Date and Price (date and numeric) are the only columns). I am doing a ggplot to highlight max and min price.
I am using stat_peaks() and stat_valleys() in ggspectra library. But stat_peaks() highlights the highest price perfectly but stat_valleys() does not.
span=NULL takes just the highest value but if span has a value then more valleys and peaks are highlighted.
ggplot(df,aes(x=Date,y=Price)) + geom_line() +
stat_peaks(span = NULL, geom = "point", color = "red") +
stat_valleys(span = NULL, geom = "point", color = "blue")
But if were to change span to a value then valleys are highlighted fine.
ggplot(df,aes(x=Date,y=Price)) + geom_line() +
stat_peaks(span = NULL, geom = "point", color = "red") +
stat_valleys(span = 51, geom = "point", color = "blue")
I can use which.min to highlight the lowest point but I want to know why stat_valleys() does not work.
#Below code highlights both high and low point
df1 = df[which.min(df$Price),]
ggplot(df,aes(x=Date,y=Price)) + geom_line() +
stat_peaks(span = NULL, geom = "point", color = "red") +
geom_point(aes(x=df1$Date,y=df1$Price),color="blue")
Note: Using latest version of RStudio and R (as of 11/5).