我使用的是 R 3.5.2。在 Mac Mojave 10.14.4 上。由于几天在我的标准图中出现文本问题。使用 ggplot 绘图也不起作用。我收到消息说:
对于标准图:
Warning messages: 1: In doTryCatch(return(expr), name, parentenv, handler) : no font could be found for family "Arial"
对于带有 ggplot2 的图:
Error in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y,
: polygon edge not found
欢迎提出我如何继续查找问题并解决的任何建议?
我已经尝试过以下步骤:
par(family = "Arial")
每次都作为标准绘图的解决方法,但这不适用于 ggplot2。 根据 MacOSX,字体已全部安装、事件且不存在重复项,但我仍然无法使用 R 访问它们。
这是我正在测试的代码:
x <- seq(1, 10,1)
y <- rnorm(10, 4,1)
plot(y~x)
library(ggplot2)
# Basic normal curve
p <- ggplot(data.frame(x = c(-4, 4)), aes(x = x)) +
stat_function(fun = dnorm, size=1.5)
p
最佳答案
平行于 extrafont
,这是使用 showtext
的解决方案.
library(showtext)
font_add("Arial", "/Library/Fonts/Arial.ttf") # Use the actual file path
showtext_auto()
x <- 1:10
y <- rnorm(10, 4, 1)
pdf("plot1.pdf")
plot(y ~ x, family = "Arial")
dev.off()
library(ggplot2)
# Basic normal curve
p <- ggplot(data.frame(x = c(-4, 4)), aes(x = x)) +
stat_function(fun = dnorm, size = 1.5) +
theme(text = element_text(family = "Arial"))
ggsave("plot2.pdf", p)
关于R 找不到要用于绘图的字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55933524/