Change the Size of the Plot Using the cex Parameter
The cex parameter is a numerical value that controls the size of plot elements. Its default value is 1, representing the standard size. Increasing cex makes elements larger while decreasing it makes them smaller.
1.Changing Text Size
In this example, we’ll create a scatter plot and change the size of the main title and axis labels using the cex parameter.
# Generate random data
set.seed(123) # Setting seed for reproducibility
x <- rnorm(100)
y <- rnorm(100)
# Create a scatter plot with larger title and axis labels
plot(x, y, main = "Changing Text Size", xlab = "X-axis", ylab = "Y-axis",
cex.main = 2.5, cex.lab = 2.0)
Output:
2. Adjusting Point Size
Here, we create another scatter plot and adjust the size of the points using the cex parameter.
# Generate random data
set.seed(123)
x <- rnorm(100)
y <- rnorm(100)
# Create a scatter plot with smaller points
plot(x, y, main = "Adjusting Point Size", xlab = "X-axis", ylab = "Y-axis", cex = 1.8)
Output:
3. Adding Text Annotation with Custom Size
This example demonstrates how to add a text annotation to a scatter plot and change its size using cex.
# Generate random data
set.seed(123)
x <- rnorm(100)
y <- rnorm(100)
# Create a scatter plot
plot(x, y, main = "Adding Text Annotation with Custom Size", xlab = "X-axis",
ylab = "Y-axis")
# Add text annotation with custom size
text(0, 0, "Annotation", cex = 3.2)
Output:
4. Changing Text Size in Bar Plot
In this example, we’ll create a bar plot and change the size of the bar labels using cex.
# Generate random data for bar plot
counts <- table(mtcars$cyl)
# Create a bar plot with larger bar labels
barplot(counts, main = "Changing Text Size in Bar Plot", xlab = "Number of Cylinders",
ylab = "Frequency", cex.names = 2.5)
Output:
5. Customizing Histogram Text Elements
We will create a histogram and adjust the sizes of the title, axis labels, and axis tick mark labels using cex.
# Generate random data
set.seed(123)
data <- rnorm(100)
# Create a histogram with customized text sizes
hist(data, main = "Customizing Histogram Text Elements", xlab = "Value",
ylab = "Frequency", cex.main = 1.5, cex.lab = 1.5, cex.axis = 1.5)
Output:
Multiple customization using Cex parameter
# Set the seed for reproducibility
set.seed(123)
# Generate random data
x <- rnorm(100)
y <- rnorm(100)
# Open a new graphics device (if needed)
dev.new()
# Create a customized scatter plot
plot(x, y,
main = "Multiple customization using Cex parameter", # Main title
xlab = "X-axis Label", # X-axis label
ylab = "Y-axis Label", # Y-axis label
cex.main = 2, # Increase main title size
cex.lab = 1.5, # Increase axis labels size
cex.axis = 1.2, # Increase axis tick labels size
cex = 1.3, # Increase point size
col = "orange", # Color of the points
pch = 16) # Point type
# Add custom text annotation
text(0, 0, "Center Point", cex = 1.5, col = "red")
# Add a legend with custom text size
legend("topright",
legend = c("Data Points", "Center Point"),
col = c("blue", "red"),
pch = c(16, NA),
pt.cex = c(1.3, NA),
text.col = c("black", "red"),
cex = 1.2)
# Additional customization for axis tick labels
axis(1, at = seq(-3, 3, by = 1), labels = seq(-3, 3, by = 1), cex.axis = 1.2)
axis(2, at = seq(-3, 3, by = 1), labels = seq(-3, 3, by = 1), cex.axis = 1.2)
# Adding grid lines for better readability
abline(h = seq(-3, 3, by = 1), v = seq(-3, 3, by = 1), col = "pink", lty = "dotted")
# Highlighting the zero lines for reference
abline(h = 0, v = 0, col = "green", lty = "solid", lwd = 2)
Output:
Cex Parameter in base R
Data visualization is a crucial aspect of data analysis and interpretation. In R, base graphics provide a powerful framework for creating various types of plots. One key aspect of customizing plots is controlling the size of plot elements such as text, points, and lines. The cex parameter in base R plays a significant role in adjusting these plot elements, offering flexibility and customization options to users.
Contact Us