问题
我想制作一个漂亮的表格,其中一列的单元格中有 ggplots。一个关键因素是我想最终创建该表的 pdf 输出。
到目前为止我尝试过的
希望下面的例子是可以理解的。基本上我发现我可以使用 gt 包实现我想要的。问题是这会创建一个 html 小部件,然后您必须使用 phantomJS 和 webshot 将其导出为 pdf。
library(dplyr)
library(purrr)
library(gt)
library(ggplot2)
dat = tibble(
RowLabel = letters[1:5],
Numeric = seq(100,500,100)
) %>%
mutate(
plotData = RowLabel %>% map(function(pos){
tibble(y=runif(10)*100) %>%
arrange(desc(y)) %>%
mutate(x=row_number())
}),
plot_obj = plotData %>% map(function(df){
df %>%
ggplot(aes(x=x,y=y))+
geom_col()
}),
plot_grob = plot_obj %>% map(cowplot::as_grob)
)
tab = dat %>%
select(RowLabel, Numeric) %>%
mutate(
ggplot = NA
) %>%
gt() %>%
text_transform(
locations = cells_body(vars(ggplot)),
fn = function(x) {
dat$plot_obj %>%
map(ggplot_image, height = px(50))
}
)
tab
我想要什么
我想要一个类似于上面例子的输出。但是,我想要一个不需要我使用 html 小部件并且可以直接保存为 pdf 而无需使用其他程序的解决方案。这可以使用ggplot吗?我已经开始更多地了解网格/grobs/gtables 等,但没有取得任何有意义的进展。
提前致谢!