简体   繁体   中英

R plots with X11 cannot show CJK fonts

I loaded a UTF-8 csv file with Japanese characters in it, its str is like this:

> str(purchases)
'data.frame':   168996 obs. of  7 variables:
 $ ITEM_COUNT     : int  1 1 1 1 1 1 2 2 1 1 ...
 $ I_DATE         : Date, format: "2012-03-28" "2011-07-04" ...
 $ SMALL_AREA_NAME: Factor w/ 55 levels "キタ","ミナミ他",..: 6 47 26 26 26 26 26 35 35 26 ...
 $ USER_ID_hash   : Factor w/ 22782 levels "0000b53e182165208887ba65c079fc21",..: 19467 7623 7623 7623 7623 7623 7623 7623 7623 7623 ...
 $ COUPON_ID_hash : Factor w/ 19368 levels "000eba9b783cec10658308b5836349f6",..: 3929 8983 5982 5982 5982 5982 5982 2737 18489 5018 ...
 $ category       : Factor w/ 13 levels "Beauty","Delivery service",..: 2 3 2 2 2 2 2 7 2 3 ...

So I think there's nothing wrong with my encoding or locale(en_US.UTF-8)? But when I plot with

> barplot(table(purchases$SMALL_AREA_NAME))

why do the Japanese characters turn into little blocks like this? R图

I think I have the font to display Japanese characters

> names(X11Fonts())
[1] "serif"        "sans"         "mono"         "Times"        "Helvetica"   
[6] "CyrTimes"     "CyrHelvetica" "Arial"        "Mincho"      

Additional info:

> sessionInfo()
R version 3.2.2 (2015-08-14)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.10.5 (Yosemite)

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] ggplot2_1.0.1

You may want to take a look at the showtext package, which allows you to use different fonts in R graphs. It also ships with a CJK font that can be used directly.

Try to run the code below:

library(showtext)
showtext.auto()
## ... code to generate data
barplot(table(purchases$SMALL_AREA_NAME))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM