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')
...我得到了这个情节(注意缺少的方面标签):
当我使用 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 labels,utf-8 in ggplot axis labels,这基本相同:Use a half filled squares on ggplot2 facet_wrap labels,How 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 回答
sprintf
适用于Windows和Rstudio 1.0.143comfortable way to use unicode characters in a ggplot graph
会话信息: