The “theme_bw” Function in R
Package: ggplot2
Purpose: To apply a black and white theme to ggplot objects, providing a clean and simple appearance.
General Class: Theme
Required Argument(s): None
Notable Optional Arguments:
base_size: The base font size for the plot.
base_family: The base font family for the plot.
base_line_size: The base line size for elements like axis lines.
base_rect_size: The base size for elements like rectangles.
base_rect_rel_size: The base size relative to the overall height of the plot for elements like rectangles.
base_rect_hjust: The base horizontal justification for elements like rectangles.
Example:
# Example usage
library(ggplot2)
# Run this code for special fonts using a Windows device.
# After the functions are run once, the extrafont...
# package only needs to be libraried for subsequent use.
library(extrafont)
font_import()
loadfonts(device = "win")
# Create a data frame
my_data <- data.frame(
x_values = seq(1, 10),
y_values = rnorm(10)
)
# Create a ggplot object with a black and white theme, remove base_family if Arial is unavailable.
my_plot <- ggplot(data = my_data, aes(x = x_values, y = y_values)) +
geom_point() +
theme_bw(base_size = 14, base_family = "Arial", base_line_size = 1)
# Print the plot
print(my_plot)In this example, the theme_bw function is used to create a ggplot object with a black and white theme. Optional arguments such as base_size and base_family are customized to control the font size and family, allowing users to tailor the appearance of the plot to their preferences.