我正试图在我闪亮的应用程序中使用plotly进行交互式视觉效果。但是,在尝试向图表添加自定义工具提示时遇到问题。如果我在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")
如果我将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_line()
参数,则会绘制线条,但工具提示不再有效:
问题:
如何在绘制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"))
答案 0 :(得分:8)
我认为线条没有显示,因为ggplot不知道哪些点连接在一起。我尝试绘制第一个graph
(不使用ggplotly
转换为情节对象),&amp;得到以下警告:
> 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")