我使用的是textplot()从gplots包写的定义,然后可旁边显示使用其他地块par(mfrow=c(3,2)).
我想将字符串中的单个单词更改为粗体(通常是定义的单词).是否有一个元字符可以让我在""中执行此操作?或者另外一个解决方法是挑选单词并给它们粗体属性而不将其分配给整个字符串?
它类似于这个问题,但我无法在textplot()中使用相同的技术: text()R函数 - 如何更改单个单词的字体?
text(0.5,0.5, expression(paste(bold("bold")," not bold")))
Run Code Online (Sandbox Code Playgroud)
这是我的代码,没有粗体术语.假装"定义"希望是大胆的面孔:
blurb<-strwrap("Definition: This is my text blurb",
width=60)
textplot(blurb, halign="left", valign="top", cex = 1, family="serif")
Run Code Online (Sandbox Code Playgroud)
我一直在玩破坏字符串并搜索一个函数,它将粗体面指定给"定义"部分,font = 2,然后将字符串粘贴在一起,但我很难过.我找不到要使用的功能:
blurb1<-"Definition" ##How to change to bold face??
blurb2<-"This is my text blurb"
blurb<-paste0(blurb1,blurb2)
Run Code Online (Sandbox Code Playgroud)
编辑:使用其他解决方案的主要障碍是,对于我的页面布局,text()并不完全可行.我希望找到一个解决方案,在textplot()内部或以可以传递给textplot()的方式编辑字符串.
我正在创建一些"报告卡",它将绘制用户数据并在图表旁边提供一段解释.不同的值会触发不同的textplot().我喜欢textplot(),因为它很容易用par(mfrow = c(4,2))放置,在不重叠其他绘图的情况下雕刻出一个单独的空间.我似乎无法在没有很多游戏定位的情况下工作text().
你需要使用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
Run Code Online (Sandbox Code Playgroud)
我希望这有帮助。该函数假设只有一个唯一字符可以分割字符串,并且您希望第一个单词以粗体显示,字符串的其余部分以正常格式显示。
干杯
编辑
抱歉,我意识到您在问题中说不能使用文本进行放置,因为它是有问题的。快速检查 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)
}
Run Code Online (Sandbox Code Playgroud)
然后我们可以像这样使用 tplot.cust...
blurb <- "Define: This is my blurb"
tplot.cust(blurb, ":" , halign="left", valign="top", cex = 1, family="serif")
Run Code Online (Sandbox Code Playgroud)
希望这是你想要的?