在 gridExtra 中使用 tableGrob 在 table 中包装文本
Wrapping text in a table using tableGrob in gridExtra
我有一系列调查问题,正在报告学校、学校家庭和整个董事会的答复。有些问题很长——我想做的是让文本在专栏结束时自动换行。现在我必须手动添加换行符。这可能吗?我使用的是 R 版本 3.2.1 和 gridExtra_2.0.0。
一些代码:
library(gridExtra)
library(grid)
library(ggplot2)
key <- c("I feel close to other parents with children the same age", "I am comfortable asking for advice about parenting", "I take time out to take care of my own health and well−being")
SCH <- c(20,30,40)
FOS <- c(25,35,56)
BOARD <- c(32,44,58)
d <- data.frame(key, SCH, FOS, BOARD)
row.names(d) <- d[,1]
d[,1] <- NULL
g <- tableGrob(d)
g$widths <- unit (c(0.4, 0.1,0.1,0.1),"npc")
grid.newpage()
grid.draw(g)
您可以通过 strwrap
实现
key <- c("I feel close to other parents with children the same age", "I am comfortable asking for advice about parenting", "I take time out to take care of my own health and well−being")
key_wraped <- strwrap(key, width = 30, simplify = FALSE) # modify 30 to your needs
key_new <- sapply(key_wraped, paste, collapse = "\n")
d <- data.frame(key_new, SCH, FOS, BOARD)
对我来说,这导致:
另一种选择是 Paul Murrell 的 "splitString" 函数:
splitString <- function(text) {
strings <- strsplit(text, " ")[[1]]
newstring <- strings[1]
linewidth <- stringWidth(newstring)
gapwidth <- stringWidth(" ")
availwidth <-
convertWidth(unit(1, "npc"),
"inches", valueOnly=TRUE)
for (i in 2:length(strings)) {
width <- stringWidth(strings[i])
if (convertWidth(linewidth + gapwidth + width,
"inches", valueOnly=TRUE) <
availwidth) {
sep <- " "
linewidth <- linewidth + gapwidth + width
} else {
sep <- "\n"
linewidth <- width
}
newstring <- paste(newstring, strings[i], sep=sep)
}
newstring
}
library(grid)
pushViewport(viewport())
grid.text(splitString("There was once a very long string that had to take up the entire viewport and then some but luckily there was also a function called 'splitString'"))
我有一系列调查问题,正在报告学校、学校家庭和整个董事会的答复。有些问题很长——我想做的是让文本在专栏结束时自动换行。现在我必须手动添加换行符。这可能吗?我使用的是 R 版本 3.2.1 和 gridExtra_2.0.0。
一些代码:
library(gridExtra)
library(grid)
library(ggplot2)
key <- c("I feel close to other parents with children the same age", "I am comfortable asking for advice about parenting", "I take time out to take care of my own health and well−being")
SCH <- c(20,30,40)
FOS <- c(25,35,56)
BOARD <- c(32,44,58)
d <- data.frame(key, SCH, FOS, BOARD)
row.names(d) <- d[,1]
d[,1] <- NULL
g <- tableGrob(d)
g$widths <- unit (c(0.4, 0.1,0.1,0.1),"npc")
grid.newpage()
grid.draw(g)
您可以通过 strwrap
key <- c("I feel close to other parents with children the same age", "I am comfortable asking for advice about parenting", "I take time out to take care of my own health and well−being")
key_wraped <- strwrap(key, width = 30, simplify = FALSE) # modify 30 to your needs
key_new <- sapply(key_wraped, paste, collapse = "\n")
d <- data.frame(key_new, SCH, FOS, BOARD)
对我来说,这导致:
另一种选择是 Paul Murrell 的 "splitString" 函数:
splitString <- function(text) {
strings <- strsplit(text, " ")[[1]]
newstring <- strings[1]
linewidth <- stringWidth(newstring)
gapwidth <- stringWidth(" ")
availwidth <-
convertWidth(unit(1, "npc"),
"inches", valueOnly=TRUE)
for (i in 2:length(strings)) {
width <- stringWidth(strings[i])
if (convertWidth(linewidth + gapwidth + width,
"inches", valueOnly=TRUE) <
availwidth) {
sep <- " "
linewidth <- linewidth + gapwidth + width
} else {
sep <- "\n"
linewidth <- width
}
newstring <- paste(newstring, strings[i], sep=sep)
}
newstring
}
library(grid)
pushViewport(viewport())
grid.text(splitString("There was once a very long string that had to take up the entire viewport and then some but luckily there was also a function called 'splitString'"))