R 中的cowplot 问题:当对GROB 进行镶板并导出为PDF 时,项目符号会变成省略号

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

我在使用cowplot进行镶板并将结果导出为PDF文件时遇到了一个相当奇怪的问题。

考虑以下 MWE:

# Load libraries
library(consort)
library(cowplot)
library(ggplotify)

# Create data for flowchart A
participant_id <- 1:10
exclusion <- rep(NA, 10)
exclusion[1:3] <- "Lost to follow-up"
df <- data.frame(participant_id, exclusion)

# Create the flowchart A
flowchart_a <- consort_plot(data = df,
                            orders = c(participant_id = "Invited participants",
                                       exclusion = "Excluded",
                                       participant_id = "Completed the study"),
                            side_box = c("exclusion"),
                            cex = 0.9)
plot(flowchart_a)


# Destroy unneeded vectors
rm(participant_id, exclusion, df)

# Create data for flowchart B
participant_id <- 1:10
exclusion <- rep(NA, 10)
exclusion[1:2] <- "Lost to follow-up"
df <- data.frame(participant_id, exclusion)

# Create the flowchart B
flowchart_b <- consort_plot(data = df,
                            orders = c(participant_id = "Invited participants",
                                       exclusion = "Excluded",
                                       participant_id = "Completed the study"),
                            side_box = c("exclusion"),
                            cex = 0.9)
plot(flowchart_b)

创建于 2023-07-29,使用 reprex v2.0.2

# Destroy unneeded vectors
rm(participant_id, exclusion, df)

# Turn the consort_plot objects into graphical objects (= grobs) for paneling

grob1 <- as.grob(function() plot(flowchart_a))
grob2 <- as.grob(function() plot(flowchart_b))

# Create panel
grid <- plot_grid(grob1, NULL, grob2,
                  rel_heights = c(1, 0.3, 1),
                  labels = c("A", "", "B"),
                  ncol = 1)

# Save the panel to a PDF file
save_plot("panel.pdf", grid, nrow = 2, ncol = 1.5)

最后,当使用cowplot创建这些小图的面板并将其导出为PDF时,项目符号变成了椭圆(见下图)。奇怪的是,导出到 PNG 却没有这个问题。

r pdf flowchart cowplot grob
1个回答
0
投票

虽然您的代码确实在我的机器上渲染了带有项目符号的 PDF(尽管已经过时:请参阅底部的会话信息),但您可以尝试将

save_plot
替换为 {ggplot2} 的
ggsave
,如下所示:

ggsave(plot = grid, filename = "panel1.pdf", height = 10, width 7.5)

来自 {cowplot} 的 文档

这个函数*的行为就像 ggplot2 中的 ggsave() 一样。主要区别在于默认情况下它不使用 Dingbats 字体进行 pdf 输出。 Dingbats 字体会导致某些 pdf 阅读器出现问题。

*

ggsave2
save_plot

的基础
> sessionInfo()
R version 4.1.1 (2021-08-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19045)

Matrix products: default
 
attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] ggplotify_0.1.0 cowplot_1.1.1   consort_1.2.0  

loaded via a namespace (and not attached):
 [1] magrittr_2.0.3     tidyselect_1.2.0   munsell_0.5.0      colorspace_2.1-0  
 [5] R6_2.5.1           ragg_1.2.4         rlang_1.1.0        fansi_1.0.3       
 [9] dplyr_1.1.2        tools_4.1.1        grid_4.1.1         gtable_0.3.3      
[13] utf8_1.2.2         cli_3.6.1          withr_2.5.0        gridGraphics_0.5-1
[17] systemfonts_1.0.4  tibble_3.2.1       lifecycle_1.0.3    textshaping_0.3.6 
[21] farver_2.1.1       ggplot2_3.4.2      vctrs_0.6.1        yulab.utils_0.0.6 
[25] glue_1.6.2         labeling_0.4.2     compiler_4.1.1     pillar_1.9.0      
[29] generics_0.1.3     scales_1.2.1       pkgconfig_2.0.3  
© www.soinside.com 2019 - 2024. All rights reserved.