ggplot shape = Unicode(例如“\u2198”或 LaTeX \searrow 等箭头)?

Dev*_*ord 6 unicode fonts r shapes ggplot2

我想在 中使用 Unicode 形状ggplot2 geom_point()(具体来说,箭头如 \xe2\x86\x98、Unicode“\\u2198”或 LaTeX \\searrow),如 中 shape = "\\u2198",这些形状不是默认字体。在这篇未答复的帖子中,@Laserhedvig 评论道“问题似乎出在字体上。显然,基本默认字体不包含对这些特定字形的支持。现在,如何更改 geom_point 的形状参数的字体( )?”

\n\n

该解决方案axes.text适用于 Unicode theme(axis.text.x = element_text(family = "FreeSerif")),并且该解决方案适用theme(text=element_text(size=16, family="Comic Sans MS"))于所有人text,但我该如何做到这一点shape

\n\n
    \n
  1. 是否有使用 Unicode 的通用解决方案shape?(我必须以某种方式使用cairo和/或字体family参数吗?)
  2. \n
  3. 如果没有,是否还有其他一组箭头形状?(我对箭头形状和字形的搜索,包括在scale_shape文档中,结果都是空的。)
  4. \n
\n\n

就我而言,我需要一个 ggplot2 图层,显示跨离散类别的时间点变化方向的定性预测。

\n\n

一个例子:

\n\n
library(dplyr)\nlibrary(ggplot2)\n\nd <- tibble(year = c(1, 1, 2, 2),\n       policy = rep( c(\'policy 1\', \'policy 2\'), 2),\n       prediction = c(NA, \'increase\', \'decrease\', NA),\n       predictionUnicode = c(NA, \'\\u2197\', \'\\u2198\', NA))\n\nggplot(d) + \n  geom_point(aes(x = year, y = policy, color = prediction), shape = "\\u2198")\n
Run Code Online (Sandbox Code Playgroud)\n\n

shape =“\\u2198”(即“\xe2\x86\x98”)不起作用

\n\n

编辑:感谢 djangodude 关于 ggplot 字体使用的评论,我找到了family的参数geom_text,它允许使用不同的字体。因此,Unicode“形状”可以用 绘制为字符geom_text。但是,图例geom_text固定为 "a"。并且主题仅控制非数据显示,因此该base_family参数不适用于shape.

\n\n
ggplot(d) + \n  geom_tile( aes(x = year, y = policy), color = "black", fill = "white") +   \n  # geom_point does not allow new fonts?\n  geom_point(aes(x = year, y = policy, \n                 color = prediction), shape = "\\u2198") + \n  # geom_text does allow new fonts, but the legend text is fixed to "a"\n  geom_text(aes(x = year, y= policy, \n                color = prediction,\n                label = predictionUnicode), \n            family = "Calibri") + \n  scale_x_continuous(breaks = c(1,2)) +\n  theme_gray(base_family = "Calibri") \n
Run Code Online (Sandbox Code Playgroud)\n\n

geom_text 绘制 unicode,但不在图例中

\n\n

看来这个shape论证确实是正确的方法,对吗?

\n\n

我尝试设置Sys.setenv(LANG = "en_US.UTF-8")Sys.setenv(LANG = "Unicode")没有效果,但也许某些全局语言设置会影响shape

\n\n

非常感谢您的帮助!

\n\n

注意:这些针对 Unicode骷髅和交叉骨以及半填充点的解决方案没有图例,如果没有正确的字体,这些解决方案将无法工作:

\n\n

要获得正确的字体:

\n\n
    \n
  1. 查找包含您要查找的 Unicode 字符的已安装字体。我发现这些说明很有帮助。

  2. \n
  3. 将安装的字体导入到 R 中

  4. \n
\n\n
library(extrafont)\nfont_import()\nfonts()\n
Run Code Online (Sandbox Code Playgroud)\n\n
sessionInfo()\nR version 3.5.2 (2018-12-20)\nPlatform: x86_64-apple-darwin15.6.0 (64-bit)\nRunning under: macOS Mojave 10.14.3\n
Run Code Online (Sandbox Code Playgroud)\n

dja*_*ude 0

使用shape="\u2198"而不是\u8600. 当用\u符号指定字符时,该值需要是十六进制。8600 是 Unicode 字符右下箭头的十进制值;十六进制值为 2198。