R中的cowplot问题:当面板化grobs并导出为pdf时,项目符号将变为椭圆

6mw9ycah  于 2023-07-31  发布在  其他
关注(0)|答案(2)|浏览(90)

我有一个相当特殊的问题,在面板与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)

字符串
x1c 0d1x的数据

# 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没有这个问题。

lsmd5eda

lsmd5eda1#

虽然您的代码确实在我的机器上呈现了带有项目符号的PDF(尽管已过时:请参见底部的会话信息),您可以尝试将save_plot替换为{ggplot 2}的ggsave,如下所示:

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

字符串
来自{cowplot}的documentation
此函数 * 的行为与ggplot 2中的ggsave()类似。主要的区别是,默认情况下,它不使用Dingbats字体进行pdf输出。Dingbats字体会导致某些pdf阅读器出现问题。

  • ggsave2save_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

yqkkidmi

yqkkidmi2#

  • 更新:*

虽然我还没有想出一个解决我自己的问题以上,我会张贴一个明显的变通方案在这里:为什么不导出到一个SVG文件,可以很容易地转换为PDF以后(如果有必要在所有)。
如下图所示,项目符号保留在生成的SVG文件中。

# Save the panel to an SVG file
save_plot("panel.svg", grid, nrow = 2, ncol = 1.5)

字符串
创建日期:2023年7月30日,使用reprex v2.0.2
x1c 0d1x的数据
显然,另一种方法是使用ggsave(),它产生了看似相同的结果:

# Alternative way:
ggsave(plot = grid, filename = "panel1.svg", height = 10, width = 7.5)


创建于2023-07-30带有reprex v2.0.2

相关问题