Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding the degree symbol at the end of each vector element in R

Tags:

r

I have a vector of numerical values, say:

angles <- c(10.2, 20.3, 14.3, 18.4)

And I want to append the degree symbol at the end of each element, in order to obtain:

labels <- c("10.2°", "20.3°", "14.3°", "18.4°")

I tried with bquote() function without success:

labels <- bquote(paste(.(angles) * degree))
like image 949
Ben Avatar asked Dec 14 '22 07:12

Ben


2 Answers

If your keyboard does not provide the degree symbol ° the character can be obtained with its UTF8 code, which in this case is 176:

paste0(angles,intToUtf8(176))
#[1] "10.2°" "20.3°" "14.3°" "18.4°"

By using the UTF8 code any character can be pasted like this. Hope this helps.

like image 68
RHertel Avatar answered Dec 16 '22 19:12

RHertel


If you are wanting to use the labels on a plot, code like this would work

angles <- c(10.2, 20.3, 14.3, 18.4)
labels <-sapply(angles, function(a)bquote(.(a) * degree))

plot(1:4, 1:4)
mapply(text, labels = labels, x = 1:4, y = 1:4, pos = 4:1)
like image 37
Richard Telford Avatar answered Dec 16 '22 19:12

Richard Telford