在 ggplotly 中设置工具提示参数时,geom_line 不绘图 [英] geom_line not plotting when tooltip argument set in ggplotly

查看:21
本文介绍了在 ggplotly 中设置工具提示参数时,geom_line 不绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我闪亮的应用程序中使用 plotly 来实现交互式视觉效果.但是,在尝试向图表添加自定义工具提示时遇到了问题.如果我在 ggplotly() 中设置 tooltip 参数,那么我的 geom_line() 在我的输出中被忽略,但工具提示有效.

I am trying to use plotly for interactive visuals in my shiny app. However, I am running into a problem when trying to add a custom tooltip to the graph. If i set the tooltip argument in ggplotly() then my geom_line() is ignored in my output but the tooltip works.

这是一个 MRE:

library(shiny)
library(ggplot2)
library(plotly)
library(tidyquant) #only using the palette_dark() function
library(magrittr)

graph <- data %>%
  ggplot(aes(date, result, text = paste0("Site: ", site, "
", 
                                     "Quarter: ", quarter, "
", 
                                     "Year: ", year, "
", 
                                     "Result: ", result))) +
  facet_wrap(~site) +
  geom_point(color = palette_dark()[1]) +
  geom_line(color = palette_dark()[1]) +
  theme_bw() 

 ggplotly(graph, 
          tooltip = "text")

这给了我这个:

如果我将 data = aes() 调用从 ggplot 调用移动到单个 geoms then 行仍然没有显示:

If I move the data = and aes() calls from the ggplot call to the individual geoms then The lines still aren't shown:

graph <- ggplot() +
  facet_wrap(~site) +
  geom_point(data = data, 
             aes(date, result, 
                 text = paste0("Site: ", site, "
", 
                               "Quarter: ", quarter, "
",
                               "Year: ", year, "
", 
                               "Result: ", result)),
             color = palette_dark()[1]) +
  geom_line(data = data, 
            aes(date, result, 
                text = paste0("Site: ", site, "
", 
                              "Quarter: ", quarter, "
",
                              "Year: ", year, "
", 
                              "Result: ", result)),
            color = palette_dark()[1]) +
   theme_bw() 

ggplotly(graph, 
     tooltip = "text") 

这给了我相同的输出,工具提示仍然有效.但是,现在我收到两个警告:

This gives me the same output with the tooltips still working. However, now I receive two warnings:

警告:忽略未知的美学:文本

Warning: Ignoring unknown aesthetics: text

警告:忽略未知的美学:文本

Warning: Ignoring unknown aesthetics: text

如果我仅从 geom_line() 函数中删除 text= 参数,则绘制线条但工具提示不再起作用:

If I remove the text= argument from just the geom_line() function then the lines are plotted but the tooltip no longer works:

问题:

如何在绘制 geom_line() 的同时让自定义工具提示正常工作?

How do I get the custom tooltip to work while still plotting my geom_line()?

我的数据:

data <- structure(list(site = c("Site 1", "Site 1", "Site 1", "Site 1", 
                                    "Site 2", "Site 2", "Site 2", "Site 2", 
                                    "Site 3", "Site 3", "Site 3", "Site 3", 
                                    "Site 4", "Site 4", "Site 4", "Site 4", 
                                    "Site 5", "Site 5", "Site 5", "Site 5"), 
                           parameter = c("Testing Parameter", "Testing Parameter",
                                         "Testing Parameter", "Testing Parameter", 
                                         "Testing Parameter", "Testing Parameter", 
                                         "Testing Parameter", "Testing Parameter", 
                                         "Testing Parameter", "Testing Parameter", 
                                         "Testing Parameter", "Testing Parameter", 
                                         "Testing Parameter", "Testing Parameter",
                                         "Testing Parameter",  "Testing Parameter", 
                                         "Testing Parameter", "Testing Parameter"),
                           year = c(2016, 2017, 2017, 2017, 2016, 2017, 2017, 2017, 
                                    2016, 2017, 2017, 2017, 2016, 2017, 2017, 2017, 
                                    2016, 2017, 2017, 2017), 
                           quarter = c(4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 
                                       3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L), 
                           result = c(22.87, 31.78, 54.76, 44.3, 27.78, 34.04, 53.27, 
                                      46.83, 19.83, 34.05, 31.18, 35.7,  24.11, 33.57, 
                                      47.5, 40.53, 29.4, 34.6, 53.18, 46.16), 
                           count = c(3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
                                     3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), 
                           date = structure(c(17075, 17167, 17257, 17348, 17075, 
                                              17167, 17257, 17348, 17075, 17167, 
                                              17257, 17348, 17075, 17167,  17257, 
                                              17348, 17075, 17167, 17257, 17348), 
                                            class = "Date")), 
                      class = c("tbl_df", "tbl", "data.frame"), 
                      row.names = c(NA, -20L), 
                      .Names = c("site", "parameter", "year", "quarter", "result", 
                                 "count", "date"))

推荐答案

我认为线条没有显示,因为 ggplot 不知道哪些点连接在一起.我试图绘制第一个 graph(不使用 ggplotly 转换为 plotly 对象),&收到以下警告:

I think the lines weren't showing up because ggplot didn't know which points to connect together. I tried to plot the first graph (without converting to plotly object using ggplotly), & got the following warning:

> graph
geom_path: Each group consists of only one observation. Do you need to adjust the
group aesthetic?

group = site 添加到对我有用的第一组代码:

Adding group = site to first set of code worked for me:

library(ggplot2); library(dplyr)

graph <- data %>%
  ggplot(aes(x = date, y = result, 
             text = paste0("Site: ", site, "
", 
                           "Quarter: ", quarter, "
", 
                           "Year: ", year, "
", 
                           "Result: ", result),
             group = site)) +
  facet_wrap(~site) +
  geom_point() + # default palette since I don't have tidyquant
  geom_line() +
  theme_bw()

plotly::ggplotly(graph, tooltip = "text")

这篇关于在 ggplotly 中设置工具提示参数时,geom_line 不绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆