2017-10-19 71 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