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

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

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

以下是已翻译的内容:

  1. 原来需要下载常规版本的字体。使用`font_add`函数,然后使用`showtext_auto`函数在图中使用新字体。这是一个小例子:
  2. library(latex2exp)
  3. library(showtext)
  4. font_add_google("Gochi Hand", "gochi")
  5. font_add_google("Schoolbell", "bell")
  6. font_add_google("Covered By Your Grace", "grace")
  7. font_add_google("Rock Salt", "rock")
  8. sysfonts::font_add(family = "dalek", # 要使用的名称
  9. regular = "fonts/Dalek.otf") # 'General'选项卡中的文本加上字体
  10. sysfonts::font_add(family = "ancient-helenic", # 要使用的名称
  11. regular = "fonts/ancienthellenic-webfont.ttf") # 'General'选项卡中的文本加上字体
  12. showtext_auto()
  13. versicolor <- which(iris$Species=="versicolor")
  14. x <- iris$Sepal.Length[versicolor]
  15. y <- iris$Petal.Length[versicolor]
  16. showtext_opts(dpi = 96)
  17. mod <- lm(y ~ x)
  18. op <- par(cex.lab = 2, cex.axis = 1.5, cex.main = 2)
  19. plot(x, y, pch = 16, col = "steelblue", xlab = "Sepal Length", ylab = "Petal Length", family = "gochi")
  20. grid()
  21. title("Petal Length regressed on Sepal Length", family = "bell")
  22. text(5.85, 5.1, "Outlier?", cex = 2, col = "steelblue", family = "grace")
  23. abline(coef(mod))
  24. abline(a = .2, b = .7, col = "red")
  25. par(family = "rock")
  26. 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")
  27. 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")
  28. legend("topright", legend = c("Truth", "OLS"), col = c("red", "black"), lty = 1)
  29. par(op)

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

  1. [![enter image description here][1]][1]
  2. [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:

  1. library(latex2exp)
  2. library(showtext);
  3. font_add_google(&quot;Gochi Hand&quot;, &quot;gochi&quot;)
  4. font_add_google(&quot;Schoolbell&quot;, &quot;bell&quot;)
  5. font_add_google(&quot;Covered By Your Grace&quot;, &quot;grace&quot;)
  6. font_add_google(&quot;Rock Salt&quot;,
  7. &quot;rock&quot;)
  8. sysfonts::font_add(family = &quot;dalek&quot;, # Name you want to use
  9. regular = &quot;fonts/Dalek.otf&quot;) # Text of the &#39;General&#39; tab plus the font
  10. sysfonts::font_add(family = &quot;ancient-helenic&quot;, # Name you want to use
  11. regular = &quot;fonts/ancienthellenic-webfont.ttf&quot;) # Text of the &#39;General&#39; tab plus the font
  12. showtext_auto()
  13. versicolor &lt;- which(iris$Species==&quot;versicolor&quot;)
  14. x &lt;- iris$Sepal.Length[versicolor]
  15. y &lt;- iris$Petal.Length[versicolor]
  16. showtext_opts(dpi = 96);
  17. mod &lt;- lm(y ~ x)
  18. op &lt;- par(cex.lab = 2, cex.axis = 1.5, cex.main = 2)
  19. plot(x, y, pch = 16, col = &quot;steelblue&quot;, xlab = &quot;Sepal Length&quot;, ylab = &quot;Petal Length&quot;, family = &quot;gochi&quot;);
  20. grid()
  21. title(&quot;Petal Length regressed on Sepal Length&quot;, family = &quot;bell&quot;)
  22. text(5.85, 5.1, &quot;Outlier?&quot;, cex = 2, col = &quot;steelblue&quot;, family = &quot;grace&quot;)
  23. abline(coef(mod))
  24. abline(a = .2, b = .7, col = &quot;red&quot;)
  25. par(family = &quot;rock&quot;)
  26. 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;)
  27. 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;)
  28. legend(&quot;topright&quot;, legend = c(&quot;Truth&quot;, &quot;OLS&quot;), col = c(&quot;red&quot;, &quot;black&quot;), lty = 1)
  29. 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:

确定