Manuel Weinkauf
Manuel Weinkauf

Reputation: 350

R: How to use proper minus symbol for negative axes in plot

I got a comment on a figure I submitted for publication, and by testing just realized something I was not aware of before: When using the bitmap-drivers, R seems to use a hyphen instead of a proper minus symbol for negative values on the axes. Is there any way to correct that?

library(extrafont)
loadfonts(device="win", quiet=TRUE)
png("Test.png", family="Helvetica", width=5, height=5, units="in", res=300)
plot(-3:3, -3:3)
text(-2, 2, labels=("\u2010 Hyphen"), pos=4)
text(-2, 1.5, labels=("\u002d Hyphen-Minus"), pos=4)
text(-2, 1, labels=("\u2212 Minus"), pos=4)
dev.off()

If you look at the image, the negative numbers on the axes are clearly labeled incorrectly with hyphens instead of minus symbols. enter image description here

When using the pdf()-driver, the negative values correctly use minus. I am aware that for the pdf()-driver, this can be changed by using cairo-pdf(). However, using another type (e.g. type="windows") in either the png()- or tiff()-driver does not change the hyphens to minus-signs.

Is there any way to use proper minus symbols for negative axis values using the bitmab-drivers?

Upvotes: 4

Views: 3705

Answers (1)

pascal
pascal

Reputation: 1085

It's rather complicated in base r. The following code prints axes after the plot itself. The hyphen in the axis lables are replaced with minus by find&replace.

plot(-3:3, -3:3, xaxt="n", yaxt="n", xlab = "\u22123:3", ylab = "\u22123:3")

xat <- axTicks(1, usr=par("usr")[1:2])
labs <- gsub("-", "\U2212", print.default(xat))
axis(1, at=xat, labels=labs)
yat <- axTicks(2, usr=par("usr")[1:2])
labs <- gsub("-", "\U2212", print.default(yat))
axis(2, at=xat, labels=labs)

enter image description here

Upvotes: 10

Related Questions