代码之家  ›  专栏  ›  技术社区  ›  tbradley

在ggplotly中设置工具提示参数时,geom\U线未打印

  •  5
  • tbradley  · 技术社区  · 7 年前

    tooltip 中的参数 ggplotly() geom_line() 在我的输出中被忽略,但工具提示有效。

    以下是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, "\n", 
                                         "Quarter: ", quarter, "\n", 
                                         "Year: ", year, "\n", 
                                         "Result: ", result))) +
      facet_wrap(~site) +
      geom_point(color = palette_dark()[1]) +
      geom_line(color = palette_dark()[1]) +
      theme_bw() 
    
     ggplotly(graph, 
              tooltip = "text")
    

    这给了我: enter image description here

    data = aes() 来自 ggplot 呼叫个人 geom

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

    这给了我同样的输出,工具提示仍然有效。然而,现在我收到两个警告:

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

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

    text= 仅从 geom_线() 函数,然后绘制线,但工具提示不再起作用:

    enter image description here

    如何使自定义工具提示在仍打印的情况下工作 geom_线()

    我的数据:

    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"))
    
    1 回复  |  直到 5 年前
        1
  •  10
  •   Z.Lin    7 年前

    我认为这些线没有出现,因为ggplot不知道哪些点要连接在一起。我试图策划第一个 graph ggplotly ),&收到以下警告:

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

    正在添加 group = site

    library(ggplot2); library(dplyr)
    
    graph <- data %>%
      ggplot(aes(x = date, y = result, 
                 text = paste0("Site: ", site, "\n", 
                               "Quarter: ", quarter, "\n", 
                               "Year: ", year, "\n", 
                               "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")
    

    plotly