我使用的是 R 3.5.2。在 Mac 莫哈韦 10.14.4 上。几天后,我的标准图中的文本出现了问题。用 ggplot 画图也不起作用。我收到消息说:
对于标准图:
警告消息:1:在 doTryCatch(return(expr), name, Parentenv, handler) 中:找不到系列“Arial”的字体
对于 ggplot2 的绘图:
grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, 中的错误
:找不到多边形边缘
欢迎提出如何继续查找问题并解决问题的建议?
我已经尝试过以下步骤:
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
Run Code Online (Sandbox Code Playgroud)
平行于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)
Run Code Online (Sandbox Code Playgroud)
小智 0
你尝试过使用font_import()吗?然后,fonts()给我可以使用的新字体(以前没有)。
之后,我可以使用family-attribute编辑字体theme():
x <- seq(1, 10,1)
y <- rnorm(10, 4,1)
plot(y~x)
library(ggplot2)
library(extrafont)
fonts()
font_import(prompt = TRUE)
fonts()
# 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"))
p
Run Code Online (Sandbox Code Playgroud)
更新:即使没有extrafont-package 我也能正常工作。
| 归档时间: |
|
| 查看次数: |
7780 次 |
| 最近记录: |