问候,
我需要在图表上显示多色文字,例如
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
感谢您的帮助
答案 0 :(得分: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"))
答案 1 :(得分:1)
按照你提到的例子给出类似的东西:
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])
}