1

I'm trying to display chess symbols in a R Plot. I have searched a lot on the Internet, but I couldn't find the answer.

symbols <- data.frame(c(1,2,3,4,5,6,7,8),c(2,2,2,2,2,2,2,2),rep("\U2654", times=8))
symbols_w <- data.frame(c(1,2,3,4,5,6,7,8),c(7,7,7,7,7,7,7,7),rep("\U25a0", times=8))
colnames(symbols) <-c("xPos", "yPos", "unicode")
colnames(symbols_w) <-c("xPos", "yPos", "unicode")
symbols$unicode <- as.character(symbols$unicode)
symbols_w$unicode <- as.character(symbols_w$unicode)
chess_field + 
geom_text(data = symbols, aes(x=xPos, y=yPos, label=unicode), size = 11, color = "gray20", alpha = 0.7) + 
geom_text(data = symbols_w, aes(x=xPos, y=yPos, label=unicode), size = 11, color = "white", alpha = 0.7)

I take the Unicode for the chess figures from here: https://en.wikipedia.org/wiki/Chess_symbols_in_Unicode

I got these pictures as result: enter image description here

It*s not getting displayed right, maybe you can help me?

EDIT

unicode <- rchess:::.chesspiecedata() %>% select(unicode)
uni <- as.character(unicode[1,])
symbols <- data.frame(c(1,2,3,4,5,6,7,8),c(2,2,2,2,2,2,2,2),rep(uni, times=8))

enter image description here

EDIT 2

dfboard <- rchess:::.chessboarddata() %>% select(cell, col, row, x, y, cc)
chess_field <- ggplot() + geom_tile(data = dfboard, aes(x, y, fill = cc)) +
scale_fill_manual("legend", values =  c("chocolate4", "wheat1")) +
scale_x_continuous(breaks = 1:8, labels = letters[1:8]) + 
scale_y_continuous(breaks = 1:8, labels = 1:8)

This is how the chessboard is created. If I add the line + theme(text = element_text(family = "Arial Unicode MS")), I get an Error "Invalid font type"...Error in grid.call.graphics(L_text, ad.graphicsAnnot(x$label), x$x, x$y

I thought this wouldn't be so hard, including this it took me 4 hours, just for some Unicode symbols..

Lennie
  • 253
  • 6
  • 16
  • Have you seen [this](http://jkunst.com/rchess/)? Or [this](http://jkunst.com/r/visualizing-chess-data-with-ggplot/)? – Maurits Evers Oct 19 '17 at 21:58
  • Thanks! I edited my answer. I have looked at both sites before. I transfer the Unicode from the package but it still doesn't work. – Lennie Oct 20 '17 at 01:40
  • Unfortunately you don't provide a reproducible example (`chess_field` is not defined anywhere). You need to make sure that the font family that you are using supports the unicode chess characters. See my example below. – Maurits Evers Oct 20 '17 at 02:25
  • I' sry, I added the creation of the chessboard. I tried to alter the font type, didn't work. – Lennie Oct 20 '17 at 02:48
  • 1
    Hmm. There is something strange going on. Using your example data, when I do `ggsave(file = "test.pdf", chess_field + ...)` I get a lot of warnings like `font metrics unknown for Unicode character U+2654` and `conversion failure on ...`. I found a potentially related issues being discussed [here](https://stackoverflow.com/questions/5886018/using-unicode-dingbat-like-glyphs-in-r-graphics-across-devices-platforms-e/5886296#5886296). It's odd that Unicode chess symbols in the title (through `ggtitle`) are displayed correctly, but not through `geom_text`... – Maurits Evers Oct 20 '17 at 05:27
  • Is there a typo in your `symbols_w`? You are getting the vertical bar "|" because "\U25a0" is Unicode for vertical bar. You probably want "\U265A", right? At least, that is what it says according to the Wikipedia page you linked. – David Oct 21 '17 at 04:10
  • I just use the vertical bar, because that's the only symbol I got to be displayed. I will check that Encoding issue Maurits, thanks for the hint! – Lennie Oct 21 '17 at 15:35
  • I looked up a lot of things on the Internet, apparently it's a problem with RSweave, because it's using single bit encoding. Some people suggest using Cairo_pdf as output device, but for me it won't help, still working on it! – Lennie Nov 09 '17 at 17:09

1 Answers1

1

Make sure that the font family that you're using supports the Unicode chess characters.

For example the following example doesn't show the knight symbol correctly.

gg <- ggplot();
gg <- gg + ggtitle(sprintf("\u265e"));

enter image description here

However, if I change the font family to Arial Unicode MS, the symbol is displayed correctly.

gg <- ggplot();
gg <- gg + theme(text = element_text(family = "Arial Unicode MS"));
gg <- gg + ggtitle(sprintf("\u265e"));

enter image description here

Maurits Evers
  • 49,617
  • 4
  • 47
  • 68