Adding Points with the points() Function
Basic Usage
The points() function adds points to an existing plot created with plot(). The basic syntax is:
points(x, y, col = "black", pch = 1, cex = 1)
- x: Numeric vector of x-coordinates.
- y: Numeric vector of y-coordinates.
- col: Color of the points.
- pch: Plotting character (symbol) to use.
- cex: Character expansion factor (size of the points).
Example:
# Create a base plot x <- 1:10 y <- x^2 plot(x, y, main = "Adding Points Example", xlab = "X", ylab = "Y") # Add additional points new_x <- c(2.5, 4.5, 6.5) new_y <- c(8, 20, 40) points(new_x, new_y, col = "red", pch = 17, cex = 2)
- plot(): Creates the initial scatter plot.
- points(): Adds red, triangular points to the existing plot.
Customizing Point Appearance
You can customize the appearance of points using various parameters.
- col: Defines the color of the points. You can use named colors or RGB values.
- pch: Defines the symbol used for the points. Common symbols are:
- 1: Circle
- 2: Triangle
- 3: Plus sign
- 4: Cross
- 16: Filled circle
- cex: Adjusts the size of the points. The default is 1; values greater than 1 increase the size.
Example:
# Create a base plot plot(x, y, main = "Customized Points", xlab = "X", ylab = "Y") # Add customized points points(new_x, new_y, col = "blue", pch = 19, cex = 1.5)
- pch = 19: Adds filled circles.
- cex = 1.5: Increases the point size.
Adding Points with Different Attributes
You can add points with different colors, sizes, and symbols to highlight specific data.
Example:
# Create a base plot plot(x, y, main = "Different Points Attributes", xlab = "X", ylab = "Y") # Add points with different attributes points(c(1, 2, 3), c(1, 4, 9), col = "green", pch = 15, cex = 2) points(c(7, 8, 9), c(49, 64, 81), col = "purple", pch = 17, cex = 1.2)
- pch = 15: Adds filled squares.
- pch = 17: Adds filled triangles.
- cex: Adjusts the size for each set of points.
Adding Points to Existing Plot
To add points to a plot after it has been created, ensure you use the points() function on the existing plot.
Example:
# Create the initial plot plot(x, y, main = "Adding Points to Existing Plot", xlab = "X", ylab = "Y") # Add points to the existing plot additional_x <- c(5, 7, 9) additional_y <- c(25, 49, 81) points(additional_x, additional_y, col = "orange", pch = 8, cex = 1.8)
- pch = 8: Adds asterisk symbols.
Adding Points with Transparency
To make points semi-transparent, you can use RGB colors with transparency.
Example:
# Create the initial plot plot(x, y, main = "Transparent Points", xlab = "X", ylab = "Y") # Add semi-transparent points points(new_x, new_y, col = rgb(1, 0, 0, 0.5), pch = 16, cex = 2) # Red with 50% transparency
- rgb(r, g, b, alpha): Defines color with transparency (alpha).
Summary
- Basic Usage: Use points() to add points to an existing plot.
- Customization: Modify col, pch, and cex to change appearance.
- Different Attributes: Add points with various colors, sizes, and symbols.
- Adding to Existing Plot: Ensure you add points after the plot is created.
- Transparency: Use rgb() with the alpha parameter for semi-transparent points.
Post Views: 79