Changing Character Sizes with cex
Understanding cex
The cex parameter is a scaling factor that multiplies the default size of text or symbols.
- cex Value:
- cex = 1: Represents the default size.
- cex < 1: Reduces the size below the default.
- cex > 1: Increases the size above the default.
Changing Text Sizes
You can use cex to adjust the size of text in various plot elements, such as titles, axis labels, and annotation text.
Example:
# Create a basic plot with default text size plot(1:10, 1:10, main = "Default Text Size", xlab = "X Axis", ylab = "Y Axis") # Add text with increased size text(x = 5, y = 5, labels = "Big Text", cex = 2, col = "red")
- cex = 2: Increases the size of the annotation text to twice the default size.
Example with Axis Labels:
# Create a plot with increased axis label size plot(1:10, 1:10, main = "Increased Axis Label Size", xlab = "X Axis", ylab = "Y Axis", cex.lab = 1.5)
- cex.lab = 1.5: Increases the size of axis labels by 50%.
Changing Symbol Sizes
You can use cex to adjust the size of plotting symbols (e.g., points) in the plot.
Example:
# Plot points with default size plot(1:10, 1:10, main = "Default Point Size") # Plot points with increased size plot(1:10, 1:10, main = "Increased Point Size", cex = 2)
- cex = 2: Increases the size of the points to twice the default size.
Changing Text Sizes in Legend
You can use cex to adjust the size of text within a legend.
Example:
# Create a plot with a legend plot(1:10, 1:10, main = "Legend Text Size", pch = 16) # Add a legend with increased text size legend("topright", legend = c("Data Points"), pch = 16, cex = 1.5)
- cex = 1.5: Increases the size of the legend text by 50%.
Using cex with Multiple Parameters
You can use cex with multiple parameters to control different aspects of text and symbols in the plot.
Example:
# Create a plot with varying sizes for title, axis labels, and points plot(1:10, 1:10, main = "Custom Sizes", xlab = "X Axis", ylab = "Y Axis", cex.main = 2, cex.lab = 1.5, cex.axis = 1.2, cex = 1.5) # Add text with a different size text(5, 5, "Custom Size", cex = 2)
- cex.main = 2: Increases the size of the main title.
- cex.lab = 1.5: Increases the size of the axis labels.
- cex.axis = 1.2: Increases the size of the axis numbers.
- cex = 1.5: Increases the size of the plotting symbols.
Combining cex with Other Plotting Parameters
You can combine cex with other graphical parameters to achieve more customized plots.
Example:
# Create a plot with customized text size, symbols, and colors plot(1:10, 1:10, main = "Customized Plot", xlab = "X Axis", ylab = "Y Axis", cex.main = 2, cex.lab = 1.5, cex.axis = 1.2, cex = 1.5, pch = 19, col = "blue") # Add text with different size and color text(5, 5, "Important Point", cex = 2, col = "red")
- pch = 19: Uses filled circles for points.
- col = “blue”: Sets the color of the points.
- text(): Adds annotation with increased size and color.
Summary
- cex Parameter: Controls the size of text and symbols.
- cex < 1: Decreases the size.
- cex > 1: Increases the size.
- Change Text Sizes: Use cex to adjust the size of titles, axis labels, and annotation text.
- Change Symbol Sizes: Use cex to adjust the size of plot symbols.
- Change Legend Sizes: Use cex to adjust the size of text in the legend.
- Use cex with Multiple Parameters: Control different sizes for titles, labels, axes, and symbols at once.
- Combine cex with Other Parameters: Customize your charts further by combining cex with other chart options.