英文:
How to use the dalek font in R plots?
问题
I want to use the dalek font in my R plots. I tried to use the showtext
plot, but I had no success.
Can someone provide some guidance?
英文:
I want to use the dalek font in my R plots. I tried to use the showtext
plot, but I had no success.
Can someone provide some guidance?
答案1
得分: 1
以下是已翻译的内容:
原来需要下载常规版本的字体。使用`font_add`函数,然后使用`showtext_auto`函数在图中使用新字体。这是一个小例子:
library(latex2exp)
library(showtext)
font_add_google("Gochi Hand", "gochi")
font_add_google("Schoolbell", "bell")
font_add_google("Covered By Your Grace", "grace")
font_add_google("Rock Salt", "rock")
sysfonts::font_add(family = "dalek", # 要使用的名称
regular = "fonts/Dalek.otf") # 'General'选项卡中的文本加上字体
sysfonts::font_add(family = "ancient-helenic", # 要使用的名称
regular = "fonts/ancienthellenic-webfont.ttf") # 'General'选项卡中的文本加上字体
showtext_auto()
versicolor <- which(iris$Species=="versicolor")
x <- iris$Sepal.Length[versicolor]
y <- iris$Petal.Length[versicolor]
showtext_opts(dpi = 96)
mod <- lm(y ~ x)
op <- par(cex.lab = 2, cex.axis = 1.5, cex.main = 2)
plot(x, y, pch = 16, col = "steelblue", xlab = "Sepal Length", ylab = "Petal Length", family = "gochi")
grid()
title("Petal Length regressed on Sepal Length", family = "bell")
text(5.85, 5.1, "Outlier?", cex = 2, col = "steelblue", family = "grace")
abline(coef(mod))
abline(a = .2, b = .7, col = "red")
par(family = "rock")
text(5.2, 4, c(TeX("True model: $\\mu_Y=\\beta_0 +\\beta_1 X$")), cex = 1.5, col = "red", srt = 20, family="ancient-helenic")
text(6.3, 3.9, bquote(paste("OLS: ", hat(y) == .(unname(round(coef(mod)[1],3))) * x + .(unname(round(coef(mod)[2],3))))), cex = 1.5, srt = 15, family ="dalek")
legend("topright", legend = c("Truth", "OLS"), col = c("red", "black"), lty = 1)
par(op)
[![enter image description here][1]][1]
[1]: https://i.stack.imgur.com/d9X9S.png
英文:
It turns out that it was necessary to download a regular version of font. Use the function font_add
and then use the showtext_auto
function to use new fonts in the plots. A small example:
library(latex2exp)
library(showtext);
font_add_google("Gochi Hand", "gochi")
font_add_google("Schoolbell", "bell")
font_add_google("Covered By Your Grace", "grace")
font_add_google("Rock Salt",
"rock")
sysfonts::font_add(family = "dalek", # Name you want to use
regular = "fonts/Dalek.otf") # Text of the 'General' tab plus the font
sysfonts::font_add(family = "ancient-helenic", # Name you want to use
regular = "fonts/ancienthellenic-webfont.ttf") # Text of the 'General' tab plus the font
showtext_auto()
versicolor <- which(iris$Species=="versicolor")
x <- iris$Sepal.Length[versicolor]
y <- iris$Petal.Length[versicolor]
showtext_opts(dpi = 96);
mod <- lm(y ~ x)
op <- par(cex.lab = 2, cex.axis = 1.5, cex.main = 2)
plot(x, y, pch = 16, col = "steelblue", xlab = "Sepal Length", ylab = "Petal Length", family = "gochi");
grid()
title("Petal Length regressed on Sepal Length", family = "bell")
text(5.85, 5.1, "Outlier?", cex = 2, col = "steelblue", family = "grace")
abline(coef(mod))
abline(a = .2, b = .7, col = "red")
par(family = "rock")
text(5.2, 4, c(TeX("True model: $\\mu_Y=\\beta_0 +\\beta_1 X$")), cex = 1.5, col = "red", srt = 20, family="ancient-helenic")
text(6.3, 3.9, bquote(paste("OLS: ", hat(y) == .(unname(round(coef(mod)[1],3))) * x + .(unname(round(coef(mod)[2],3))))), cex = 1.5, srt = 15, family ="dalek")
legend("topright", legend = c("Truth", "OLS"), col = c("red", "black"), lty = 1)
par(op)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论