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

时间:2017-06-13 14:59:51

标签: r unicode ggplot2 rstudio

如何在ggplot图表中的小平面标签(或任何地方,真的)中显示unicode字符?

有相当多的相关帖子浮动,但没有人为我做过这个伎俩。 (我解释为什么最后都没有重复)

所以,鉴于此代码:

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时,一切都按预期显示。

我的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   

有关此内容的其他帖子

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 个答案:

答案 0 :(得分: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