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.

R
# 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:

Change the Size of the Plot Using the cex Parameter

2. Adjusting Point Size

Here, we create another scatter plot and adjust the size of the points using the cex parameter.

R
# 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:

Change the Size of the Plot Using the cex Parameter

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.

R
# 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:

Cex parameter in base R

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.

R
# 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:

Cex parameter in base R

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.

R
# 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:

Cex parameter in base R

Multiple customization using Cex parameter

R
# 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

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.

Similar Reads

Cex parameter in base R

In R Programming Language the cex parameter is used to specify the magnification factor for text size relative to the default size. It stands for “character expansion.” This parameter is commonly used in plotting functions to adjust the size of text elements such as labels, axis titles, and annotations....

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....

Conclusion

The cex parameter in base R is an essential tool for customizing plot elements, allowing the adjustment of text, point, and annotation sizes. Mastering cex enhances the readability and visual appeal of plots, making data visualizations more effective and impactful. Whether emphasizing key information, improving visibility, or personalizing plots, cex provides the flexibility needed to create clear and informative graphical representations of data....

Cex parameter in base R-FAQs

What does cex stand for in R?...

Contact Us