0

我正在尝试使用 ggiraph 将 2 个交互式图表链接在一起,如 Sharon 所示:https ://www.infoworld.com/article/3626911/easy-interactive-ggplot-graphs-in-r-with-ggiraph.html

我设法并排绘制了两个图表。当我将鼠标悬停在一个栏上时,它会弹出额外的信息,并且栏会“亮起”,但只是“个别”

问题:我无法将它们链接起来。理想情况下,我希望两个图中的相关条同时亮起,如第 6.25 分钟的视频所示https://www.youtube.com/watch?v=12QaZp1FokU&t=421s

这是我的数据

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"))

和情节部分:

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))

能否请你帮忙?太感谢了!

4

1 回答 1

0

要解决您的问题,您必须statistical_areadata_id. 要链接图表,您需要在两个数据集或图表中都有一个“固定”键或 id。否则 ggiraph 应该如何知道您想要链接哪些观察结果。

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))

在此处输入图像描述

于 2021-08-08T07:37:32.993 回答