Combining vector variables in R expression for plot text

孤人 提交于 2019-12-12 18:11:04

问题


I am trying to create a text layer in an R plot containing normal and superscript text that derives from variables.

So far I have this:

first = c("one", "two", "three")
second = c(1, 2, 3)
plot(1:3, 3:1)
text(1:3, 3:1, labels=first)

The way it works now, it shows one, two, etc on the plot. I want it to show one1, two2, etc.

I guess it should be some combination of expression, paste, bquote and maybe another function. I just can't get it to get it to read the data as vectors and make the proper superscript.

I've seen some questions on this site, for example:

  • Combining paste() and expression() functions in plot labels
  • Use expression with a variable r
  • Including variables in expression call in R

None of them fully answer my question.


回答1:


May be you can try

 plot(1:3, 3:1)
 text(1:3, (3:1)-0.03, labels= mapply(function(x,y)
      as.expression(bquote(.(x)^.(y))), first, second))



来源:https://stackoverflow.com/questions/27275798/combining-vector-variables-in-r-expression-for-plot-text

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!