Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hover Misaligned for geom_line_interactive() #248

Open
raymondnicholas4 opened this issue Jan 3, 2023 · 5 comments
Open

Hover Misaligned for geom_line_interactive() #248

raymondnicholas4 opened this issue Jan 3, 2023 · 5 comments
Assignees
Labels

Comments

@raymondnicholas4
Copy link

raymondnicholas4 commented Jan 3, 2023

Since updating my packages, I've been getting strange behavior when hovering over lines created by geom_line_interactive(). Lines get the hover effect even when the mouse is far from the line, and sometimes the line won't get the hover effect when the mouse is directly over it. This seems to only be a problem for line geoms (see the code below).

library(ggplot2)
library(ggiraph)

df = data.frame(
    x = rep(1:5, 4),
    y = c(1:5, (1:5)^2, 4,2,1,2,4, 5,7,8,7,5),
    group = as.character(rep(1:4, each = 5)),
    color = as.character(c(rep(1, 15), rep(2, 5)))
)

gg = ggplot(df, aes(x, y, color = color, group = group, data_id = group, tooltip = group))

girafe(ggobj = gg + geom_line_interactive() + geom_point_interactive()) # Causes a problem
girafe(ggobj = gg + geom_line_interactive()) # Still causes a problem
girafe(ggobj = gg + geom_point_interactive()) # No problem

Version numbers:
ggplot2 3.4.0
ggiraph 0.8.5
R 4.2.2

Edit I think I found the issue. The geom_line_interactive() function uses the same "hitbox" as geom_polygon_interactive().

@davidgohel
Copy link
Owner

Sorry, it seems we activated hover_nearest=TRUE in geom_line_interactive(). For now, you can switch it off:

library(ggplot2)
library(ggiraph)

df = data.frame(
  x = rep(1:5, 4),
  y = c(1:5, (1:5)^2, 4,2,1,2,4, 5,7,8,7,5),
  group = as.character(rep(1:4, each = 5)),
  color = as.character(c(rep(1, 15), rep(2, 5)))
)

gg = ggplot(df, aes(x, y, color = color, group = group, data_id = group, tooltip = group))

girafe(ggobj = gg + geom_line_interactive(hover_nearest = FALSE))

@raymondnicholas4
Copy link
Author

raymondnicholas4 commented Jan 25, 2023

Sorry, it seems we activated hover_nearest=TRUE in geom_line_interactive(). For now, you can switch it off:

library(ggplot2)
library(ggiraph)

df = data.frame(
  x = rep(1:5, 4),
  y = c(1:5, (1:5)^2, 4,2,1,2,4, 5,7,8,7,5),
  group = as.character(rep(1:4, each = 5)),
  color = as.character(c(rep(1, 15), rep(2, 5)))
)

gg = ggplot(df, aes(x, y, color = color, group = group, data_id = group, tooltip = group))

girafe(ggobj = gg + geom_line_interactive(hover_nearest = FALSE))

Hi @davidgohel , thanks for the reply and for the dedication to this awesome package. I tried your suggestion, but I'm getting the same unexpected behavior. For example, hovering over the group 1 line between x=2 and x=4 highlights the group 3 line because that polygon is considered on top of the group 1 polygon.

EDIT: Accidentally closed the issue.

@davidgohel
Copy link
Owner

ok, sorry I understand now

yes, your analysis is the correct one.

@davidgohel davidgohel added the bug label Jan 25, 2023
@OlaCaster
Copy link

I also noticed this bug just now when upgrading R version and packages. In my case I was plotting ROC curves and the hovering behaviour was very different from the expected. The only workaround I was able to come up with was to split each line into shorter pieces, since the "hitbox" from each piece becomes much smaller than from the entire line.

@sigmapi sigmapi self-assigned this Sep 4, 2023
@OlaCaster
Copy link

@davidgohel @sigmapi do you have any updates on this issue? As much as I appreciate your package and promote its use, I find this particular issue to severely limit the usefulness of the package when plotting lines and paths. The problem is now further compounded by more recently introduced bugs related to lines - I'm thinking primarily of #299 that I have also experienced.

I constructed another example to try to illustrate just how limiting this issue can be. If there are other elements, e.g. points, that end up inside a path's hitbox, their interactive properties can disappear entirely. In this case the path takes the shape of a triangle (together with the x-axis), and it is impossible to interact with the six points that are inside this triangle.

library(ggplot2)
library(ggiraph)

# Define ggplot with only points
gg1 <- ggplot(data=data.frame(x=c(0.5, rep(1:3, 2)), y=c(2, rep(c(0.5,1), each=3)))) +
  geom_point_interactive(aes(x=x, y=y, data_id="point", tooltip="This is a point")) +
  scale_x_continuous(limits=c(0,4)) +
  scale_y_continuous(limits=c(0,3)) +
  theme_minimal()

# Define ggplot with both points and lines
gg2 <- gg1 +
  geom_path_interactive(data=data.frame(x=c(0,2,4), y=c(0,3,0)),
                        aes(x=x, y=y, data_id="line", tooltip="This is a line (or is it?)"), color="blue")

# Only points: Works as expected
girafe(ggobj=gg1)

# Points and line:
#  - Line hover is active inside the entire triangle
#  - Points inside the triangle cannot be hovered at all
girafe(ggobj=gg2)

I tried to look into the source code and I appreciate that this might not be entirely straightforward to resolve. Nevertheless, I would encourage you to consider this a prioritized issue to fix. From the user perspective, the desired behaviour would be for lines and paths to be treated merely as sets of connected points without considering the area they enclose. (For this we can always use polygons.)

Many thanks for a great package and for all the work you have put into it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants