2010-07-15 160 views
2

問候,彩色文本

我需要我的圖表上顯示彩色文本例如

early <- 30 
ontime <- 70 
late <- 25 

txt <- paste(early, ontime, late, sep='/') 
plot(1:2, type='n') 
text(1.5, 1.5, txt) 

我需要值早,準時,尾盤TXT,是藍色,綠色和紅色分別。

我發現在標題多色文字下面的帖子,但我無法使它適應我的問題 http://blog.revolutionanalytics.com/2009/01/multicolor-text-in-r.html

謝謝您的幫助

回答

5

這個怎麼樣代碼由Jim Lemon寫的?

concat.text<-function(x,y,txt,col) { 
    thisx<-x 
    for(txtstr in 1:length(txt)) { 
     text(thisx,y,txt[txtstr],col=col[txtstr],adj=0) 
     thisx<-thisx+strwidth(txt[txtstr]) 
    } 
} 
plot(0,xlim=c(0,1),ylim=c(0,1),type="n") 
ctext<-c("Roses are ","red, ","violets are ","purple") 
concat.text(0,0.5,ctext,col=c("black","red","black","purple")) 
+0

此功能的偉大工程! – ilya 2010-07-15 20:15:17

1

下面你提到你會看到這樣的exampe:

early <- 30 
ontime <- 70 
late <- 25 

txt <- paste(early, ontime, late, sep='/') 
plot(1:2, type='n') 
vars <- list(early=early,ontime=ontime,late=late) 
cols <- c('red', 'green', 'blue') 
for (i in 1:3) { 
    tmpvars <- vars 
    tmpvars[-i] <- paste("phantom(",tmpvars[-i],")",sep="") 
    expr <- paste(tmpvars, collapse="*") 
    text(1.5, 1.5, 
     parse(text=expr), 
     col=cols[i]) 
} 
+0

謝謝你的回覆。 – ilya 2010-07-15 20:16:06