首页 > 解决方案 > 将ggplot图和通过flextable获得的表格合并到网格中缺乏分辨率

问题描述

我正在尝试通过plot_grid使用获得的绘图ggplot2和使用获得的表格进行合并flextable()。我从其他问题中看到了将转换为光栅的过程,但我意识到这样做会损失表格的分辨率。线条变得太粗了。我想拥有在我看来更好的原始分辨率弹性表。是否可以?

在此处输入图像描述

在原始代码下方:

library(ggplot2)
library(tidyverse)
library(ggthemes)
library(patchwork)
library(gridExtra)
library(grid)
library(cowplot)
library(flextable)


#generic, reproducible data
Graph1 <- data.frame(x = 1:100, y = runif(100))

Graph <- ggplot(Graph, aes(x = x)) + geom_line(aes(y = y)) + scale_x_discrete(expand = c(0,0)) #control axis parameters






`Sample name` <- c("Media1", "Media2")
`Valore medio Peel (N)` <- c(4.35, 4.31)
`Valore massimo Peel (N)` <- c(5.5, 6.5)
df <- data.frame(`Sample name`, `Valore medio Peel (N)`, `Valore massimo Peel (N)`) 


df <- flextable(df)
df <- set_header_labels(df, Sample.name = "Sample name", `Valore.medio.Peel..N.` = "Valore medio peel (N)", `Valore.massimo.Peel..N.`= "Valore massimo Peel (N)")
df <- set_table_properties(df, layout = "autofit", width = .6)
df <- bold(df, bold = TRUE, part = "header")
df

##webshot::install_phantomjs() ##To be installed if needed

df <- as_raster(df, zoom = 2)
df <- ggplot() + theme_void() + annotation_custom(rasterGrob(df), xmin=-Inf, xmax=Inf, ymin=-Inf, ymax=Inf)


plot_grid(Graph, df, labels = NULL, ncol = 1, nrow = 2, scale = c(1, .75))

标签: rggplot2

解决方案


尝试zoom = 1as_raster()通话中设置。以下是设置zoom = 1vs生成的图像zoom = 2

df <- as_raster(df, zoom = 1)

缩放1

df <- as_raster(df, zoom = 2)

缩放2


推荐阅读