更改桑基图中的字体并将绘图保存为 PDF 格式

问题描述 投票:0回答:1

我正在使用“sfo”包来准备桑基图。下面,您可以看到我的代码和数据

library(sfo)
library(dplyr, warn = FALSE)
library(plotly)


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)

现在我想改变这个图中的两件事。首先是更改字体并使用粗体 Arial 字体,其次是将此图表导出为 PDF。有人可以帮忙解决这个问题吗?

r plotly sankey-diagram
1个回答
2
投票

使用

plotly::layout
,您可以修改绘图的各种属性,例如字体系列。
要将绘图保存为 PDF 格式,可以使用
webshot
包。

library(sfo)
library(dplyr)
library(plotly)

p <- 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") %>%
  add_annotations(c("Terminals", "Domestic/Int"), x = c(0.2, 0.5), y = c(1, 1), showarrow = FALSE) %>%
  layout(font=list(family="Arial", size=30, color="red"), margin=list(t=100))

# Save the plotly plot to an HTML file
library(htmlwidgets)
saveWidget(p, file="sankey_plot.html", selfcontained=TRUE)

# Take a screenshot of the plot saved in the html file
library(webshot)
webshot::install_phantomjs()
webshot("sankey_plot.html", "sankey_plot.pdf", vwidth = 1984, vheight = 1088, zoom=0.6)

参数

vwidth
vheight
zoom
需要根据图表的具体特点进行微调。

© www.soinside.com 2019 - 2024. All rights reserved.