Want to add sd and mean to a facet wrap

I'm relatively new to R and I'm a heck of a time figuring this out on my own. I used get symbols to get the last 5 years of a few stocks and now I'm trying to write a function that allows me view them with ggplot's facet_wrap by period and put that periods standard deviation and mean on each plot.

First I added the desired periods (terms) to the data like this:

start_dates <- c(st_date, (st_date + months(15) + days(1)), (st_date + months(30) + days(1)), (st_date + months(45) + days(1)))
end_dates <- c(st_date + months(15), st_date + months(30), st_date + months(45), st_date + months(60))

term <- function(date) {
cut(date,
breaks = ymd(st_date, end_dates),
labels = c("Months: 45-60", "Months: 30-45", "Months: 15-45", "Months: Today-15")
)
}

Then I took the raw data and pivoted it to a long format :

tail(Sector_Prices_long)

A tibble: 6 x 4

date assets prices term

1 2020-10-30 FB 263. Months: Today-15
2 2020-10-30 AMZN 3036. Months: Today-15
3 2020-10-30 NFLX 476. Months: Today-15
4 2020-10-30 GOOG 1621. Months: Today-15
5 2020-10-30 Sector_Value 1349. Months: Today-15
6 2020-10-30 Spy 327. Months: Today-15

which looks like that. and then I wrote a function that I hoped would accomplish my goal :

plot_4_ranges <- function(data, column, asset, stat, title) {
column <- rlang::enquo(column)
asset <- rlang::ensym(asset)
raw_num_eval <- data.frame(data %>%
filter(assets == asset) %>%
group_by(term) %>%
summarise(
sd = sd(prices),
mean = mean(prices),
count = length(prices)))
ungroup(raw_num_eval)

  stat = c("sd", "mean", "count")
  
  label_a <- plyr::mapvalues(stat, from = c("sd", "mean", "count"), to = c("(expression(sigma))", "(expression(mu))", "n"))
  
  raw_num_eval$join <- paste0(raw_num_eval$term, "\n(", label_a, " = ",
                              round(raw_num_eval[[stat]], 4), ")")
  
  raw_map <- setNames(raw_num_eval$join, as.factor(raw_num_eval$term))
  return(raw_map[as.character(data[[asset]])])

  plot1 <-
  data %>% 
  dplyr::filter(!!column == asset) %>% 
  ggplot(aes(prices)) +
  geom_histogram(aes(y = stat(count))) +
  facet_wrap(~ term, ncol = 2, labeler = label_both(pluck(.raw_map[["stat"]])), scales = 'free_x')  +
    geom_vline(data = raw_num_eval, aes(xintercept = mean), color = "steelblue", size = 1) +
    ggtitle(title) +
    theme_bw()

    plot(plot1)
}

but when I plot it

plot_4_ranges(Sector_Prices_long, assets, Spy, stat = "sd", "Spy Prices")

I get this error message

Error in .subset2(x, i, exact = exact) :
recursive indexing failed at level 2

can someone help me figure out where I am screwing up?

It would be easier to help if you provided a minimal example, with some data and the shortest code possible that reproduces the error.

Here is some guidance that can help.

This topic was automatically closed 21 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.