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:
# 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:
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.
# 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:
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()
andtext()
functions. The custom font “Pacifico” is specified for these text elements using thefamily
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.