# Getting Maximum Y Value from a Trendline

Hello all! I am very new to RStudio and I have reached a block in my data analysis.

I have already plotted bird captures over time for two age classes of birds. I have one column for date, and two columns that summarize how many birds I captured for each day for each age class (SY and ASY). From this data, I have been able to summarize it with this graph:

using this code:

``````ggplot(data = MYWA, aes(x = DATE, y = SY))+
geom_point()+
stat_smooth(aes(x = DATE, y = SY), color="black", se= FALSE)+
geom_point(y=ASY, color="red")+
labs(y= "Number of Individuals", x = "Date", title="MYWA Spring" )+
stat_smooth(aes(x = DATE, y = ASY), color = "red", se= FALSE)+
ylim(0,11)
``````

My next step that I am trying (and failing) to figure out is how to find the maximum y value for each line (the highest point). I saw on a few sites where someone had used smooth_vals but I have no idea which package I need to download to be able to use it!

Any help is greatly appreciated! It seems like it should be much easier than it is but I've been working on this problem for a few hours and I still have no clue!!

I don't have access to your data so I did it using the built-in example data set, `mtcars`.

My basic approach is to fit the smoother (LOESS) outside of `ggplot` and then use some `dplyr` to group by and select the max values.

``````library(tidyverse)

# Similar plot to yours
mtcars %>%
ggplot(aes(mpg, hp, group = vs, color = factor(vs))) +
geom_point() +
geom_smooth(se = FALSE)
#> `geom_smooth()` using method = 'loess' and formula 'y ~ x'
`````` ``````# Nest the data and fit LOESS smoothers
dd <- mtcars %>%
group_nest(vs) %>%
mutate(
loess = map(data, ~ loess(hp ~ mpg, .x)),
loess = map(loess, predict)
) %>%
unnest(c(loess, data))

# Extract max value for each group of `vs`
dd_max <- dd %>%
group_by(vs) %>%
slice_max(loess)

dd_max
#> # A tibble: 2 x 12
#> # Groups:   vs 
#>      vs   mpg   cyl  disp    hp  drat    wt  qsec    am  gear  carb loess
#>   <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1     0  13.3     8  350    245  3.73  3.84  15.4     0     3     4  245.
#> 2     1  19.2     6  168.   123  3.92  3.44  18.3     0     4     4  118.

# Same as first plot, with lines at max values
mtcars %>%
ggplot(aes(mpg, hp, group = vs, color = factor(vs))) +
geom_point() +
geom_smooth(se = FALSE) +
geom_hline(data = dd_max, aes(yintercept = loess, color = factor(vs)))
#> `geom_smooth()` using method = 'loess' and formula 'y ~ x'
`````` This is awesome thank you so much!!

I was also wondering if there are any alternatives to the `unnest` function. I am trying to download the package that contains it (`unnest_0.0.2.tar.gz`) but I am receiving feedback that it is not compatible with my version of R for some reason.

Thanks again!!

The `unnest()` function comes from the `tidyr` package which is loaded when installing `tidyr` alone or the whole `tidyverse`.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.