首页 文章

ggplot facet_wrap中的Unicode(en_US.UTF-8语言环境)

提问于
浏览
1

How can I get unicode characters to appear in the facet labels (or anywhere, really) in ggplot charts?

有很多相关的帖子漂浮在周围,但没有一个对我有用 . (我解释为什么最后都没有重复)

所以,鉴于此代码:

library(ggplot2)

facets <- c('✓', '✗')
facets2 <- c('\u2713', '\u2717')
facets3 <- c('check', 'x')

set.seed(123)
my_df <- data.frame(x = runif(40), y = runif(40), 
                    z = rep(facets, each=20),
                    stringsAsFactors = F)

ggplot(my_df, aes(x, y, color=z)) + geom_point() + 
  facet_wrap(~z) +
  theme(legend.position = 'none')

...我得到了这个情节(注意缺少的方面标签):

enter image description here

当我使用 facets2 作为标签时(即指定转义的char代码而不是文字),我得到相同的结果,但当然,当我使用 facets3 时,一切都应该出现 .

My sessionInfo()

我正在使用R Studio 1.0.136而我的 sessionInfo()

R version 3.3.1 (2016-06-21)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.11.6 (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] ggplot2_2.2.1

loaded via a namespace (and not attached):
 [1] labeling_0.3     colorspace_1.2-6 scales_0.4.1     assertthat_0.1   lazyeval_0.2.0  
 [6] plyr_1.8.4       tools_3.3.1      gtable_0.2.0     tibble_1.2       Rcpp_0.12.11.2  
[11] grid_3.3.1       digest_0.6.12    munsell_0.4.3

Other posts about this

1)这些没有答案(Unicode characters in ggplot labelsutf-8 in ggplot axis labels,这基本相同:Use a half filled squares on ggplot2 facet_wrap labelsHow can I get a unicode symbol into factor levels for a ggplot?

2)这是关于希腊字母而不是任何unicode,答案似乎对我不起作用(ggplot unicode characters without Cairo?

3)最常见的解决方案似乎涉及 cairo_pdf() ,例如正如本文所述:using Unicode 'dingbat-like' glyphs in R graphics, across devices & platforms, especially PDF .

但是,这是关于pdf输出而不是R Studio预览窗口,其中我也想看到unicode标签 .

在任何情况下,当我在我的示例中使用 cairo_pdf() 进行ggplot调用之前,ggplot调用只是挂起而我必须终止R.

4)对上面某些帖子的评论表明该问题与使用具有英语语言环境的Windows有关,但我在OS X上使用UTF-8语言环境 .

我很感激任何建议!

1 回答

  • 1

    sprintf 适用于Windows和Rstudio 1.0.143

    comfortable way to use unicode characters in a ggplot graph

    library(ggplot2)
    
    facets <- sprintf(c('✓', '✗'))
    facets2 <- sprintf(c('\u2713', '\u2717'))
    facets3 <- sprintf(c('check', 'x'))
    
    set.seed(123)
    my_df <- data.frame(x = runif(40), y = runif(40), 
                    z = rep(facets, each=20),
                    stringsAsFactors = F)
    
    ggplot(my_df, aes(x, y, color=z)) + geom_point() + 
      facet_wrap(~z) +
      theme(legend.position = 'none')
    

    会话信息:

    R version 3.4.0 (2017-04-21)
    Platform: x86_64-w64-mingw32/x64 (64-bit)
    Running under: Windows 7 x64 (build 7601) Service Pack 1
    
    Matrix products: default
    
    locale:
    [1] LC_COLLATE=French_France.1252  LC_CTYPE=French_France.1252    LC_MONETARY=French_France.1252 LC_NUMERIC=C                  
    [5] LC_TIME=French_France.1252    
    
    attached base packages:
    [1] stats     graphics  grDevices utils     datasets  methods   base     
    
    other attached packages:
    [1] ggplot2_2.2.1    xts_0.9-7        zoo_1.8-0        dygraphs_1.1.1.4
    
    loaded via a namespace (and not attached):
     [1] Rcpp_0.12.11     lattice_0.20-35  digest_0.6.12    plyr_1.8.4       grid_3.4.0       jsonlite_1.4     gtable_0.2.0     magrittr_1.5    
     [9] scales_0.4.1     rlang_0.1.1      lazyeval_0.2.0   labeling_0.3     tools_3.4.0      htmlwidgets_0.8  munsell_0.4.3    yaml_2.1.14     
    [17] compiler_3.4.0   colorspace_1.3-2 htmltools_0.3.6  tibble_1.3.3
    

相关问题