Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add an en dash to a plot in R?

Tags:

r

character

I'm creating a plot in R, and need to add an en dash to some axis labels, as opposed to your everyday hyphen.

axis(1, at=c(0:2), labels=c("0-10","11-30","31-70"))

I'm running R version 2.8.1 on Linux.

like image 455
Banjer Avatar asked Mar 19 '10 16:03

Banjer


1 Answers

Old question but still a problem...

I'm using R vsn 3.3.2 on OSX 10.12.2, plotting with plot() to a pdf file that I import into Affinity Designer vsn 1.5.4. Axis labels of the form "2-0" show up in Affinity Designer with the dash overlapping the "0". I don't know if the problem lies with Affinity Designer or the pdf file or what. It would be nice to be able to try various Unicode dash characters, but R and pdf files both seem to not yet be fully equipped to deal with Unicode using the default fonts.

Solution: the "cairo" package in R:

library("cairo")
d = 0:11
names(d) = paste(0:11, "-", 11:0, sep="")
names(d) = gsub("-", "\U2012", names(d)) # U+2012 is "figure dash"
d
barplot(d)
cairo_pdf(filename="x.pdf", width=11, height=8)
barplot(d)
dev.off()

The dashes show up in the R console, default R plotting device, and the pdf file viewed with both Preview and Affinity Designer.

like image 120
tedtoal Avatar answered Sep 18 '22 23:09

tedtoal