Using Expression(Paste( to Insert Math Notation into a Legend

Using expression(paste( to insert math notation into a legend

Here is a simple example of how to use values and labels to get what I think you want.

Note that values map the values in the data to the colours you wish to use, while labels are the labels you want displayed (so this is where you would put the expression).

ggplot(data = data.frame(x= c(0,5)),aes(x=x)) +
stat_function(fun=dnorm,aes(colour = 'red')) +
stat_function(fun = dexp, aes(colour = 'blue')) +
scale_colour_manual(values = c('red' = 'red','blue' = 'blue'),name = '',
labels = expression(P[M1](tilde(z)>0),P[M0](tilde(z)>0)))

Sample Image

ggplot2: how to assign math symbols to legend labels for two different aesthetics?

I had overlooked that a similar question had already been asked and the answers give the right hint: assign the labels via the scales, as shown below. Note that the both scales must be defined and that both must have the same name and labels, in order to avoid producing two legends.

a1 = exp(seq(1, 3))
a2 = exp(seq(1, 3) + 0.2)
a3 = exp(seq(1, 3) + 0.4)

df = data.frame(coefficients = c(a1, a2, a3), order = rep(1:3, 3),
norm = factor(rep(1:3, each = 3), labels = c("L1", "L2", "Linf")))

ggplot(df, aes(x = order, y = coefficients, colour = norm, shape = norm)) +
geom_line() + geom_point() +
scale_colour_manual(name = "norm", values = c("blue", "red", "green"),
labels = expression(L[1], L[2], L[infinity])) +
scale_shape_discrete(name = "norm",
labels = expression(L[1], L[2], L[infinity]))

Sample Image

How to add expression to plot legend label with %in% and %~%

Try a combination of as.expression() and bquote() instead:

require(ggplot2)

df <- data.frame(x=rnorm(30),y=rnorm(30))

ggplot(df) +
geom_point(aes(x=x,y=y,col='blue')) +
scale_color_manual("Legend",
values='blue',
labels=as.expression(bquote('Prior'~beta[3]%in%'(-0.1 0.1)')))

Output:

Sample Image

Putting mathematical symbols and subscripts mixed with regular letters

Something like this :

g <- ggplot(data=data.frame(x=0,y=0))+geom_point(aes(x=x,y=y))
g+ xlab( expression(paste("Value is ", sigma,",", R^{2},'=0.6')))

EDIT

Another option is to use annotate with parse=T:

g+ annotate('text', x = 0, y = 0, 
label = "Value~is~sigma~R^{2}==0.6 ",parse = TRUE,size=20)

Sample Image

EDIT

The paste solution may be useful if the constant 0.6 is computed during plotting.

r2.value <- 0.90
g+ xlab( expression(paste("Value is ", sigma,",", R^{2},'=',r2.value)))

Inserting math symbols in legends of R plots

To place a unicode character in a string you can use the unicode number for the symbol you want escaped with \u. That is using \u2265 for ≥ and \u2264 for ≤.

ggsurvplot(
fit = survfit(Surv(d$y, event = d$failed) ~ d$Xfactor, data = d),
xlab = "Years",
ylab = "Overall survival probability",
main = "Overall survival",
legend.labs = c("\u2264 4 Units","> 4 Units"))

Sample Image

How to write an equation with a variable in legend?

For this task I think it is best to do parse(text=sprintf(...)). You can code the R language syntax into the string literal to be parsed into an R expression using parse(), and use sprintf() format specifications to embed any numeric or string values that are stored in variables into the expression.

set.seed(100L);
x <- rnorm(100L);
y <- 1:100;
fit <- lm(y~x);
R_squared <- format(summary(fit)$r.squared,digits=3L);
plot(x,y,type='l');
legend('topleft',legend=parse(text=sprintf('paste(R^2,\' = %s\')',R_squared)),bty='n');

An alternative syntax that leverages the fact that == is plotted as a single equal sign:

legend('topleft',legend=parse(text=sprintf('R^2 == %s',R_squared)),bty='n');

See the plotmath documentation.

plot



Related Topics



Leave a reply



Submit