0

I'm trying to use UTF-8 code to put a symbol in some text in an R figure. If I wanted a black circle, I could use the code intToUtf8(9679). Where can I find a database that lists the values for other symbols? I want to find the code to create a red circle (i.e., pch=16, col="red"), but I can't find a list of what all of the unicode values are for specific symbols.

# example R code
x <- 1:10
y1 <- rnorm(10, x*2, 0.5)
y2 <- rnorm(10, x, 0.5)
plot(x, y1, xlab='x-value', ylab='', pch=16)
points(x, y2, pch=16, col='red')
mtext(paste0('value for y1 (', intToUtf8(9679), ') and y2 (',    intToUtf8(9679), ')'), side=2, line=2)
# except that I want the second black circle in the axis label to be a red circle

Thank you for your help, Mikey

mikey
  • 1,066
  • 9
  • 17
  • Do you want to make a legend? If yes, take a look at the help page: `?legend`. For example, `legend("bottom", legend = c("y1", "y1"), col = c("black", "red"), pch = 16, ncol=2)` isn't the worst. – lmo Nov 15 '17 at 15:26
  • Thank you, but I'd rather only use a legend as a last option. I'd prefer to have the information in the axis label as my figure with real data does not have room for a legend. – mikey Nov 15 '17 at 15:44
  • Check out `?points()` for a list a point characters (`pch`) and [how to do multi-colored labels](http://blog.revolutionanalytics.com/2009/01/multicolor-text-in-r.html). Side note: You could do `\u25CF` instead of `', intToUtf8(9679), '`. There is also a unicode for a [large red circle](https://www.google.com/search?q=unicode+large+red+circle), however I doubt that you get it to work in a plot. – lukeA Nov 15 '17 at 15:48
  • Thank you @lukeA. I tried these ideas, I tried the ideas in the link, but I run into a problem when trying to insert the symbol into the expression command: `mtext(expression('value for y1 (\u25CF) and y2 ('* phantom('\u25CF') * ')'), side=2, line=2)`. But I'm wondering how you know that a black point is `\u25CF`? Is there a table somewhere that shows what these values are? There is not that much information under `?points` – mikey Nov 15 '17 at 17:05
  • Use your favorite search engine and look for unicode list? – lukeA Nov 15 '17 at 17:41
  • 1
    Thank you. I guess I wasn't searching properly. I still couldn't get it to work with the unicode for a red dot. I ended up following the advice in the link provided [multi-colored labels](http://blog.revolutionanalytics.com/2009/01/multicolor-text-in-r.html) but since you can't use the `phantom` command and unicode at the same time (because unicode is misinterpreted within `expression`), I just put a bunch of spaces in the text until the point fell where I wanted it. – mikey Nov 15 '17 at 18:58

1 Answers1

0

Here is the solution I ended up using. It's not the most efficient, but it works:

x <- 1:10
y1 <- rnorm(10, x*2, 0.5)
y2 <- rnorm(10, x, 0.5)
plot(x, y1, xlab='x-value', ylab='', pch=16)
points(x, y2, pch=16, col='red')
mtext('value for y1 (\u25CF) and y2 (  )', side=2, line=2)
mtext('                                   \u25CF', side=2, line=2, col='red')

# alternatively, the following would also work in place of line 6
mtext(paste0('value for y1 (', intToUtf8(9679),' and y2 (  )'), side=2, line=2)

If you want to find more unicode character information for your specific symbol, you can look here.

Thank you lukeA for your help with this.

mikey
  • 1,066
  • 9
  • 17