我正在尝试在 ggplot 中包含不同的数字,但该包无法识别 unicode。例如,当我尝试这个时:
library(dplyr)
library(tidyverse)
ig_5 <- data.frame(
category = c("A", "B", "C", "D", "E", "F"),
prop = c(0.1, 0.2, 0.15, 0.25, 0.05, 0.25)) %>%
mutate(lab.ypos = cumsum(prop) - 0.5*prop,
lab.ypos2 = cumsum(prop) + 0.3*prop)
ggplot(ig_5, aes(x = 2, y = prop, fill = prop)) +
geom_bar(stat = "identity", color = "white") +
coord_polar(theta = "y", start = 0) +
geom_text(aes(y = lab.ypos, label = prop), color = "black", size = 5) +
geom_label(aes(y = lab.ypos,
label = "\u26AA"),
vjust = 1.5,
color = 'red') +
xlim(.5, 2.5)
我明白了:
而不是这个:
在DataCamp Workspace中运行代码得到后一张图
问题只出现在 ggplot 上。如果我打印这个:“\u26AA”,我得到了我应该得到的。
"⚪"
此外,问题不存在于R中,仅存在于Rstudio中。
我的会话信息如下:
R version 4.3.0 (2023-04-21 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 11 x64 (build 22621)
Matrix products: default
locale:
[1] LC_COLLATE=Spanish_Ecuador.utf8 LC_CTYPE=Spanish_Ecuador.utf8 LC_MONETARY=Spanish_Ecuador.utf8 LC_NUMERIC=C
[5] LC_TIME=Spanish_Ecuador.utf8
time zone: America/Guayaquil
tzcode source: internal
有什么想法吗?
你在这里看到的很可能与图形后端和字体处理有关。您正在使用 (默认) / Windows 后端,对吗? (工具 > 全局选项 > 常规 > 图形 > 后端)。
尝试切换到 AGG(您可能需要先安装
ragg
包)并测试:
library(ggplot2)
ggplot() + geom_label(aes(x = 0, y = 0, label = "\u26AA")) + theme_void()
Windows 渲染器可能会返回下面的第一张图像,而使用 AGG 你应该会看到第二张输出。
更多信息和样本在这里 - https://www.tidyverse.org/blog/2021/02/modern-text-features/#color-fonts