简体   繁体   English

使用Unicode字符作为形状

[英]Using unicode characters as shape

I'd like to use unicode characters as the shape of plots in ggplot, but for unknown reason they're not rendering. 我想将unicode字符用作ggplot中图形的形状,但由于未知原因,它们没有呈现。 I did find a similar query here , but I can't make the example there work either. 我确实在这里找到了类似的查询,但是我也无法使该示例正常工作。

Any clues as to why? 为什么有任何线索?

Note that I don't want to use the unicode character as a "palette", I want each item plotted by geom_point() to be the same shape (color will indicate the relevant variable). 请注意,我不想将unicode字符用作“调色板”,我希望geom_point()绘制的每个项目都具有相同的形状(颜色将指示相关变量)。

Running 跑步

Sys.setenv(LANG = "en_US.UTF-8")

and restarting R does not help. 重新启动R并没有帮助。 Wrapping the unicode in sprintf() also does not help. 在sprintf()中包装unicode也无济于事。

This is an example bit of code that illustrates the problem: 这是说明问题的代码示例:

library(tidyverse)
library(ggplot2)
library(Unicode)

p1 = ggplot(mtcars, aes(wt, mpg)) +
  geom_point(shape="\u25D2", colour="red", size=3) +
  geom_point(shape="\u25D3", colour="blue", size=3) + 
  theme_bw()

plot(p1)

And here's what that renders result. 这就是结果。

在此处输入图片说明

I use macOS Sierra (10.13.6), R version 3.5.1 & Rstudio 1.0.143. 我使用macOS Sierra(10.13.6),R版本3.5.1和Rstudio 1.0.143。

Grateful for any help! 感谢您的帮助! I've been scouting several forums looking for a solution and posted to #Rstats, so far nothing has worked. 我一直在几个论坛上寻找解决方案,并将其发布到#Rstats上,到目前为止没有任何效果。 It may be that the solution is hidden in some thread somewhere, but if so I have failed to detect it and I suspect others have also missed it. 解决方案可能隐藏在某个线程中的某个位置,但是如果这样,我将无法检测到它,并且我怀疑其他人也错过了它。 So, here I am making my first ever post to stack overflow :) 所以,在这里,我正在做我的第一个帖子以堆栈溢出:)

Might it work to use geom_text instead? 改为使用geom_text吗? It allows control of the font, so you can select one with the glyph you need. 它允许控制字体,因此您可以选择带有所需字形的字体。

library(tidyverse)
ggplot(mtcars, aes(wt, mpg)) +
  geom_text(label = "\u25D2", aes(color = as.character(gear)),  
            size=10, family = "Arial Unicode MS") +
  geom_text(label = "\u25D3", colour="blue", 
            size=10, family = "Arial Unicode MS") + 
  scale_color_discrete(name = "gear") +
  theme_bw()

在此处输入图片说明

It's possible to change the font family using par . 可以使用par更改字体系列。 The problem is that this will affect base R graphics but not ggplot2 graphics, as they use two different graphics devices ( grDevices vs. grid ). 问题在于,这将影响基本R图形,但不会影响ggplot2图形,因为它们使用两种不同的图形设备( grDevices vs. grid )。 For instance, we can try to plot your example using base R functions, but at first we see the same issue: 例如,我们可以尝试使用基本R函数来绘制示例,但是起初我们会看到相同的问题:

plot(mtcars$wt, mtcars$mpg, pch="\u25D2", col = "red", cex = 2)
points(mtcars$wt, mtcars$mpg, pch="\u25D3", col = "blue", cex = 2)

基本R图,不呈现unicode字符

We can get what we want if we call par first (the font should support the symbols): 如果先调用par就可以得到想要的(字体应支持符号):

par(family = "Arial Unicode MS")
plot(mtcars$wt, mtcars$mpg, pch="\u25D2", col = "red", cex = 2)
points(mtcars$wt, mtcars$mpg, pch="\u25D3", col = "blue", cex = 2)

基本R图,呈现unicode

Changing the font family parameter that specifically affects the points in a ggplot geom_point appears to be a bit more complicated. 更改特别影响ggplot geom_point中的点的字体系列参数似乎要复杂一些。 As far as I can tell, it would involve turning the ggplot object into a grob, editing the parameters, and then drawing it. 据我所知,这将涉及将ggplot对象转换为grob,编辑参数然后进行绘制。 It probably makes more sense to either use Jon Spring's geom_text solution, or use base R. 使用Jon Spring的geom_text解决方案或使用基数R可能更有意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM