Visualizing Data in R with Default Package:
Modified Plot

Asst. Prof. Dr. Somsak Chanaim

International College of Digital Innovation, CMU

June 24, 2026

Examples

Modifying a Plot

Modifying a plot in the base R package can be done using various parameters within the plot() function, as well as adding additional elements using other base plotting functions like lines(), points(), abline(), text(), legend(), etc.

Here’s a guide on how to modify different aspects of a plot in base R:

1. Modifying Axes

  • Custom Axis Labels: Use xlab and ylab to modify the axis labels.

  • Custom Axis Limits: Use xlim and ylim to set the limits of the x and y axes.

  • Custom Axis Ticks: Use axis() to customize the ticks and labels.

2. Modifying Points

  • Point Shape: Use pch to change the shape of the points.

  • Point Size: Use cex to change the size of the points.

  • Point Color: Use col to change the color of the points.

Change the Shape of the Points (pch)

Note: The pch = 0 is useless, then we will not consider on this value.

The pch = 15 to 20 are useful for the argument col.

3. Adding Titles and Subtitles

  • Main Title: Use main to add a main title.

  • Subtitle: Use sub to add a subtitle.

4. Adding a Grid

Adding Grid Lines: Use grid() to add grid lines to the plot.

argument

  • nx, ny number of cells of the grid in x and y direction (try: nx = NA, ny = NULL or nx = NULL, ny = NA)

  • col color of grid lines.

  • lty line type of the grid lines. (1-6)

  • lwd line width of the grid lines

5. Adding Lines

  • Adding a Line through Points: Use lines() to add a line connecting points.

  • Adding a Regression Line: Use abline() to add a regression line.

6. Adding Text Annotations

  • Adding Text: Use text() to add text annotations to specific points.

  • Positioning Text: Use pos, adj, and offset to control the position of the text.

7. Adding Legends

  • Adding a Legend: Use legend() to add a legend to the plot.

8. Customizing Plot Margins

Custom Margins: Use par(mar = c(bottom, left, top, right)) to adjust the margins.

9. Adding Custom Axes

Suppress Default Axes: Use axes = FALSE in plot() and add custom axes with axis().

10. Modified the background color

We can modify the background color of a base R plot using the bg parameter within the par() function.

Explanation:

  • par(bg = "lightblue"): This sets the background color of the plot to light blue.

  • par(bg = "white"): This resets the background color to white for subsequent plots, ensuring that only the current plot is affected.

You can replace “lightblue” with any valid color name or hexadecimal color code (e.g., “#FFD700” for lightblue) to customize the background color as desired.

  • If we want a transparent background color, we can set bg = NULL.

11. Changing the Background Color of the Plot Area

In a base R plot, you can use the rect() function after creating the plot.

Explanation:

  • plot(..., type = "n"): Creates an empty plot with the axes and labels but without plotting the data points.

  • rect(...): Draws a rectangle that covers the entire plot area using the coordinates obtained from par("usr"). The col parameter sets the background color, and border = NA removes the border around the rectangle.

  • points(): Adds the data points on top of the custom background.

12. Changing the Background Color of the Plot Area and Adding Grid Lines

13.1 Multiple Plots in a Row Layout (Using mfrow)

The mfrow parameter arranges plots in a row-wise manner.

13.2 Multiple Plots in a Column Layout (Using mfcol)

The mfcol parameter arranges plots in a column-wise manner.

13.3 Custom Layouts Using layout()

For more complex layouts, you can use the layout() function, which allows for custom arrangements, including varying plot sizes.

Exercise

Exercise 1: Point Color and Symbol Customization

Complete the script to configure a bivariate scatter plot, assigning a specific aesthetic color property and custom plotting symbol layers.

Target output

Complete the code

plot(x = mtcars$wt, y = mtcars$mpg,
= "red",
= 17,
main = "Weight vs MPG",
xlab = "Weight",
ylab = "Miles Per Gallon")

Exercise 2: Annotation and Axis Label Strategy

Complete the script to generate a bivariate scatter plot and apply distinct text strings to structural title and axis label parameters.

Target output

Complete the code

plot(x = mtcars$disp, y = mtcars$hp,
= "Displacement vs Horsepower",
= "Displacement (cu. in.)",
= "Horsepower (hp)")

Exercise 3: Canvas Layering and Background Customization

Complete the multi-stage rendering script to initialize an empty canvas, paint the bounded graphical viewport region, and overlay coordinate observations.

Target output

Complete the code

plot(x = mtcars$wt, y = mtcars$mpg,
= "n",
main = "Weight vs MPG",
xlab = "Weight",
ylab = "Miles Per Gallon")

(par("usr")[1], par("usr")[3], par("usr")[2], par("usr")[4],
col = "lightblue",
border = NA)

(x = mtcars$wt, y = mtcars$mpg, pch = 19)

Exercise 4: Bivariate Mapping and Grid Alignment

Complete the script to construct a bivariate coordinate space and layer structural alignment grids onto the active viewport canvas.

Target output

Complete the code

plot(x = mtcars$qsec, y = mtcars$drat,
main = "1/4 Mile Time vs Rear Axle Ratio",
xlab = "1/4 Mile Time (seconds)",
ylab = "Rear Axle Ratio")

(col = "gray", = "dotted")

Exercise 5: Advanced Axis Suppression and Custom Tick Mapping

Complete the script to suppress the default horizontal and vertical coordinate axes, instantiate tailored scaling boundaries, and layer the observation nodes.

Target output

Complete the code

plot(x = mtcars$wt, y = mtcars$mpg,
= "n",
= "n",
main = "Weight vs MPG",
xlab = "Weight",
ylab = "Miles Per Gallon")

(1, at = seq(from = 1, to = 6, by = 1),
labels = paste(seq(from = 1, to = 6, by = 1), "tons"))

(2, at = seq(from = 10, to = 35, by = 5),
labels = paste(seq(from = 10, to = 35, by = 5), "MPG"))

points(x = mtcars$wt, y = mtcars$mpg, pch = 19)

Exercise 6: Graphic Parameter Matrices and Subplot Layouts

Complete the script to split the structural graphical window into a multi-panel layout matrix and execute sequential plotting commands.

Target output

Complete the code

par( = c(2, 2))
plot(x = mtcars$wt, y = mtcars$mpg, main = "Weight vs MPG")
plot(x = mtcars$disp, y = mtcars$hp, main = "Displacement vs Horsepower")
plot(x = mtcars$qsec, y = mtcars$drat, main = "1/4 Mile Time vs Rear Axle Ratio")
plot(x = mtcars$wt, y = mtcars$hp, main = "Weight vs Horsepower")

Exercise 7: Advanced Matrix-Based Layout Segmentation

Complete the script to split the graphical viewport using an asymmetric matrix schema and arrange subplots with custom height and width constraints.

Target output

Complete the code

(matrix(c(1, 2, 3, 3), nrow = 2, = TRUE), widths = c(1, 2), heights = c(1, 2))
plot(x = mtcars$wt, y = mtcars$mpg, main = "Weight vs MPG")
plot(x = mtcars$disp, y = mtcars$hp, main = "Displacement vs Horsepower")
plot(x = mtcars$qsec, y = mtcars$drat, main = "1/4 Mile Time vs Rear Axle Ratio")

Exercise 8: Categorical Mapping and Legend Configurations

Complete the script to evaluate transmission groupings, map dynamic vectors to aesthetic point colors, and deploy an explanatory legend map.

Target output

Complete the code

colors <- (
mtcars$am == 0, "red", "blue"
)
plot(x = mtcars$wt, y = mtcars$mpg,
col = colors,
pch = 19,
main = "Weight vs MPG by Transmission",
xlab = "Weight",
ylab = "Miles Per Gallon")
("topright",
legend = c("Automatic", "Manual"),
col = c("red", "blue"),
pch = 19)

Exercise 9: Maximum Value Extraction and Text Annotation

Complete the script to pinpoint the structural maximum outlier and overlay an explicit textual callout annotation onto the target coordinate.

Target output

Complete the code

plot(x = mtcars$wt, y = mtcars$mpg,
pch = 19,
main = "Weight vs MPG",
xlab = "Weight",
ylab = "Miles Per Gallon")
max_mpg <- (
mtcars$mpg
)
(x = mtcars$wt[max_mpg], y = mtcars$mpg[max_mpg],
labels = "Highest MPG",
pos = 4,
col = "red")