2017-10-19 73 views
1

我正试图在R剧情中显示国际象棋符号。我在互联网上搜索了很多,但我找不到答案。如何在R图中显示国际象棋(unicode)符号?

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) 

我拿的Unicode从这里的国际象棋数字:https://en.wikipedia.org/wiki/Chess_symbols_in_Unicode

我得到了这些图片的结果是: enter image description here

它* S没有得到正确显示,也许你能帮助我吗?

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

编辑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) 

这是棋盘是如何创建的。如果我添加行+主题(text = element_text(family =“Arial Unicode MS”)),我得到一个错误“Invalid font type”...错误在grid.call.graphics(L_text,ad.graphicsAnnot(x $标签),X $ X,X $ Y

我想这不会是这么辛苦,包括这个,我花了4个小时,只为一些Unicode的符号..

+0

你见过[这](http://jkunst.com/rchess/)?或者[this](http://jkunst.com/r/visualizing-chess-data-with-ggplot/)? –

+0

谢谢!我编辑了我的答案。我曾经看过这两个网站。我从包中传输Unicode,但它仍然不起作用。 – Lennie

+0

不幸的是,你不提供一个可重复的例子('chess_field'没有在任何地方定义)。您需要确保您使用的字体系列支持unicode国际象棋字符。看我下面的例子。 –

回答

1

确保字体系列您正在使用支持Unicode字符棋。

例如下面的示例中没有正确显示的骑士象征。

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

enter image description here

但是,如果我将字体族更改为Arial Unicode MS,则符号显示正确。

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

enter image description here