绘制xtable或将其保存为图像 [英] plot xtable or save it as image

查看:143
本文介绍了绘制xtable或将其保存为图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 powerpoint 演示文稿中使用 xtable 布局,我需要一种直接转换 data.frame 转换为 xtable 的图片或图表,例如 here 理想的解决方案会给我一个 ggplot 对象,因为我从那里获得了最大的灵活性,但是只要我能看到 xtable 在一个图片(光栅或矢量)或绘图窗口中。 使用 ggplot2 grid.extra 我们可以实现体面的表格:

  library(ggplot2)
library(gridExtra)
ggplot()+ annotation_custom(tableGrob(head(iris)))+ theme_void()

tableGrob 有一个主题参数,允许足够的灵活性来重新生成接近 xtable 的东西。请参阅这个小插曲



这里有一个方便的函数来做更多的事情,你需要包 grid gtable
pre $ lt; code> table_plot< - function(x,
theme_fun = gridExtra :: ttheme_minimal,
base_size = 12,
base_colour =black,
base_family =,
parse = FALSE,
padding = unit(c(4,4),mm ),
col = base_colour,
lwd = 1,
lty = 1
){
g< - gridExtra :: tableGrob(x,
theme = theme_fun(base_size,base_colour,base_family,parse,padding))
分隔符< - replicate(ncol(g) - 2,
grid :: segmentsGrob(x1 = gpar(col = col,lwd = lwd,lty = lty)),
simplification = FALSE)
g < - gtable :: gtable_add_grob(g, grobs =分隔符,
t = 2,b = nrow(g),l = seq_len(ncol(g)-2)+2)
ggplot2 :: ggplot()+ ggplot2 :: annotation_custom(g) + ggplot2 :: theme_void()
}

简单示例:

  table_plot(head(iris))

复杂的例子:

  iris2 < -  setNames(head(iris)[1:3],c( alpha * integral(xdx,a,infinity),this text \\\
is high,'alpha / beta'))
table_plot(iris2,
base_size = 10,
base_colour = darkred,
base_family =serif,
parse = TRUE,
theme = ttheme_default,
lwd = 3,
lty = 2,
col =darkblue)


I want to use xtable layouts in my powerpoint presentations and I need a way to directly convert a data.frame into a picture or plot of an xtable such as the ones displayed here.

The ideal solution would give me a ggplot object as I have the most flexibility from there, but I can work with another output as long as I can see a xtable in a pic (raster or vector) or plot window.

解决方案

Using ggplot2 and grid.extra we can achieve decently looking tables:

library(ggplot2)
library(gridExtra)
ggplot() + annotation_custom(tableGrob(head(iris))) + theme_void()

tableGrob has a theme parameter that allows enough flexibility to reproduce something close to xtable. see this vignette.

Here's a convenient function to do a bit more, you'll need packages grid and gtable :

table_plot <- function(x,
                       theme_fun= gridExtra::ttheme_minimal,
                       base_size = 12,
                       base_colour = "black",
                       base_family = "",
                       parse = FALSE,
                       padding = unit(c(4, 4), "mm"),
                       col = base_colour,
                       lwd=1,
                       lty=1
                       ){
  g <- gridExtra::tableGrob(x,
    theme = theme_fun(base_size, base_colour, base_family, parse, padding))
  separators <- replicate(ncol(g) - 2,
                          grid::segmentsGrob(x1 = unit(0, "npc"), gp=gpar(col=col,lwd=lwd,lty=lty)),
                          simplify=FALSE)
  g <- gtable::gtable_add_grob(g, grobs = separators,
                               t = 2, b = nrow(g), l = seq_len(ncol(g)-2)+2)
  ggplot2::ggplot() + ggplot2::annotation_custom(g) + ggplot2::theme_void()
}

simple example:

table_plot(head(iris))

complicated example :

iris2 <- setNames(head(iris)[1:3],c("alpha*integral(xdx,a,infinity)", "this text\nis high", 'alpha/beta'))
table_plot(iris2,
           base_size=10,
           base_colour="darkred",
           base_family = "serif",
           parse=TRUE,
           theme=ttheme_default,
           lwd=3,
           lty=2,
           col="darkblue")

这篇关于绘制xtable或将其保存为图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆