r - 图表上的多色文本

标签 r

问候,

例如,我需要在我的图表上显示多色文本

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

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

我需要 early、ontime、late in txt 的值,分别为蓝色、绿色和红色。

我在标题中找到了以下关于多色文本的帖子,但是我无法使其适应我的问题 http://blog.revolutionanalytics.com/2009/01/multicolor-text-in-r.html

谢谢你的帮助

最佳答案

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")) 

关于r - 图表上的多色文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3258101/

相关文章:

r - 内存分配 "Error: cannot allocate vector of size 75.1 Mb"

r - 图例不显示颜色

r - R-错误: “duplicate ' row.names' are not allowed”

r - 迭代创建数据框子集的函数

r - 如何定义 `f_n-chi-square and use ` uniroot` 的函数来查找置信区间?

r - 修改色标图例指南以匹配 ggplot2 中的线条大小

javascript - 在 Shiny 的 textOutput() 更改后触发 react

r - 从多个数据框中选择第一行并绑定(bind)

r - 使用 dplyr 按组连接字符串

r - 你能把标签放在单杠之间吗?