R Ggplot ggiraph - 交互式线显示两点之间的值

问题描述 投票:0回答:1

我正在尝试使用 geom_line_interactive 渲染交互式绘图。如下例所示,geom_line_interactive 仅显示整个图表中的第一个 x 值。有没有办法基本上显示数据集中给出的最接近的 x 值?

library(ggplot2)
library(ggiraph)
if( requireNamespace("dplyr", quietly = TRUE)){
  recent <- economics[economics$date > as.Date("2013-01-01"), ]
  gg = ggplot(recent, aes(date, unemploy)) +
    geom_step_interactive(aes(tooltip = date, data_id = date))
  x <- girafe(ggobj = gg)
  x <- girafe_options(x = x,
                      opts_hover(css = "stroke:red;") )
  if( interactive() ) print(x)
}

在图片中我希望在工具提示中看到“2013-07”

r ggplot2 linechart interactive ggiraph
1个回答
0
投票

正如here和David在评论中所提到的,

geom_path
系列中的功能(因此
_path
_step
,甚至
_line
)无法为不同的功能提供不同的工具提示沿着同一条路径进行观察。连接在同一行上的任何内容都会给出相同的工具提示。

据我所知,解决这个问题最简单的方法是添加一个 完全透明

geom_point_interactive
以及您想要的工具提示。由于
geom_point_interactive
中的每个点都没有连接,因此它们可以有多个工具提示。在您提供的示例中,以下内容应该足够了:

library(ggplot2)
library(ggiraph)

recent <- economics[economics$date > as.Date("2013-01-01"), ]
gg <- 
  ggplot(recent, aes(date, unemploy)) +
  geom_point_interactive(aes(tooltip = date), alpha = 0)+
  geom_step()
x <- girafe(ggobj = gg)
if( interactive() ) print(x)

请注意,我已关闭步骤图的交互性(因此红色悬停效果消失了)。然而,

geom_point_interactive
alpha = 0
让我们可以看到
date
的值。因此,如果将鼠标悬停在上方查看值比为系列着色更重要,这可能是一个不错的选择。

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.