tags:

views:

153

answers:

2

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.

A: 

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.

kwatford
Thanks. I tried copy and pasting it into the labels, but that results in "Error: invalid multibyte character in parser at line x". On another note, print("\55") will give me a hyphen in ASCII, but en dash is extended ASCII, which I'm having trouble with. print("\150") should do the trick (per ASCII tables I've Googled), but no dice.
Banjer
@Banjer - I just tried it in R 2.9.2 in Ubuntu 9.10, and both copying the en dash and typing it in directly worked properly, and it displayed correctly on the axis. Perhaps you should update R?
kwatford
For clarity, it worked when I entered it from the repl. If you're trying to put this in a source file, you'll need to make sure the source file uses an appropriate encoding. You could open it in a text editor like gedit and then in the "Save As" dialog select UTF-8 or something.
kwatford
Yes, I'm running my R programs as batch. I tried saving my program with the various encodings (UTF-8, UTF-16, etc..) but it just crashes harder :). I tried working with the print("\150") from the R interactive CLI, and I notice that R will display ASCII, but it stops "working" once you get to the extended ASCII. See http://www.ascii-code.com/.
Banjer
I just fired up R GUI 2.8.1 on Windows and it shows an en dash with print("\226") just fine! This doesn't work on Linux. Guess I'll have to copy my data files off the network to build this plot with R on Windows for now. All for a stupid en dash! I think you're on the right track kwatford - it's probably an encoding issue, or an X11 or graphics library issue, or something.
Banjer
Hmm, you're using the ASCII character escape... try the unicode escape instead. en dash is unicode 2013, so try print("\u2013")
kwatford
Yeah I tried the unicode escape too. print("\u2013") gives me this little guy: â
Banjer
+1  A: 

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)))
ayman
Thanks ayman, but this doesn't seem to work for me. I get the "invalid multibyte character" error again. I also tried adding quotes in various places.
Banjer