I wonder if you can help me.
I often use the splendid ggiraph
, but have recently encountered an issue where it doesn't seem to work when one of its interactive geoms is paired with another geom (either from ggiraph
or ggplot2
). Let me explain with my example below and start with the data.
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))
In other words, the data looks like this:
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, -...
When I do things conventionally, all works fine:
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()
... generates:
However, when I try to use geom_point_interactive
instead of 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()
... I get the following:
(Note that I couldn't upload an
svg
file here, but can confirm that the tooltips from the geom_point_interactive
work fine.) Furthermore, the same behaviour seems to happen if I replace geom_boxplot
with geom_boxplot_interactive
.
So ... to get to my question: Do you know of a way to preserve the tooltips on the points, whilst having a boxplot that looks like it does in the first picture above?
As always, many thanks for looking.