[英]How to add name of column in column of rownames in tableGrob in R?
我正在嘗試從 R 中的矩陣輸入創建一個圖表。我為此使用了 tableGrob。 我的矩陣的行名是 YEAR,即 2000, 2001, .... 這是我現在正在使用的代碼。
tt1 <- ttheme_minimal(
core=list(bg_params = list(fill = "#ffffff", alpha = 1),
fg_params=list(fontface="bold", hjust=0, x=0.05, fontsize=9, just='centre',
col=ifelse(startsWith(matrix_input, "-"), "#FF0000", "#228B22")),
padding=unit.c(unit(10, "mm"), unit(2.5, "mm"))
),
colhead=list(fg_params=list(fontface='bold', col='#3c3c3c', hjust=0, x=0.05, fontsize=9)),
rowhead = list(fg_params=list(fontface='bold', col='#3c3c3c', hjust=0, x=0.05, fontsize=9),
padding=unit.c(unit(10, "mm"), unit(2.5, "mm")))
)
tGrob <- tableGrob(as.matrix(matrix_input), rows = rownames(matrix_input), theme = tt1)
tGrob <- add_border_at_bottom_of_row(1)
grid.arrange(tGrob)
在輸出中,rownames (2004, 2005....) 的列名稱為空。 我需要將其命名為“年份”。 有人可以幫助並指導我如何做嗎?
您可以將行名添加為數據中的新列,
library(gridExtra)
grid.table(tibble::rownames_to_column(iris[1:4,1:2], 'title'), rows=NULL)
或將它們保留為行名並在 gtable 單元格中手動添加標題。 如果從現有表中復制 textGrob 並僅更改文本,則樣式設置可能會更容易,
library(gridExtra)
library(grid)
tg <- tableGrob(iris[1:4,1:2])
tg <- gtable::gtable_add_grob(tg, textGrob('title', hjust=1,x=1), t=1,l=1, b=1,r=1,z=0)
tg$layout$clip <- 'off'
grid.newpage()
grid.draw(tg)
tg2 <- tableGrob(iris[1:4,1:2])
tg2 <- gtable::gtable_add_grob(tg2, editGrob(tg$grobs[[1]], label='new'), t=1,l=1, b=1,r=1,z=0)
tg2$layout$clip <- 'off'
grid.newpage()
grid.draw(tg2)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.