Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
258 views
Welcome To Ask or Share your Answers For Others

1 Answer

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
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")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...