2010-07-15 123 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