7

我使用textplot()包中的gplots来编写定义,然后使用par(mfrow=c(3,2)).

我想将字符串中的单个单词更改为粗体(通常是正在定义的单词)。是否有一个元字符可以让我在“”中执行此操作?或者另一种选择单词并赋予它们粗体属性而不将其分配给整个字符串的解决方案?

它类似于这个问题,但我无法在 textplot() 中使用相同的技术: text() R-function - 如何更改单个单词的字体?

text(0.5,0.5, expression(paste(bold("bold")," not bold")))

这是我的代码,没有粗体字。假装“定义”是粗体:

blurb<-strwrap("Definition: This is my text blurb",
                width=60)
textplot(blurb, halign="left", valign="top", cex = 1,  family="serif")

我一直在尝试将字符串分开并搜索将粗体分配给“定义”部分的函数,font=2,然后将字符串重新粘贴在一起,但我很难过。我找不到要使用的功能:

blurb1<-"Definition"             ##How to change to bold face?? 
blurb2<-"This is my text blurb"

blurb<-paste0(blurb1,blurb2)

编辑:使用其他解决方案的主要障碍是对于我的页面布局, text() 并不完全可行。我希望找到在 textplot() 内部或以可以传递给 textplot() 的方式编辑字符串的解决方案。

我正在创建一些“报告卡”,它将绘制用户数据并在情节旁边提供一段解释。不同的值会触发不同的 textplot()。我喜欢 textplot() 因为它很容易与 par(mfrow=c(4,2)) 一起放置,在不重叠其他图的情况下划出一个单独的空间。如果没有大量的定位,我似乎无法在 text() 中工作。

4

1 回答 1

3

你需要使用bquote(). 这是一个简单的函数,它接受一个文本字符串并将其拆分并返回适当的表达式以满足您的粗体绘图需求。我相信您可以根据需要进行调整。

# Pass the function a string and a character to split on
# The splitting is greedy (i.e. it will split on all matches so make sure you are splitting on a unqiue character such as ":" in your example)
tsplit <- function( string , split ){
    require( stringr )
    blurb <- paste( string )
    blurbs <- strsplit( blurb , paste(split) )
    annot <- bquote( paste( bold( .( blurbs[[1]][1] ) ) , .(split) , .(blurbs[[1]][2]) , sep = "" ) )
    return( annot )
}



#And the function in action...
j <- tsplit( "Define: This is my blurb" , ":" )
textplot( paste( " " ) ) #Get new plot
text(0.5 , 0.5 , j ) #paste the text

我希望这有帮助。该函数假定只有一个唯一字符可以拆分字符串,并且您希望第一个单词以粗体显示,而字符串的其余部分以正常格式显示。

干杯

编辑

抱歉,我在您所说的问题中意识到您不能使用文本进行放置,因为它有问题。快速检查 textplot ( showMethods(textplot)) 的可用方法和用于绘制字符 ( getAnywhere(textplot.character)) 的适当方法的来源表明 textplot 实际上使用调用来text使用文本对象注释绘图。大部分代码都涉及在您想要文本的位置进行繁重的工作。您可以进行一些简单的调整来textplot.character()创建自定义函数来执行您想要的操作。您可以将其复制并粘贴到 R 中,它应该按照底部的示例工作。

tplot.cust <-   function ( object , split , halign = c("center", "left", "right"), valign = c("center", 
"top", "bottom"), cex, fixed.width = TRUE, cspace = 1, lspace = 1, 
mar = c(0, 0, 3, 0) + 0.1, tab.width = 8, ...) 
{
# extra code to split text according to 'split' argument and make text before the split bold.
require(stringr)
blurb <- paste( object )
blurbs <- strsplit( blurb , paste(split) )
annot <- bquote( paste( bold( .( blurbs[[1]][1] ) ) , .(split) , .(blurbs[[1]][2]) , sep = "" ) )


object <- paste(object, collapse = "\n", sep = "")
object <- gplots:::replaceTabs(object, width = tab.width) #you need to add gplots::: to this line because replaceTabs is a function that is not exported from the gplots namespace
halign = match.arg(halign)
valign = match.arg(valign)
plot.new()
opar <- par()[c("mar", "xpd", "cex", "family")]
on.exit(par(opar))
par(mar = mar, xpd = FALSE)
if (fixed.width) 
    par(family = "mono")
plot.window(xlim = c(0, 1), ylim = c(0, 1), log = "", asp = NA)
slist <- unlist(lapply(object, function(x) strsplit(x, "\n")))
slist <- lapply(slist, function(x) unlist(strsplit(x, "")))
slen <- sapply(slist, length)
slines <- length(slist)
if (missing(cex)) {
    lastloop <- FALSE
    cex <- 1
}
else lastloop <- TRUE
for (i in 1:20) {
    oldcex <- cex
    cwidth <- max(sapply(unlist(slist), strwidth, cex = cex)) * 
        cspace
    cheight <- max(sapply(unlist(slist), strheight, cex = cex)) * 
        (lspace + 0.5)
    width <- strwidth(object, cex = cex)
    height <- strheight(object, cex = cex)
    if (lastloop) 
        break
    cex <- cex/max(width, height)
    if (abs(oldcex - cex) < 0.001) {
        lastloop <- TRUE
    }
}
if (halign == "left") 
    xpos <- 0
else if (halign == "center") 
    xpos <- 0 + (1 - width)/2
else xpos <- 0 + (1 - width)
if (valign == "top") 
    ypos <- 1
else if (valign == "center") 
    ypos <- 1 - (1 - height)/2
else ypos <- 1 - (1 - height)
text(x = xpos, y = ypos, labels = annot , adj = c(0, 1), 
    cex = cex, ...) #add the newly created annot expression here
par(opar)
invisible(cex)
}

然后我们可以像这样使用 tplot.cust ......

blurb <- "Define: This is my blurb"
tplot.cust(blurb, ":" , halign="left", valign="top", cex = 1,  family="serif")

希望这是你想要的??

于 2013-02-19T12:29:15.207 回答