$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>r - Interactive facet plot with ggiraph: not responding to mouse-over - Stack Overflow|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

r - Interactive facet plot with ggiraph: not responding to mouse-over - Stack Overflow

matteradmin12PV0评论

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 badges
Add a comment  | 

2 Answers 2

Reset to default 1

From 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

Post a comment

comment list (0)

  1. No comments so far