1

I would like to create a plot in R using dice font for values 1 to 6. Dice font for windows can be downloaded here - copy the downloaded true type font to the C:\Windows\Fonts directory and it can be used in windows applications like Excel or Word.

I found out about the showtext package in this question for adding fonts to R which seems to add the dice font using the R script below.

require(showtext)
font_add(family = "dice",regular = "C:/Users/Mark/AppData/Local/Microsoft/Windows/Fonts/dice.ttf")

For some reason, I don't understand I had to set the path to the AppData/Local directory to get font_add to no report a could-not-find-it error. The next challenge was to get was to make a simple test plot

plot(c(1,2),c(2,1),pch = 0, col = "blue", type = "p", cex =5)
text(c(1,2),c(2,1),c(2,1),col = "red")

However, when I attempt to use the dice font using the text funtion I get an error ...

font family not found in Windows font database

text(c(1,2),c(2,1),c(2,1),col = "red", family = "dice")

Questions:

  1. Is there a way to display the fonts accessible to R so I can determine if the dice font is available?
  2. If dice font is available then how do I get the font working with the text function?
  3. Is there an alternative or better way to approach this idea?
Markm0705
  • 1,340
  • 1
  • 13
  • 31

1 Answers1

1

After exploring the example suggested by Andrew Brēza I managed to get a result using ggplot2 - I could not find a solution for base R. With respect to the three questions I listed in the original post

  1. showtext has two functions to list the font families and fonts available to are respectively

    font_families() font_files()

I also found that I needed to install dice font using the windows right-click option 'install for all users' else the font file was installed under a user AppData/Local directory not the C:\Windows\Font directory

2.showtext does not appear to work with the base R text function even though the fonts are listed as available - I therefore explored ggplot

3 ggplot with showtext is the only working solution - script below produces the results I was looking for - I will just have to spend a bit of time making it look like a base R plot

# Plot numbers using dice font
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# https://stackoverflow.com/questions/34522732/changing-fonts-in-ggplot2

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
require(showtext)

# Note install the dice font using right-click install for all users
# Otherwise the font file is install in user profile nto C:\Windows\fonts

# Add the dice font to R
font_add(family = "dice",regular = "C:/Windows/Fonts/dice.ttf")

# Checks to show the dice font available in the fonts available to R

font_families()
font_files()

# Automatically use show text for newplots - do I need this?

showtext_auto()

# Load ggplot2
require(ggplot2)

# Create some data
df <- data.frame(rbind(c(1,2,2),c(2,1,4)))


# Plot the dice values
ggplot(df, aes(x = X1, y = X2)) +
        geom_point(size = 0.1, shape = 0) +
        annotate("text",df[1,1],df[1,2], label = df[1,3], family = "dice", size = 10) +
        annotate("text",df[2,1],df[2,2], label = df[2,3], family = "dice", size = 10) 
Markm0705
  • 1,340
  • 1
  • 13
  • 31