当我运行 showtext::showtext_auto()
启用 showtext 包自动处理中文的能力后,运行如下代码块
library(ggplot2)
ggplot() +
geom_function(
fun = dgamma, args = list(shape = 9, scale = 1),
aes(colour = "dgamma(x,9,1)"),
linewidth = 1.2, xlim = c(0, 20)
) +
geom_function(
fun = dgamma, args = list(shape = 5, scale = 1),
aes(colour = "dgamma(x,5,1)"),
linewidth = 1.2, xlim = c(0, 20)
) +
scale_colour_viridis_d(
labels = c(
"dgamma(x,9,1)" = expression(paste(alpha == 9, ", ", sigma == 1)),
"dgamma(x,5,1)" = expression(paste(alpha == 5, ", ", sigma == 1))
)
) +
theme_bw()
会发现图例中的表达式集体向右挪了一步,见下图
如果不启用 showtext 包,表达式位置显示正常,见下图。
不知道是不是 showtext 包的 BUG?它是不是处理中文的同时对数学表达式不兼容,我不知道是什么原因?
运行信息
xfun::session_info(packages = c("ggplot2", "showtext"), dependencies = F)
R version 4.3.0 (2023-04-21)
Platform: x86_64-apple-darwin22.4.0 (64-bit)
Running under: macOS Ventura 13.3.1, RStudio 2023.3.0.386
Locale: en_US.UTF-8 / en_US.UTF-8 / en_US.UTF-8 / C / en_US.UTF-8 / en_US.UTF-8
time zone: Asia/Shanghai
tzcode source: internal
Package version:
ggplot2_3.4.2 showtext_0.9-5