I'm using ggiraph
version 0.8.13
to make an interactive line plot with geom_line_interactive()
in ggplot2
. My goal is to highlight each line when hovering over it using data_id
, but I'm encountering the following warning:
Warning messages:
1: In set_attr(name = attrName, ids = as.integer(ids), values = attrValue) :
Failed setting attribute 'title', mismatched lengths of ids and values (most often, it occurs because of clipping or because of NAs in data)
2: In set_attr(name = attrName, ids = as.integer(ids), values = attrValue) :
Failed setting attribute 'data-id', mismatched lengths of ids and values (most often, it occurs because of clipping or because of NAs in data)
Here's a minimal reproducible example:
# Libraries
library(ggplot2)
library(ggiraph)
# Sample data
df <- data.frame(
id = rep(c("A", "B"), each = 5),
time = rep(1:5, times = 2),
value = c(5, 5, 5, 7, 9, # Group A
1, 5, NA, 6, 8) # Group B — has an NA at position 3
)
# Interactive plot
p <- ggplot(df, aes(x = time, y = value, group = id)) +
geom_line_interactive(
aes(tooltip = id, data_id = id, color = id)
)
girafe(
ggobj = p,
options = list(
opts_hover(css = "stroke-width:3px;"),
opts_hover_inv(css = "opacity:0.2;")
)
)
The issue seems to happen because of the NA
in the value
column, which breaks the line into two disconnected segments. From what I understand, ggiraph
tries to assign data_id
and tooltip
to each line segment, but when there are multiple segments for the same id
, it fails to match the values properly — leading to this error.
My Questions:
Why exactly does this mismatch happen?
Is it because a single id
produces multiple disconnected line segments, and ggiraph
expects a 1-to-1 match between data_id
and each rendered segment?
Is there a clean way to fix this without modifying the original data, or without manually splitting the lines?
I don't think there is a clean way to achieve this, without some data manipulation. Although I would categorize this as a bug, worthy of getting reported; there was a similar (not the same) bug with geom_map_interactive()
that was fixed a while ago.
You can alter the group
within ggplot
as I've shown below, but I'd recommend something like : df %>% mutate(unique_id = paste0(id, "_seg", cumsum(c(1, diff(is.na(value)) == -1))), .by = id)
beforehand. In any case, this works:
ggplot(df, aes(x = time, y = value,
group = paste0(id, "_seg", cumsum(c(1, diff(is.na(value)) == -1))))) +
geom_line_interactive(
aes(tooltip = paste("ID:", id),
data_id = id,
color = id)) -> p
girafe(ggobj = p,
options = list(opts_hover(css = "stroke-width:3px;"),
opts_hover_inv(css = "opacity:0.2;")))
#> Warning: Removed 1 row containing missing values or values outside the scale range
Created on 2025-07-10 with reprex v2.1.1
See the interactive plot on CodePen: https://codepen.io/Mdoubledash/pen/myedpYr?editors=1000