7

textplot()パッケージから定義を記述し、それをを使用gplotsして他のプロットの横に表示していますpar(mfrow=c(3,2))

文字列内の1つの単語を太字に変更したい(通常は定義されている単語)。「」の中にこれを可能にするメタ文字はありますか?または、文字列全体に割り当てずに単語を選択して太字の属性を与えるための別の解決策はありますか?

これはこの質問に似ていますが、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

これがお役に立てば幸いです。この関数は、文字列を分割する一意の文字が1つだけであり、最初の単語を太字にし、残りの文字列を通常の形式にすることを前提としています。

乾杯

編集

申し訳ありませんが、問題があるため、配置にテキストを使用できないとの質問で気づきました。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 に答える