如何在R绘图中使用dalek字体?

huangapple go评论77阅读模式
英文:

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?

如何在R绘图中使用dalek字体?

英文:

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?

如何在R绘图中使用dalek字体?

答案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)

如何在R绘图中使用dalek字体?


[![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(&quot;Gochi Hand&quot;, &quot;gochi&quot;)
font_add_google(&quot;Schoolbell&quot;, &quot;bell&quot;)
font_add_google(&quot;Covered By Your Grace&quot;, &quot;grace&quot;)
font_add_google(&quot;Rock Salt&quot;,
&quot;rock&quot;)
sysfonts::font_add(family = &quot;dalek&quot;, # Name you want to use 
                   regular = &quot;fonts/Dalek.otf&quot;) # Text of the &#39;General&#39; tab plus the font
sysfonts::font_add(family = &quot;ancient-helenic&quot;, # Name you want to use 
                   regular = &quot;fonts/ancienthellenic-webfont.ttf&quot;) # Text of the &#39;General&#39; tab plus the font 
showtext_auto()

versicolor &lt;- which(iris$Species==&quot;versicolor&quot;)
x &lt;- iris$Sepal.Length[versicolor]
y &lt;- iris$Petal.Length[versicolor]

showtext_opts(dpi = 96);
mod &lt;-  lm(y ~ x)
op &lt;-  par(cex.lab = 2, cex.axis = 1.5, cex.main = 2)
plot(x, y, pch = 16, col = &quot;steelblue&quot;, xlab = &quot;Sepal Length&quot;, ylab = &quot;Petal Length&quot;, family = &quot;gochi&quot;);
grid()
title(&quot;Petal Length regressed on Sepal Length&quot;, family = &quot;bell&quot;)
text(5.85, 5.1, &quot;Outlier?&quot;, cex = 2, col = &quot;steelblue&quot;, family = &quot;grace&quot;)
abline(coef(mod))
abline(a = .2, b = .7, col = &quot;red&quot;)
par(family = &quot;rock&quot;)
text(5.2, 4, c(TeX(&quot;True model: $\\mu_Y=\\beta_0 +\\beta_1 X$&quot;)), cex = 1.5, col = &quot;red&quot;, srt = 20, family=&quot;ancient-helenic&quot;)
text(6.3, 3.9, bquote(paste(&quot;OLS: &quot;, hat(y) == .(unname(round(coef(mod)[1],3))) * x + .(unname(round(coef(mod)[2],3))))), cex = 1.5, srt = 15, family =&quot;dalek&quot;)
legend(&quot;topright&quot;, legend = c(&quot;Truth&quot;, &quot;OLS&quot;), col = c(&quot;red&quot;, &quot;black&quot;), lty = 1)
par(op)

如何在R绘图中使用dalek字体?

huangapple
  • 本文由 发表于 2023年6月12日 07:31:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76452900.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定