0

I'm trying to use ggiraph to link 2 interactive graphs together as Sharon has shown here : https://www.infoworld.com/article/3626911/easy-interactive-ggplot-graphs-in-r-with-ggiraph.html

I managed to plot both graphs side by side. When I hover the mouse over a bar, it pops up with extra info and the bar 'lights up' but only "individually"

Problem: I cannot get them linked. Ideally I would like the related bars in both plots to light up simultaneously, as shown in video here at minute 6.25 https://www.youtube.com/watch?v=12QaZp1FokU&t=421s

This is my data

library(tidyverse)
library(here)
library(janitor)
library(skimr)
library(RColorBrewer)
library(ggiraph)
library(patchwork)
library(Rcpp)

structure(list(statistical_area = c("Baulkham Hills and Hawkesbury", 
"Baulkham Hills and Hawkesbury", "Blacktown", "Blacktown", "Central Coast", 
"Central Coast", "City and Inner South", "City and Inner South", 
"Eastern Suburbs", "Eastern Suburbs", "Inner South West", "Inner South West", 
"Inner West", "Inner West", "North Sydney and Hornsby", "North Sydney and Hornsby", 
"Northern Beaches", "Northern Beaches", "Outer South West", "Outer South West", 
"Outer West and Blue Mountains", "Outer West and Blue Mountains", 
"Parramatta", "Parramatta", "Ryde", "Ryde", "South West", "South West", 
"Sutherland", "Sutherland"), year = c("x2019", "x2020", "x2019", 
"x2020", "x2019", "x2020", "x2019", "x2020", "x2019", "x2020", 
"x2019", "x2020", "x2019", "x2020", "x2019", "x2020", "x2019", 
"x2020", "x2019", "x2020", "x2019", "x2020", "x2019", "x2020", 
"x2019", "x2020", "x2019", "x2020", "x2019", "x2020"), cases = c(7329, 
8758, 45657, 47669, 30877, 30840, 65274, 67286, 18695, 19416, 
41880, 42581, 15860, 16223, 19178, 18800, 12198, 11483, 28751, 
27775, 33910, 35107, 41824, 41538, 7611, 7674, 41396, 45330, 
15997, 17418), crime_per_day = c(20.08, 23.99, 125.09, 130.6, 
84.59, 84.49, 178.83, 184.35, 51.22, 53.19, 114.74, 116.66, 43.45, 
44.45, 52.54, 51.51, 33.42, 31.46, 78.77, 76.1, 92.9, 96.18, 
114.59, 113.8, 20.85, 21.02, 113.41, 124.19, 43.83, 47.72)), row.names = c(NA, 
-30L), class = c("tbl_df", "tbl", "data.frame"))

and the plot section:

plot_2019<-crimeperday %>%
  filter(year=="x2019") %>% 
  ggplot(aes(x=reorder(statistical_area,crime_per_day),y=crime_per_day))+
  geom_col_interactive(aes(tooltip=crime_per_day,data_id=crime_per_day))+
  coord_flip()

plot_2020<-crimeperday %>%
  filter(year=="x2020") %>% 
  ggplot(aes(x=reorder(statistical_area,crime_per_day),y=crime_per_day))+
  geom_col_interactive(aes(tooltip=crime_per_day,data_id=crime_per_day))+
  coord_flip()

girafe(code=print(plot_2019+plot_2020))

Could you please help? thank you so much!

Fern
  • 1
  • 1
  • 1
    Welcome to SO! To help us to help would you mind making your issue reproducible by sharing a sample of your **data**? See [how to make a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) To post your data type `dput(NAME_OF_DATASET)` into the console and copy & paste the output starting with `structure(....` into your post. – stefan Aug 07 '21 at 20:00
  • 1
    ... just a guess. But try with mapping `statistical_area` on `data_id`. To link the graphs you need a "fixed" key or id in both datasets or graphs. Otherwise how should `ggiraph` know which observations you want to link. – stefan Aug 07 '21 at 20:02
  • 1
    Hi Stefan, thank you for your reply! I was hoping that by using `+` in `girafe` on the last line, R will know to link the 2 graphs together, like in example [link](https://www.infoworld.com/article/3626911/easy-interactive-ggplot-graphs-in-r-with-ggiraph.html) – Fern Aug 07 '21 at 23:53
  • ps. I added my data type to the original post. thanks for the guide, it's my first post here (and first week using R). :) – Fern Aug 08 '21 at 00:13

1 Answers1

0

To solve your issue you have to map the statistical_area on data_id. To link the graphs you need a "fixed" key or id in both datasets or graphs. Otherwise how should ggiraph know which observations you want to link.

library(ggiraph)
library(ggplot2)
library(patchwork)
library(dplyr)

plot_2019<-crimeperday %>%
  filter(year=="x2019") %>% 
  ggplot(aes(x=reorder(statistical_area,crime_per_day),y=crime_per_day))+
  geom_col_interactive(aes(tooltip=crime_per_day,data_id=statistical_area))+
  coord_flip()

plot_2020<-crimeperday %>%
  filter(year=="x2020") %>% 
  ggplot(aes(x=reorder(statistical_area,crime_per_day),y=crime_per_day))+
  geom_col_interactive(aes(tooltip=crime_per_day,data_id=statistical_area))+
  coord_flip()

girafe(code=print(plot_2019+plot_2020))

enter image description here

stefan
  • 90,330
  • 6
  • 25
  • 51