gpt4 book ai didi

r - 在 ggplotly 中设置工具提示参数时,geom_line 不绘制

转载 作者:行者123 更新时间:2023-12-01 19:45:35 24 4
gpt4 key购买 nike

我正在尝试在我 Shiny 的应用程序中使用绘图来实现交互式视觉效果。但是,在尝试向图表添加自定义工具提示时遇到了问题。如果我设置 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")

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

Warning: Ignoring unknown aesthetics: text

Warning: Ignoring unknown aesthetics: text

如果我删除 text=仅来自 geom_line() 的论点函数,然后绘制线条,但工具提示不再起作用:

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

最佳答案

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

> 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

关于r - 在 ggplotly 中设置工具提示参数时,geom_line 不绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46241436/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com