我正在试着做一张桑基图表.下面您可以看到我的代码和数据:

library(dplyr)
library(sfo)

sfo_passengers %>%
  filter(activity_period == max(activity_period)) %>%
  group_by(activity_type_code, geo_region) %>%
  summarise(total = sum(passenger_count), .groups = "drop")

sfo_passengers %>% 
  filter(operating_airline == "United Airlines",
         activity_period >= 201901 & activity_period < 202001) %>%
  mutate(terminal = ifelse(terminal == "International", "international", terminal)) %>%
  group_by(operating_airline,activity_type_code, geo_summary, geo_region,  terminal) %>%
  summarise(total = sum(passenger_count), .groups = "drop") %>%
  sankey_ly(cat_cols = c("operating_airline", "terminal","geo_summary", "geo_region", "activity_type_code"), 
            num_col = "total",
            title = "Dist. of United Airlines Passengers at SFO During 2019")

This code produces a chart without titles. Now, I want to add titles circled with a yellow color. Can anyone help me with how to do that? enter image description here

推荐答案

由于sankey_ly()函数使用ploly来创建Plot,因此让我们使用Last_Plot()和Add_Annotation()函数来获取和增强该图,如下所示:

library(sfo)

sfo_passengers %>%
  filter(activity_period == max(activity_period)) %>%
  group_by(activity_type_code, geo_region) %>%
  summarise(total = sum(passenger_count), .groups = "drop")

sfo_passengers %>% 
  filter(operating_airline == "United Airlines",
         activity_period >= 201901 & activity_period < 202001) %>%
  mutate(terminal = ifelse(terminal == "International", "international", terminal)) %>%
  group_by(operating_airline,activity_type_code, geo_summary, geo_region,  terminal) %>%
  summarise(total = sum(passenger_count), .groups = "drop") %>%
  sankey_ly(cat_cols = c("operating_airline", "terminal","geo_summary", "geo_region", "activity_type_code"), 
            num_col = "total",
            title = "Dist. of United Airlines Passengers at SFO During 2019")

p <- plotly::last_plot()

plotly::add_annotations(p, c("Terminals", "Domestic/Int"), x = c(0.2, 0.5), y = c(1, 1), showarrow = FALSE)

R相关问答推荐

是否可以 Select 安装不带文档的R包以更有效地存储?

ggplot 2中的地块底图(basemaps_gglayer()不起作用)

查找具有平局的多个列的最大值并返回列名或平局 destruct 者NA值

如何得到R中唯一的组合群?

用R ggplot2求上、下三角形中两个变量的矩阵热图

以NA为通配符的R中的FULL_JOIN以匹配其他数据中的任何值.Frame

函数可以跨多个列搜索多个字符串并创建二进制输出变量

汇总数据帧中的复制列,保持行的唯一性

从多层嵌套列表构建Tibble?

将项粘贴到向量中,并将它们分组为x的倍数,用空格分隔

R+reprex:在呈现R标记文件时创建可重现的示例

随机 Select 的非NA列的行均数

对R中的列表列执行ROW Mean操作

通过R:文件名未正确写入[已解决]将.nc文件转换和导出为.tif文件

ggplot斜体轴刻度标签中的单个字符-以前的帖子建议不工作

R将函数参数传递给ggploy

真实世界坐标的逆st_变换

通过分析特定列中的字符串在数据框中创建新的行和列

创建由三个单独的shapefile组成的单个 map

Ggplot2水平线和垂直线的图例图标不匹配