I have the following data frame
df <- structure(list(section_name = c("WWW", "WWW:XXX:YYY",
"WWW:ZZZ", "WWW:ZZZ:YYY", "WWW:YYY",
"XXX", "XXX:ZZZ:YYY", "XXX:YYY",
"ZZZ", "YYY"), member = c("BATF, TEAD1, RUNX2, POL003.1_GC-box, NFIC, EBF, Rfx5, PB0194.1_Zbtb12_2, E2F7",
"Atf1", "PB0182.1_Srf_2, PB0156.1_Plagl1_2", "MF0010.1_Homeobox_class, MEF2A, CRX",
"BORIS, ETS1, CEBPE", "TEAD4, NFATC3, Mef2b, Sp1, PB0099.1_Zfp691_1, NFY, PH0170.1_Tgif2, PB0117.1_Eomes_2, NFY, LEF1, PB0024.1_Gcm1_1",
"RUNX, CTCF", "JunB", "AP-1, TEAD, KLF5, Fli1, Atf2, NFIA, GFY, POL001.1_MTE, PB0135.1_Hoxa3_2, Ahr::Arnt, NFYA, Arnt:Ahr",
"TEAD3, NFIX, KLF3, Rbpj1, SPDEF, Mef2c, Foxf1, PB0203.1_Zfp691_2, SOX9, HOXC13"
)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-10L), .Names = c("section_name", "member"))
df
#> section_name
#> 1 WWW
#> 2 WWW:XXX:YYY
#> 3 WWW:ZZZ
#> 4 WWW:ZZZ:YYY
#> 5 WWW:YYY
#> 6 XXX
#> 7 XXX:ZZZ:YYY
#> 8 XXX:YYY
#> 9 ZZZ
#> 10 YYY
#> member
#> 1 BATF, TEAD1, RUNX2, POL003.1_GC-box, NFIC, EBF, Rfx5, PB0194.1_Zbtb12_2, E2F7
#> 2 Atf1
#> 3 PB0182.1_Srf_2, PB0156.1_Plagl1_2
#> 4 MF0010.1_Homeobox_class, MEF2A, CRX
#> 5 BORIS, ETS1, CEBPE
#> 6 TEAD4, NFATC3, Mef2b, Sp1, PB0099.1_Zfp691_1, NFY, PH0170.1_Tgif2, PB0117.1_Eomes_2, NFY, LEF1, PB0024.1_Gcm1_1
#> 7 RUNX, CTCF
#> 8 JunB
#> 9 AP-1, TEAD, KLF5, Fli1, Atf2, NFIA, GFY, POL001.1_MTE, PB0135.1_Hoxa3_2, Ahr::Arnt, NFYA, Arnt:Ahr
#> 10 TEAD3, NFIX, KLF3, Rbpj1, SPDEF, Mef2c, Foxf1, PB0203.1_Zfp691_2, SOX9, HOXC13
In which I want to create table as an image. I do it with this code:
library(gridExtra)
p <- gridExtra::tableGrob(df)
grid.arrange(p)
Which produces this
My question is how can I wrap every row member
column to a certain column width?
If you add line breaks to the text, the column width will be based on the length of longest unbroken string. One option to automate the setting of the line breaks is to use str_wrap
from the stringr
package. For example:
library(gridExtra)
library(stringr)
df$member = str_wrap(df$member, 40)
p <- gridExtra::tableGrob(df)
grid.arrange(p)
Just for completeness, I should mention that it's possible to directly set the widths of the table columns, but the text won't be automatically wrapped and will therefore be truncated. However, in combination with text wrapping, this can give you additional control over column widths. Here's an example showing the truncation of unwrapped strings:
library(grid)
# Starting with the original (unwrapped) version of df
p <- tableGrob(df, rows=NULL)
p$widths = unit(c(0.2, 0.5), "npc")
grid.arrange(p)
For more on formatting table grobs, see the Vignette .
there's also a little-known feature in tableGrob that lets you overwrite the default function to draw the labels,
library(gridExtra)
text_wrap <- function(label, ...){
labwrap <- stringr::str_wrap(label, 40)
gridExtra:::text_grob(label=labwrap, ...)
}
tt <- ttheme_default(core=list(fg_fun = text_wrap))
grid.table(df, theme=tt)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.