首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ggiraph -无法链接2个交互式条形图

ggiraph -无法链接2个交互式条形图
EN

Stack Overflow用户
提问于 2021-08-07 16:02:53
回答 1查看 66关注 0票数 0

我正在尝试使用ggiraph将两个交互式图形链接在一起,就像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的视频所示

这是我的数据

代码语言:javascript
复制
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部分:

代码语言:javascript
复制
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))

你能帮帮忙吗?非常感谢!

EN

回答 1

Stack Overflow用户

发布于 2021-08-08 07:37:33

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

代码语言:javascript
复制
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))

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68694024

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档