How to get an expression(cos (alpha)) into a labels?
Asked Answered
B

3

6

I'm trying to get an Label on an arrow that says:

||x|| cos (alpha)

But I can't seem to make it work.

I can write the ||x|| without problems, and the cos (alpha) without problems, but I don't know how to get them into one statement.

Any ideas?

Here is my code:

library(plotrix)
library(shape)

xlim <- c(-2, 6)
ylim <- c(-2, 6)
plot(0, type = "n", xlim = xlim, ylim = ylim,asp=1)


Arrows(1,1,5,1)
boxed.labels(3,1,labels="||x|| cos (a)",border=NA,xpad=1,ypad=1)


Arrows(1,2,5,2)
boxed.labels(3,2,labels=expression(cos (alpha)),border=NA,xpad=1,ypad=1)
Brindle answered 4/11, 2014 at 9:29 Comment(0)
N
9

Study help("plotmath") and the demo.

plot(0, type = "n", xlim = xlim, ylim = ylim,asp=1)
text(2,1,labels=expression(group("||", x, "||") %.% cos(alpha)),adj=c(1.2,-1.5))
text(2,3,labels=expression(group("||", x, "||") ~ cos(alpha)),adj=c(1.2,-1.5))

resulting plot

Norford answered 4/11, 2014 at 10:0 Comment(0)
B
5

You can also use bquote:

plot(1, type = "n")
text(1, 1, bquote("||x||" ~ cos(alpha)))

enter image description here

Bozo answered 4/11, 2014 at 10:20 Comment(0)
F
2

Passing pasted elements to expression works for this. For example:

plot.new()
plot.window(xlim=c(0, 1), ylim=c(0, 1))
text(0.5, 0.5, expression(paste("||x|| cos(", alpha, ")")))

enter image description here

Fairminded answered 4/11, 2014 at 9:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.