1

我想知道你是否可以帮助我。

我经常使用 splendid ,但最近遇到了一个问题,当它的一个交互式 geom 与另一个 geom (来自或)ggiraph配对时,它似乎不起作用。让我用下面的例子来解释,从数据开始。ggiraphggplot2

library(ggiraph)
library(tidyverse)
dput(data)

structure(list(moniker = c("OU", "IN", "AI", "HR", "T ", "LA", 
"AJ", "WC", "BE", "NV"), monthly = c(-0.03489, -0.028034, -0.0374, 
-0.0479, -0.032485, -0.0332, -0.045954, -0.048032, -0.0446, -0.027724
), quarterly = c(-0.096452525515629, -0.0642786819909756, -0.12256466734, 
-0.112914230016, -0.121439751549064, -0.12779609, -0.103490246054841, 
-0.152770470354658, -0.12167068725, -0.0993823354535133), yearly = c(-0.107231010977261, 
-0.00722368758143395, -0.088470240158463, -0.0817767947388421, 
-0.136265993838053, -0.0970216743424386, -0.092159717986092, 
-0.174532024103611, -0.131514750319878, -0.0866996056676958)), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -10L))

换句话说,数据如下所示:

glimpse(data)

Observations: 10
Variables: 4
$ moniker   <chr> "OU", "IN", "AI", "HR", "T ", "LA", "AJ", "WC", "BE", "NV"
$ monthly   <dbl> -0.034890, -0.028034, -0.037400, -0.047900, -0.032485, -0...
$ quarterly <dbl> -0.09645253, -0.06427868, -0.12256467, -0.11291423, -0.12...
$ yearly    <dbl> -0.107231011, -0.007223688, -0.088470240, -0.081776795, -...

当我按常规做事时,一切正常:

data %>% 
  gather(key = "timeframe", value = "return", -moniker) %>% 
  mutate(timeframe = as_factor(timeframe)) %>% 
  ggplot(
    aes(
      x = timeframe, 
      y = return
      )
    ) + 
  geom_hline(yintercept = 0) + 
  geom_point(position = position_jitter(w = 0.15, h = 0)) + 
  geom_boxplot()

...生成:

在此处输入图像描述

但是,当我尝试使用geom_point_interactive而不是geom_point

data %>% 
  gather(key = "timeframe", value = "return", -moniker) %>% 
  mutate(timeframe = as_factor(timeframe)) %>% 
  ggplot(
    aes(
      x = timeframe, 
      y = return,
      tooltip = moniker
      )
    ) + 
  geom_hline(yintercept = 0) + 
  geom_point_interactive(position = position_jitter(w = 0.15, h = 0)) + 
  geom_boxplot()

...我得到以下信息:

在此处输入图像描述 (请注意,我无法svg在此处上传文件,但可以确认工具提示geom_point_interactive工作正常。)此外,如果我替换geom_boxplotgeom_boxplot_interactive.

所以......回答我的问题:你知道一种方法来保留点上的工具提示,同时有一个看起来像上面第一张图片中的箱线图吗?

一如既往,非常感谢您的关注。

4

1 回答 1

2

问题是它geom_boxplot是继承的tooltip = moniker,这意味着每个moniker人都会有自己的盒子,为了避免这种举动,tooltip审美geom_point_interactive就像这样:

my_data %>% 
  gather(key = "timeframe", value = "return", -moniker) %>% 
  mutate(timeframe = as_factor(timeframe)) %>% 
  ggplot(
    aes(
      x = timeframe, 
      y = return
    )
  ) + 
  geom_hline(yintercept = 0) + 
  geom_boxplot() + 
  geom_point_interactive(aes(tooltip = moniker), 
                         position = position_jitter(w = 0.15, h = 0))
于 2019-02-13T13:17:26.250 回答