我一直在嘗試將 flextable 保存為 ggplot,然后將其寫入 PNG 以進行網格化。我得到了我需要的東西,除了生成的 PNG 的背景是灰色的,像這樣:

而 ggplot 的繪圖視圖看起來像我想要的(但解析度低):

這是我用來生成影像的代碼(flexRPOPS 是一個 flextable 物件):
library(flextable); library(ggplot2); library(png); library(magick);library(webshot)
flexRPOPS_raster <- as_raster(flexRPOPS, zoom = 20, webshot="webshot")
flexRPOPS_raster <- image_trim(flexRPOPS_raster, fuzz = 10)
flexRPOPS_ggplotex <- ggplot() annotation_custom(rasterGrob(flexRPOPS_raster), xmin=-Inf, xmax=Inf, ymin=-Inf, ymax=Inf)
flexRPOPS_compare_ex1 <- ggsave(filename = "deptovr_ex1.png", flexRPOPS_ggplotex, width = flexRPOPS_dims$widths, height = flexRPOPS_dims$heights, dpi = 600, units = "in", bg="#ffffff", device='png')
uj5u.com熱心網友回復:
問題是,當您繪制空圖時,默認面板背景為淺灰色。這就是您所看到的,因此您需要將面板背景設定為空白(或透明),然后它應該可以作業。這里有一個簡單的例子rectGrob,從grid給你的想法:
library(ggplot2)
library(grid)
p <- ggplot() annotation_custom(grob=rectGrob(width=0.5, height=0.5))
p
ggsave('a_gray.png', bg='#ffffff')

使用以下命令將面板背景設定為空白theme(panel.background=element_blank()):
p theme(panel.background = element_blank())
ggsave('a_white.png', bg='#ffffff')

或者,您可以設定theme.background = element_rect(fill=NA).
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/360786.html
上一篇:條形圖中的標準誤差條未分組
下一篇:如何在ggplot中保留所有圖例
