Lennie
Lennie

Reputation: 363

How to display chess (unicode) symbols in a R Plot?

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..

Upvotes: 1

Views: 1211

Answers (1)

Maurits Evers
Maurits Evers

Reputation: 50718

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

Upvotes: 1

Related Questions