Site icon R-bloggers

trekfont: Star Trek-themed fonts package

[This article was first published on Matt's R Blog, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

trek is now available on CRAN. This is a simple data package that exposes 107 Star Trek families. You might not want to use Star Trek s for work, but if you are making plots for fun and like Star Trek then these s are for you.

This post gives a basic example of one how to load these s in R for use in plots. Using external files is handled by the showtext package, which uses the syss package to load the s. There is no need to load the trek package directly; you only need it installed. To see all available files, call trek::treks.

This example shows eight s. _add and _families below are part of the syss package loaded by showtext. Use these to load s and then check to see that they are now available, respectively. showtext_auto handles the rest. If you are not already familiar with showtext see its documentation for broader examples.

# install.packages('trek')
library(showtext)
 <- c("Khan", "StarNext", "FederationDS9Title", "Federation", "Klingon", 
    "ModernVulcan", "TNGcast", "FederationStarfleet")
path <- system.file(paste0("s/", , ".ttf"), package = "trek")
for (i in 1:8) _add([i], path[i])
_families()
#>  [1] "sans"                "serif"               "mono"               
#>  [4] "wqy-microhei"        "Khan"                "StarNext"           
#>  [7] "FederationDS9Title"  "Federation"          "Klingon"            
#> [10] "ModernVulcan"        "TNGcast"             "FederationStarfleet"
showtext_auto()

First use base graphics.

y <- seq(0.1, 0.9, length.out = 7)
txt <- "The Quick Brown Fox Jumps Over The Lazy Dog"
plot(0, 0, type = "n", ylim = c(0, 1), main = "trek package  sample", 
    family = [8])
for (i in 1:7) text(0, y[i], txt, family = [i])

Did you ever think you would be annotating your plots in Vulcan and Klingon? Next use ggplot2.

library(ggplot2)
g <- ggplot() + theme_gray(base_family = [8]) + ggtitle("trek package  sample")
for (i in 1:7) g <- g + annotate("text", 0, y[i], label = txt, family = [i], 
    size = 12.5)
g

That’s all there is to it. Qapla’! The above code is simplified. You can tweak your par and ggplot2 theme settings to achieve a similar appearance to what is shown in the plots.

If you have trouble with the s not displaying and are receiving warnings such as

In grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, : family not found in Windows database

don’t be a petaQ. Try using showtext via the regular R GUI rather than through the RStudio IDE. Unfortunately showtext does not currently work with RStudio’s built-in graphics device.

To leave a comment for the author, please follow the link and comment on their blog: Matt's R Blog.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.