【问题标题】:ggplotly is breaking my geom_smooth elementggplotly 正在破坏我的 geom_smooth 元素
【发布时间】:2020-11-10 01:35:34
【问题描述】:

我正在构建一个 NBA R Shiny 应用程序,但在尝试制作交互式情节时遇到了一个小问题。我的 Geom Smooth 元素在我提供的第一组代码中工作,该代码显示了所选团队获胜幅度的平滑平均值,但是一旦我使用 ggplotly 实现自定义工具提示,geom smooth 元素就会停止工作。

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'))
  
}

下面是两张图片,显示了当我使用第二组代码时 geom_smooth 元素刚刚消失的问题。

如果有人对 plotly 有经验并且对潜在的修复有任何想法,我将不胜感激!

【问题讨论】:

    标签: r ggplot2 plotly ggplotly


    【解决方案1】:

    geom_smooth 层正在尝试包含 text 美学,在您的情况下您不想要它。我建议您将 text 美学从 ggplot() 父函数移动到 geom_col() 函数,以便将工具提示放在列上。

    如果感兴趣的读者想将工具提示放在geom_smooth 上:

    text 美学不适用于geom_smooth,因为geom_smooth 是一个聚合层,或“ggplot2 生成的汇总统计信息”(https://plotly-r.com/controlling-tooltips.html#tooltip-text-ggplotly)。 ggplotly 对象有不止一个“踪迹”——第二个踪迹可能是geom_smooth 层。您可以通过首先保存ggplotly 对象来探索这些子对象

    w <- ggplotly(p)
    

    然后查看w$x$data。在geom_smooth 上,工具提示将是“连续的”,因此水平轴变量和垂直轴变量可能分别保存在w$x$data[[2]]$xw$x$data[[2]]$y 中。因此,工具提示中的文本可以填充这些向量:

    # 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)
    

    【讨论】:

      【解决方案2】:

      在您的geom_smooth 中尝试formula = 'y ~ x',如下所示

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

      由于您没有提供示例数据框,因此很难验证。

      【讨论】:

      • 很遗憾没用,但我很欣赏这个建议。
      • 您还可以在工具提示中尝试其他一些操作。首先你可以把text改成label或者name,试试看。如果还是不行,或许可以把tooltip定义中的&lt;br&gt;换成\n
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-14
      • 2023-03-25
      • 2015-07-21
      • 1970-01-01
      • 2017-07-10
      • 1970-01-01
      相关资源
      最近更新 更多