本文介绍了减去作为plotmath的指数(在ggplot2图例中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在包含减号作为指数(指数中没有其他字符)的 ggplot2 图中绘制图例。但是,我找不到 plotmath 语法。



看起来像下面这样工作: p>

  expr1<  -  expression(paste(text,main [sub] ^ { - }))

ggplot(mpg,aes(x = cty,y = hwy,color = drv))+ geom_point()+
scale_colour_discrete(labels = c(expr1,b,c))

(如果我们说 expr1< - expression(paste(text ,main [sub] ^ {super}))。在 plotmath ?中有减号的转义字符吗?

解决方案

几乎肯定会需要在减号附近加引号,因为否则它可能会是中缀运算符,因此如果没有解决问题,请添加一个小的测试用例。

转义在plotmath中不起作用,特别是不能使用\ n作为行结束/换行标记(正如h中所记录的那样) elp(plotmath)页面。



这也成功了:

  expr1<  -  expression (text,main [sub] ^ {phantom() -  phantom()}))

我从来没有尝试过在中缀操作符之前使用 phantom ,但似乎可以接受解释器。 Plotmath表达式得到解析并需要符合R解析规则。请参阅?语法。如注释中所述,使用 - 作为单个 phantom()的前缀运算符也会成功,因为减号sign可以用作一元减法或二元减法:

  expr1<  -  expression(paste(text ,main [sub] ^ { -  phantom()}))

我们也可以使用空字符值作为前缀减号后的项目:{ - }


I'm trying to make a legend in a ggplot2 plot that contains a minus sign as an exponent (with no other characters in the exponent). However, I can't figure out the plotmath syntax.

It seems like the following would work:

expr1 <- expression(paste("text", main[sub]^{-}))

ggplot(mpg, aes(x=cty, y=hwy, colour=drv)) + geom_point() +
  scale_colour_discrete(labels=c(expr1, "b", "c"))

(And it does work if we say expr1 <- expression(paste("text", main[sub]^{super})). Is there an escape character or something for minus signs in plotmath?

解决方案

You are almost certainly going to need to put quotes around that minus sign, because it would otherwise be expected to be an infix operator and as such require arguments before and after it. Add a small test case if that does not solve the problem.

Escaping does not work in plotmath. In particular you cannot use "\n" as an end-of-line/newline marker (as is documented in the help(plotmath) page.

This also succeeds:

expr1 <- expression(paste("text", main[sub]^{phantom()-phantom()}))

I had never before tried using phantom preceding and succeeding an infix operator, but it seems acceptable to interpreter. Plotmath expressions do get parsed and need to conform to R-parsing rules. See ?Syntax. As noted in comments, using "-" as a prefix operator to a single phantom() also succeeds because the minus sign can be used as either a unary-minus or a binary-minus:

expr1 <- expression(paste("text", main[sub]^{-phantom()}))

We could also have used an empty character value as the item after the prefix minus: {-""}

这篇关于减去作为plotmath的指数(在ggplot2图例中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 09:01