Fonts In Lattice package

In the lattice package, the fonts are determined by the base R graphics which depends on the operating system for functioning. They are generally used to provide high-level functions for creating various statistical graphs such as scatter plots, bar plots, histograms. To use the lattice package, we have to first load the lattice library. The below code explains how to create a simple lattice scatter plot using lattice package:

R
# Load the lattice package
library(lattice)

# Create a sample data frame
data <- data.frame(
  x = 1:5,
  y = c(2, 3, 4, 5, 6)
)

# Create a lattice plot with different font styles
xyplot(y ~ x, data = data,
       main = list(label = "Font Styles Example", fontfamily = "sans", 
                   fontface = "bold"),
       xlab = list(label = "X Axis", fontfamily = "serif", fontface = "italic"),
       ylab = list(label = "Y Axis", fontfamily = "mono", fontface = "bold"),
       scales = list(fontfamily = "fantasy", fontface = "plain")
)

Output:

Fonts In R

Installing and Adding a Google Font

Installing additional fonts allows more flexibility and visibility to the plot. To install additional fonts, first-of-all we have to use packages such as “extrafont” or “showtext”. These packages provide more fonts which can be used in the code for better understanding.

R
# Installing and loading fonts using extrafont
install.packages("extrafont")
library(extrafont)
font_import()

# install.packages("showtext")
library(showtext)

font_add_google(name = "Pacifico",   #fonts on the Google Fonts site
                family = "pacifico")
# Load the fonts for all graphic devices
showtext_auto()

boxplot(trees$Volume,
        main = "", xlab = "", ylab = "",
        ylim = c(10, 50),
        col = 3)

# Add the title, Y-axis label, and text with the custom font
title("Volume of Trees", family = "pacifico", cex.main = 2, col.main = "blue") 
title(ylab = "Volume (cu.ft)", family = "pacifico", cex.lab = 1.2, col.lab = "pink") 
text(x = 3, y = 85, "Outlier", family = "pacifico", cex = 1.2, col = "red") 

Output:

Fonts In R

The font_import() function is used to import fonts into R. This function scans system fonts and makes them available for use in R plots.

  • Add Custom Font: The font_add_google() function from the showtext package is used to add a custom font from the Google Fonts website. In this case, the “Pacifico” font is added with the family name “pacifico”.
  • Load Fonts Automatically: The showtext_auto() function from the showtext package is used to enable automatic loading of fonts for all graphic devices. This ensures that the custom font will be used in plots without explicitly specifying it each time.
  • Create Boxplot: A simple boxplot of tree volumes is created using the boxplot() function.
  • Add Custom Font to Text Elements: The title, Y-axis label, and custom text (“Outlier”) are added to the plot using the title() and text() functions. The custom font “Pacifico” is specified for these text elements using the family argument. Additionally, colors and sizes are adjusted for each text element.

Fonts In R

In data visualization and data representation, it becomes very important how we express our words and code with the help of different fonts. The text plays a very crucial role in conveying useful information and enhancing the aesthetic part of the plot. R Programming Language, being a powerful open-source programming language provides various fonts which can help to build visually appealing and professional-looking plots that are very useful to convey the message.

Similar Reads

Fonts In R

In R Programming Language the default font system can vary according to the graphic devices or plotting system. The graphic system sets the default font and can vary according to the platforms or the versions of R. However, some common graphic devices. The Base R Graphics uses the default fonts available on the system. The fonts depend on the operating system....

Base R Graphics Fonts

Here is a basic example of how to change fonts in base R graphics. In base R, you can set the font family using the family argument in text functions....

Fonts in ggplot2 package

ggplot2 is the most popular plotting packages in R. It does not have a default font, but it inherits the font settings from the base graphic system. Therefore, the ggplot2 plots aligns themselves according to the default font set for base R graphic on the operating system. The additional point of using ggplot2 is that it has many useful features to set text elements like title and text using ‘theme ()’ function....

Fonts In Lattice package

In the lattice package, the fonts are determined by the base R graphics which depends on the operating system for functioning. They are generally used to provide high-level functions for creating various statistical graphs such as scatter plots, bar plots, histograms. To use the lattice package, we have to first load the lattice library. The below code explains how to create a simple lattice scatter plot using lattice package:...

Conclusion

Using different fonts in R is very important as it helps in making the graphs and charts to be more attractive and easily understood by viewers. To provide a better and more appealing look to the graphics, one can also include custom fonts and can also use some packages like extrafont and showtext. Using either the default R graphics or ggplot2, it is always useful to be able to specify font type and usage as this will enhance the presentation and appearance of your visualizations no matter what format or device you are using....