Include text control characters in plotmath expressions

倖福魔咒の 提交于 2019-11-26 21:36:50

问题


Is there any way to get control characters for text strings, e.g. "\n" for newline evaluated inside a plotmath expression, or vice versa. In the following example, I would like to combine:

  • some character text
  • text control character (newline)
  • substitute a variable name
  • include a plotmath expression

After reading this question I can get most of the way there with substitute, but the newline character is not evaluated. I am now going round in circles and confusing myself with plotmath, parse, bquote and substitute. In the help page for plotmath it says

Control characters (e.g. \n) are not interpreted in character strings in plotmath, unlike normal plotting.

Does this mean it really is impossible?

lab = "some data"
form = "Exponential"
x = 1:10
y = x^2


plot( x , y , type = "b" )
title( main = substitute( paste( "Plot of " , phi , " of: "  , lab , "\nFunctional form: " , form ) , list(lab = lab , form = form ) ) , adj = 0 )


回答1:


As you have figured plotmath does not support newlines within, but you can use mtext with bquote, to write each line. For example I create a list of lines :

Lines <- list(bquote(paste( "Plot of " , phi , " of: "  , .(lab))),
              bquote(paste("Functional form: " , .(form)))

mtext(do.call(expression, Lines),side=3,line=1:0)




回答2:


if you use grid graphics, then the following grob can be useful to space the lines according to their height,

library(devtools)
source_gist(2732693)
grid.expr(as.expression(Lines))

(using agstudy's Lines)




回答3:


For the sake of completeness, here's another solution using unicode and no expressions (adapted from here and here):

plot(x, y, type="b")
title(main=paste("Plot of \u03A6 of:", lab, "\nFunctional form:", form), adj=0)



来源:https://stackoverflow.com/questions/15297814/include-text-control-characters-in-plotmath-expressions

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