My aim is to generate an interactive combined line plot and facet dot plot in R. Moving the mouse over one of the lines should highlight it together with the corresponding dot plot facet, and vice versa. I have been trying to follow Albert Rapp's approach using ggiraph together with patchwork.
However, at the moment I am stuck at generating the interactive facet plot. Here is an example for the code which produces an unresponsive facet plot:
library(ggirafe)
library(ggplot2)
# Sample data
data <-
data.frame(
x = rnorm(100),
y = rnorm(100),
facet = rep(c("A", "B", "C"),
length.out = 100)
)
# Create the ggplot
p <-
ggplot(
data, aes(x, y),
data_id = facet
) +
geom_point() +
facet_wrap_interactive(~facet)
# Create the girafe object with hover options
girafe(
ggobj = p,
options =
list( opts_hover(css = ""),
opts_hover_inv(css = "opacity:0.1;")
)
)
Thanks for any suggestions!
My aim is to generate an interactive combined line plot and facet dot plot in R. Moving the mouse over one of the lines should highlight it together with the corresponding dot plot facet, and vice versa. I have been trying to follow Albert Rapp's approach using ggiraph together with patchwork.
However, at the moment I am stuck at generating the interactive facet plot. Here is an example for the code which produces an unresponsive facet plot:
library(ggirafe)
library(ggplot2)
# Sample data
data <-
data.frame(
x = rnorm(100),
y = rnorm(100),
facet = rep(c("A", "B", "C"),
length.out = 100)
)
# Create the ggplot
p <-
ggplot(
data, aes(x, y),
data_id = facet
) +
geom_point() +
facet_wrap_interactive(~facet)
# Create the girafe object with hover options
girafe(
ggobj = p,
options =
list( opts_hover(css = ""),
opts_hover_inv(css = "opacity:0.1;")
)
)
Thanks for any suggestions!
Share Improve this question edited Nov 16, 2024 at 7:31 stefan 128k6 gold badges38 silver badges77 bronze badges asked Nov 15, 2024 at 21:11 ThomasWThomasW 114 bronze badges2 Answers
Reset to default 1From my understanding and a look at the ggiraph book making the facet strips interactive requires using labeller_interactive
to set the data_id
.
library(ggiraph)
library(ggplot2)
set.seed(123)
# Sample data
data <-
data.frame(
x = rnorm(100),
y = rnorm(100),
facet = rep(c("A", "B", "C"),
length.out = 100
)
)
p <-
ggplot(
data, aes(x, y)
) +
geom_point() +
facet_wrap_interactive(
~facet,
labeller = labeller_interactive(
aes(
tooltip = paste("this is facet", facet),
data_id = facet
)
)
)
girafe(
ggobj = p,
options =
list(
opts_hover(""),
opts_hover_inv(css = "opacity:0.1;")
)
)
Thanks for your help! Here is a slightly modified code that produces the kind of figure I wanted:
p <- ggplot(data, aes(x, y)) +
geom_point_interactive(aes(data_id = facet)) +
facet_wrap_interactive(
~facet,
interactive_on = "both",
labeller = labeller_interactive(aes(data_id = facet))
) +
theme(
strip.background = element_blank(),
strip.text.x = element_text(colour = "black", size = 12)
)
girafe(
ggobj = p,
options = list(
opts_hover(""),
opts_hover_inv(css = "opacity:0.1;")
)
)
I think the problems were 1. mouse-over needs to be rather precisely on the text in the facet strip - here a single letter, 2. in the original code, data_id = facet was part of the global aestethics, but it seems it needs to be placed inside labeller_interactive to make the facets interactive
enter image description here