开发者

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

开发者 https://www.devze.com 2022-12-24 01:24 出处:网络
I\'m creating a plot in R, a开发者_Go百科nd need to add an en dash to some axis labels, as opposed to your everyday hyphen.

I'm creating a plot in R, a开发者_Go百科nd 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.


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.


In this example, you can use the expression() function to get en dashes rendered properly:

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


You're using Linux, so depending on how well R understands unicode, you could map one of your spare keyboard keys to the Compose Key and then just type it out. To get a —, press Compose and then the normal - key two or three times (depending on your system's mappings). Note that when using the Compose key, you don't hold it down - just press the keys in sequence.

Exactly how you'd enable that varies, but in Ubuntu, System->Preferences->Keyboard, Layout tab, Layout Options button, and select something appropriate for the "Compose key position" item. I usually use the Menu key.

Edit: My mistake, you wanted an en-dash, not an em-dash. Then en-dash (–) is Compose dash dash period, rather than Compose dash dash dash.


a MDPI journal has requested to change from hyphen to en dash in the axis labels. Using the base system for graph, I solved the problem by simply changing the "-" with "\u2013" without spaces. The example code for axis in a complete form is

axis(1,1:2,c("20\u201329","40\u201349")

In my case the two labels expressed two age groups. I used it in R 4.1.3 and windows 10.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号