ggplot2轴中的乱码韩文和中文字符

时间:2016-07-03 10:03:55

标签: r ggplot2 character-encoding

我无法让ggplot2正确地在轴上渲染韩文和中文字符,如下所示:

Axes with garbled Korean and Chinese characters

顶部的项目是朝鲜语,而底部的第5项是汉字。

您可以重现"情节"使用以下代码:

top_artists <- structure(
  list(
    artist = c("John Mayer", "Jorge Drexler", "Netsky", 
               "Daft Punk", "Joe Satriani", "Steve Vai", "Zero 7", "Pendulum", 
               "Jamie Cullum", "소녀시대", "Coldplay", "Killswitch Engage", 
               "Andy McKee", "toe", "Bombay Bicycle Club", "Roadrunner United", 
               "Kaskade", "Robot Science", "HOME MADE 家族", "Opeth"),
    genre = c("singer-songwriter", "singer-songwriter", "drum and bass",
              "electronic", "guitar virtuoso", "guitar virtuoso",
              "chillout", "drum and bass", "jazz", "k-pop", 
              "rock", "metalcore", "acoustic", "post-rock", "indie", "metal", 
              "house", "electronic", "hip-hop", "progressive metal"),
    n = c(760L, 603L, 564L, 428L, 418L, 417L, 417L,
          410L, 407L, 402L, 387L, 385L, 319L, 303L,
          292L, 289L, 275L, 257L, 256L, 244L)),
  class = c("tbl_df", "tbl", "data.frame"),
  row.names = c(NA, -20L),
  .Names = c("artist", "genre", "n"))

ggplot(top_artists, aes(artist, n)) + coord_flip()

我意识到我应该有geoms来实际显示数据,但我认为我会尽可能地减少数据。这个问题仍然存在于完整的情节中,包括几何,缩放和主题设置......

这是我的sessionInfo:

R version 3.3.0 (2016-05-03)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.11.5 (El Capitan)

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] viridis_0.3.4       scales_0.4.0        hrbrmisc_0.0.0.9002 zoo_1.7-13          streamgraph_0.8.1  
[6] ggplot2_2.1.0       lubridate_1.5.6     tidyr_0.5.1         dplyr_0.5.0        

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.5      rstudioapi_0.6   magrittr_1.5     munsell_0.4.3    xtable_1.8-2    
 [6] colorspace_1.2-6 lattice_0.20-33  R6_2.1.2         stringr_1.0.0    plyr_1.8.4      
[11] tools_3.3.0      xts_0.9-7        grid_3.3.0       gtable_0.2.0     miniUI_0.1.1    
[16] DBI_0.4-1        htmltools_0.3.5  lazyeval_0.2.0   assertthat_0.1   digest_0.6.9    
[21] tibble_1.0       gridExtra_2.2.1  shiny_0.13.2     formatR_1.4      htmlwidgets_0.6 
[26] mime_0.4         memoise_1.0.0    labeling_0.3     stringi_1.1.1    httpuv_1.3.3

1 个答案:

答案 0 :(得分:0)

事实证明,该问题与默认ggplot2主题在MacOS上使用的字体有关,该字体不支持韩文/日文。

要修复它,我必须手动将字体设置为&#34; HiraginoSans-W3&#34;。

theme(axis.title = element_text(font = "HiraginoSans-W3"))

相关问题