How to replace an expression in ggplot2?

I would like to get a latex symbol for beta with index 2.

The following code:

idx <- 2; ylab(eval(expression(paste("beta[",idx,"]",sep="")))) 

creates the label "beta [2]" instead of "\ beta_ {2}". What am I doing wrong?

Note: to see what I want, just use ylab(expression(beta[2])) , but I want to infer the value from the idx variable instead of using a hard-coded value.

+4
source share
1 answer

Use bquote. Here is an example code snippet

 library(ggplot2) p1 = qplot(tip, data = tips) idx = 2; my.ylab = bquote(beta[2] == .(idx)) p1 + ylab(my.ylab) 
+5
source

Source: https://habr.com/ru/post/1347325/


All Articles