tableGrob 格式化

tableGrob formatting

我在 R 中有以下 table(下面是 csv 格式)。由于其中有很多,我需要对它们进行格式化,我尝试使用 tableGrob 来节省大量时间,但我需要非常特定格式的输出,包括 Calibri 字体(也在下面)。

"2015\nabril","2016\nabril","2017\nmarzo","2017\nabril"

"Pedidos",-23.3,-13.8,-26,-39

"Existencias",7.6,1.2,5.3,10.5

"Expectativas",30.7,32.7,28.7,24

"ICI",-0.1,5.9,-0.9,-8.5

"ICI Desest*",0.4,5.7,-3.2,-6.9

但是,每次我尝试编辑主题并打印 table 时,我都会收到此错误

library(gridExtra)
t1 <- ttheme_default(core=list(
       fg_params=list(fontface=c(rep("plain", 4), "bold.italic")),
       bg_params = list(fill=c(rep(c("grey95", "grey90"),
                               length.out=4), "#6BAED6"),
                    alpha = rep(c(1,0.5), each=5))
))

t  <- tableGrob(tabla_fin)
grid.table(t, theme = t1)

Error in array(x, c(length(x), 1L), if (!is.null(names(x))) list(names(x), : dims [product 60] do not match the length of object [12]

如果有任何帮助,我将不胜感激。谢谢!

以下是一些可能对解决您的问题有用的想法。

library(grid)
library(gridExtra)
library(gtable)

# Build table
tabla_fin <- rbind(
c("","2015\nabril","2016\nabril","2017\nmarzo","2017\nabril"),
c("Pedidos",-23.3,-13.8,-26,-39),
c("Existencias",7.6,1.2,5.3,10.5),
c("Expectativas",30.7,32.7,28.7,24),
c("ICI",-0.1,5.9,-0.9,-8.5),
c("ICI Desest*",0.4,5.7,-3.2,-6.9))

colnames(tabla_fin) <- tabla_fin[1,]
tabla_fin <- tabla_fin[-1,]

rownames(tabla_fin) <- tabla_fin[,1]
tabla_fin <- tabla_fin[,-1]

print(tabla_fin)

#              2015\nabril 2016\nabril 2017\nmarzo 2017\nabril
# Pedidos      "-23.3"     "-13.8"     "-26"       "-39"      
# Existencias  "7.6"       "1.2"       "5.3"       "10.5"     
# Expectativas "30.7"      "32.7"      "28.7"      "24"       
# ICI          "-0.1"      "5.9"       "-0.9"      "-8.5"     
# ICI Desest*  "0.4"       "5.7"       "-3.2"      "-6.9"


# Install Calibri font using the extrafont package
# read the help of font_install()
library(extrafont)
loadfonts(device="win")
# font_install()

# Theme for text tables
t1 <- ttheme_default(
        core=list(
         fg_params=list(fontface=rep("plain", 6), fontfamily="Calibri",
                      x=1, hjust=1),
         bg_params = list(
                     fill=c(rep("white",3),"grey80","grey90"),
                     col=c("white","white","white","grey80","grey90"),
                     alpha = rep(1,5))
        ),
        rowhead=list(
         fg_params=list(x=0, hjust=0, fontface="plain", fontfamily="Calibri"),
         bg_params = list(
                     fill=c("white",rep("white",3),"grey80","grey90"),
                     col=c("white","white","white","white","grey80","grey90"),
                     alpha = rep(1,5))
        ),
        colhead=list(
         fg_params=list(fontface="plain",fontfamily="Calibri"),
         bg_params = list(
                     fill="grey70",
                     col="grey70",
                     alpha = rep(1,5))
        )
)

# Create gtable containing text grobs
t  <- tableGrob(tabla_fin, theme=t1)

# Add horizontal and vertical lines
t <- gtable_add_grob(t,
        grobs = segmentsGrob( # line across the bottom
            x0 = unit(0,"npc"),
            y0 = unit(0,"npc"),
            x1 = unit(1,"npc"),
            y1 = unit(0,"npc"),
            gp = gpar(lwd = 4.0)),
        t = 1, b = 1, l = 1, r = ncol(t))
t <- gtable_add_grob(t,
        grobs = segmentsGrob( # line across the bottom
            x0 = unit(0,"npc"),
            y0 = unit(0,"npc"),
            x1 = unit(1,"npc"),
            y1 = unit(0,"npc"),
            gp = gpar(lwd = 4.0)),
        t = 4, b = 4, l = 1, r = ncol(t))
t <- gtable_add_grob(t,
        grobs = segmentsGrob( # line across the bottom
            x0 = unit(0,"npc"),
            y0 = unit(0,"npc"),
            x1 = unit(0,"npc"),
            y1 = unit(1,"npc"),
            gp = gpar(lwd = 4.0)),
        t = 1, b = nrow(t), l = 4, r =4)

# Draw table
grid.newpage()
grid.draw(t)

这里是 table: