To adjust the font size of text and axes on R plots, you can use various functions and parameters within the base R graphics system or through packages like ggplot2. Here's how you can do it using both methods:
Base R Graphics:
You can use the par() function to set graphical parameters, including font sizes. Here's an example:
# Set up example data
x <- 1:10
y <- 1:10
# Create a plotplot(x, y, main = "Scatter Plot", xlab = "X-axis", ylab = "Y-axis")# Adjust font size of title, x-axis label, and y-axis labelpar(cex.main = 2, cex.lab = 1.5)
In this example, cex.main controls the size of the main title, and cex.lab controls the size of the axis labels.
ggplot2:
With ggplot2, you can adjust font sizes using the theme() function. Here's an example:
library(ggplot2)# Set up example datadf <- data.frame(x = 1:10, y = 1:10)# Create a ggplot objectp <- ggplot(df, aes(x, y)) + geom_point() + labs(title = "Scatter Plot", x = "X-axis", y = "Y-axis")# Adjust font size of title, x-axis label, and y-axis labelp + theme( plot.title = element_text(size = 16), axis.title.x = element_text(size = 14), axis.title.y = element_text(size = 14))
In this example, size within element_text() controls the font size of the plot title and axis labels.
Feel free to adjust the font size values (cex.main, cex.lab, size) according to your preferences. These are just examples to demonstrate how to change font sizes on R plots using both base R graphics and ggplot2.