ggplot shape = Unicode(例如像“\ u2198”或LaTeX \ searrow这样的箭头)?

问题描述 投票:1回答:1

我想在ggplot2 geom_point()中使用Unicode形状(特别是像↘,Unicode“\ u2198”或LaTeX \ searrow这样的箭头),如shape = "\u2198"中那样,不是默认字体。在this unanswered post@Laserhedvig commented“似乎问题在于字体。显然,基本默认字体不包含对这些特定字形的支持。现在,如何更改geom_point()的形状参数的字体?”

This solution中使用axes.texttheme(axis.text.x = element_text(family = "FreeSerif"))使用this solution,而theme(text=element_text(size=16, family="Comic Sans MS"))使用text用于所有shape,但我怎样才能为shape执行此操作?

  1. 有没有为cairo使用Unicode的一般解决方案? (我必须以某种方式使用family和/或字体scale_shape documentation参数?)
  2. 如果没有,是否还有其他一些箭头形状? (我搜索箭头形状和字形,包括在library(dplyr) library(ggplot2) d <- tibble(year = c(1, 1, 2, 2), policy = rep( c('policy 1', 'policy 2'), 2), prediction = c(NA, 'increase', 'decrease', NA), predictionUnicode = c(NA, '\u2197', '\u2198', NA)) ggplot(d) + geom_point(aes(x = year, y = policy, color = prediction), shape = "\u2198") 中出现空洞。)

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

一个例子:

shape = "\u2198" (i.e. "↘") does not work

family

编辑:感谢djangodude关于ggplot字体用法的评论,我找到了geom_textgeom_text参数,它允许使用不同的字体。因此,Unicode“形状”可以用geom_text绘制为字符。然而,fixed to "a"的传奇是themes only control non-data display。和base_family,所以shape论点不适用于ggplot(d) + geom_tile( aes(x = year, y = policy), color = "black", fill = "white") + # geom_point does not allow new fonts? geom_point(aes(x = year, y = policy, color = prediction), shape = "\u2198") + # geom_text does allow new fonts, but the legend text is fixed to "a" geom_text(aes(x = year, y= policy, color = prediction, label = predictionUnicode), family = "Calibri") + scale_x_continuous(breaks = c(1,2)) + theme_gray(base_family = "Calibri")

geom_text plots unicode, but not in the legend

shape

似乎Sys.setenv(LANG = "en_US.UTF-8")论证真的是这样做的正确方法,对吗?

我尝试设置Sys.setenv(LANG = "Unicode")shape没有效果,但也许一些全球语言设置会影响skull and crossbones

非常感谢你的帮助!

注意:Unicode half-filled pointsthese instructions的这些解决方案没有图例,如果没有正确的字体,它们将无法工作:

To get the right font:

  1. 查找包含您搜索的Unicode字符的已安装字体。我发现library(extrafont) font_import() fonts() 很有帮助。
  2. 将已安装的字体导入R中
sessionInfo()
R version 3.5.2 (2018-12-20)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Mojave 10.14.3
shape="\u2198"
r ggplot2 unicode fonts shapes
1个回答
0
投票

使用\u8600而不是\u。使用qazxswpoi表示法指定字符时,该值必须为十六进制。 8600是Unicode字符LOWER RIGHT ARROW的十进制值;十六进制值是2198。

© www.soinside.com 2019 - 2024. All rights reserved.