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

arv*_*000 5 unicode r ggplot2 rstudio

如何才能使unicode字符出现在ggplot图表的构面标签中(或实际上,在任何地方)?

周围有很多相关的帖子,但是没有一个对我有用。(我指出了为什么最后没有重复项)

因此,给出以下代码:

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')
Run Code Online (Sandbox Code Playgroud)

...我得到此图(注意缺少的构面标签):

在此处输入图片说明

当我使用facets2标签时(即指定转义的字符代码而不是文字),我会得到相同的结果,但是当我使用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   
Run Code Online (Sandbox Code Playgroud)

关于此的其他帖子

1)这些没有答案(在ggplot标签Unicode字符UTF-8 ggplot轴标签这是基本相同的使用上GGPLOT2 facet_wrap标签半实心方形我怎样才能得到一个Unicode符号为因子水平的ggplot ?

2)这是关于希腊字母而不是任何unicode,而且答案似乎对我也不起作用没有开罗的ggplot unicode字符?

3)最常见的解决方案似乎涉及cairo_pdf()(例如,如本文中所建议的那样):在设备和平台(尤其是PDF)中的R图形中使用Unicode“类似于dingbat的”字形

但是,这是关于pdf输出,而不是R Studio预览窗口,在该窗口中,我还希望看到unicode标签。

无论如何,当我在示例中使用cairo_pdf()进行ggplot调用之前时,ggplot调用只是挂起,我必须终止R。

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

我将不胜感激任何建议!

s.b*_*nel 2

sprintf适用于 Windows 和 Rstudio 1.0.143

\n\n

在 ggplot 图中使用 unicode 字符的舒适方式

\n\n
library(ggplot2)\n\nfacets <- sprintf(c(\'\xe2\x9c\x93\', \'\xe2\x9c\x97\'))\nfacets2 <- sprintf(c(\'\\u2713\', \'\\u2717\'))\nfacets3 <- sprintf(c(\'check\', \'x\'))\n\nset.seed(123)\nmy_df <- data.frame(x = runif(40), y = runif(40), \n                z = rep(facets, each=20),\n                stringsAsFactors = F)\n\nggplot(my_df, aes(x, y, color=z)) + geom_point() + \n  facet_wrap(~z) +\n  theme(legend.position = \'none\')\n
Run Code Online (Sandbox Code Playgroud)\n\n

会议信息:

\n\n
R version 3.4.0 (2017-04-21)\nPlatform: x86_64-w64-mingw32/x64 (64-bit)\nRunning under: Windows 7 x64 (build 7601) Service Pack 1\n\nMatrix products: default\n\nlocale:\n[1] LC_COLLATE=French_France.1252  LC_CTYPE=French_France.1252    LC_MONETARY=French_France.1252 LC_NUMERIC=C                  \n[5] LC_TIME=French_France.1252    \n\nattached base packages:\n[1] stats     graphics  grDevices utils     datasets  methods   base     \n\nother attached packages:\n[1] ggplot2_2.2.1    xts_0.9-7        zoo_1.8-0        dygraphs_1.1.1.4\n\nloaded via a namespace (and not attached):\n [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    \n [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     \n[17] compiler_3.4.0   colorspace_1.3-2 htmltools_0.3.6  tibble_1.3.3   \n
Run Code Online (Sandbox Code Playgroud)\n