2017-09-15 4 views
1

私は、光沢のあるアプリでインタラクティブなビジュアルをプロットすることを試みています。しかし、私はグラフにカスタムツールチップを追加しようとすると問題に遭遇しています。 tooltip引数をggplotly()に設定した場合、私の出力ではgeom_line()は無視されますが、ツールチップが機能します。ここでgeom_line ggplotlyにtooltip引数が設定されているときにプロットしない

は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") 

これは私にこの与える:私は、個々のgeom sのggplot呼び出しからdata =aes()呼び出しを移動する場合は、行がまだ」上がらない enter image description here

をt:

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

これにより、ツールチップがまだ機能している同じ出力。私はその後、ちょうどgeom_line()関数からtext=引数を削除した場合、テキスト

警告:不明な美学を無視:テキスト

警告:不明な美学を無視しかし、今私は2回の警告を受けます線はプロットされていますが、ツールチップは機能しなくなりました。

enter image description here

質問:

は、どのように私はまだ私の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")) 

答えて

5

私はggplotが一緒に接続するために指すかわからなかったので、ラインが現れていなかったと思います。

> 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

Iは( ggplotlyを使用してオブジェクトをplotlyに変換することなく)第をプロットしようとし、&は、次の警告を得
+0

それはトリックです!私は 'facet_wrap'コールが内部的にそれを処理することを期待していましたが、今私は知っています!ありがとう! – tbradley

+1

もっと複雑なチャートでは、各ファセット内に複数の線が必要な場合があるので、個別に処理することをお勧めします。 :) –

関連する問題