rggplot2plotalphabet

Plotting characters from the Hershey fonts (package) using R's base plot


This question originates from: r - How to plot alphabets? which is where I came across the hershey package and the vignette. I tried the following:

string_df <- hershey::create_string_df(text = "CRAN", font = 'astrology')

ggplot(string_df, aes(x, y, group = interaction(char_idx, stroke))) +
  geom_path() +
  geom_point(size = 1) + 
  coord_equal() +
  theme_void()

which works well and produces:

enter image description here

However, I would like more control of this plot so that I can make more changes. For instance, I would like to replace the R above with the 'R` logo at: https://www.r-project.org/logo/Rlogo.svg

It appears to me that I can do that better with using the base R plot, however, how do I display the astrology font for the above in base R?


Solution

  • Here's a first pass using a loop. Would have to play with par to make it look more similar.

    string_df <- hershey::create_string_df(text = "CRAN", font = 'astrology')
    
    string_df$i = with(string_df, interaction(char_idx, stroke))
    
    plot(NA, xlim=range(string_df$x), ylim=range(string_df$y), 
         xlab="", ylab="", axes=FALSE)
    
    for (i in unique(string_df$i)){
        tmp = string_df[string_df$i == i, ]
        with(tmp, lines(x,y))
        with(tmp, points(x,y, pch=16))
    }
    

    Created on 2025-07-25 with reprex v2.1.1