2
votes

I'm building an NBA R Shiny Application and I'm running into a small problem trying to make interactive plots. My Geom smooth element works in the first set of code I've provided which shows a smoothing average of the margin of victory for the selected team, but once I implement custom tooltips with ggplotly the geom smooth element stops working.

mov_plot <- function(df){
  p <- df %>%
    ggplot(aes(Date, Margin_of_Victory)) +
    geom_col(color = 'black', alpha = 0.7, aes(fill = Outcome)) +
    geom_smooth(method = 'loess', se = FALSE, color = 'grey20', alpha = 0.4) +
    scale_y_continuous(breaks = c(-25, -20, -15, -10, -5, 0, 5, 10, 15, 20, 25)) +
    scale_fill_manual(values = c("red", "dark green")) +
    labs(x = NULL,
         y = 'Margin of Victory',
         title = paste0(df$FullName, ' Game Log History \n 2019-2020 NBA Season'),
         subtitle = '2019-2020 NBA Season') +
    theme_jacob()
  
  ggplotly(p)
  
}
mov_plot <- function(df){
  p <- df %>%
    ggplot(aes(Date, Margin_of_Victory, text = paste(Date, '<br>',
                                                     Outcome, ' vs', Opponent, '<br>',
                                                     'Scoreline: ', team_pts, ' - ', Opp_PTS, '<br>',
                                                     'Margin of Victory: ', Margin_of_Victory))) +
    geom_col(color = 'black', alpha = 0.7, aes(fill = Outcome)) +
    geom_smooth(method = 'loess', se = FALSE, color = 'grey20', alpha = 0.4) +
    scale_y_continuous(breaks = c(-25, -20, -15, -10, -5, 0, 5, 10, 15, 20, 25)) +
    scale_fill_manual(values = c("red", "dark green")) +
    labs(x = NULL,
         y = 'Margin of Victory',
         title = paste0(df$FullName, ' Game Log History \n 2019-2020 NBA Season'),
         subtitle = '2019-2020 NBA Season') +
    theme_jacob()
  
  ggplotly(p, tooltip = c('text'))
  
}

Below are 2 images showing the problem where the geom_smooth element just disappears when I use the second set of code.

enter image description here

enter image description here

If anyone has experience with plotly and has any ideas on a potential fix I'd greatly appreciate it !

2

2 Answers

0
votes

Try formula = 'y ~ x' in your geom_smooth as below

geom_smooth(method = 'loess', se = FALSE, formula = 'y ~ x', color = 'grey20', alpha = 0.4)

As you didn't provide a sample dataframe, it is hard to verify.

0
votes

The geom_smooth layer is trying to include the text aesthetic, which in your case you do not want. I recommend that you move the text aesthetic from the ggplot() parent function to the geom_col() function, in order to put the tooltips on the columns.

If an interested reader wants to put the tooltip on geom_smooth instead:

The text aesthetic does not work with geom_smooth because geom_smooth is an aggregated layer, or "summary statistic generated by ggplot2" (https://plotly-r.com/controlling-tooltips.html#tooltip-text-ggplotly). The ggplotly object has more than one "trace"—the second trace is probably the geom_smooth layer. You can explore these sub-objects by first saving the ggplotly object

w <- ggplotly(p)

then looking at w$x$data. On geom_smooth, the tooltips will be "continuous", so the horizontal axis variable and vertical axis variables are probably saved in w$x$data[[2]]$x and w$x$data[[2]]$y respectively. The text in the tooltip can thus be populated with these vectors:

# Suppress the tooltip on the columns and supply custom text to the smooth line
w %>%
  style(hoverinfo = "skip", traces = 1) %>%
  style(text = paste0(
      "Date: ", w$x$data[[2]]$x, "\nMargin of Victory: ", w$x$data[[2]]$y
    ), traces = 2)