Adding a Polygon with the polygon() Function in R
Adding a Polygon with the polygon() Function Syntax of the polygon() Function The polygon() function draws a polygon defined by a series of points. Its basic syntax is: polygon(x, y, border = NULL, col = NULL, lty = 1, lwd = 1) x: Numeric vector of x-coordinates for the vertices of the polygon. y: Numeric vector of y-coordinates for the vertices of the polygon. border: Color of the polygon border. (Default is NULL, meaning no border.) col: Color of the polygon fill. (Default is NULL, meaning no fill.) lty: Line type for the border. (Default is 1, solid line.) lwd: Line width of the border. (Default is 1.) Basic Example: Drawing a Simple Polygon Here’s how to create a basic polygon: # Generate example data x <- c(2, 4, 6, 4) y <- c(2, 4, 2, 0) # Create a basic plot plot(1:10, 1:10, type = “n”, main = “Basic Polygon Example”) # Add a polygon polygon(x, y, border = “blue”, col = “lightblue”) x: x-coordinates of the polygon vertices. y: y-coordinates of the polygon vertices. border = “blue”: Border color of the polygon. col = “lightblue”: Fill color of the polygon. Creating a Complex Polygon You can draw polygons with more vertices for complex shapes. Example: # Generate coordinates for a complex polygon x <- c(1, 3, 5, 7, 6, 4) y <- c(1, 2, 1, 4, 6, 4) # Create a basic plot plot(1:10, 1:10, type = “n”, main = “Complex Polygon”) # Add a complex polygon polygon(x, y, border = “red”, col = “yellow”) x and y: Coordinates for the vertices of the polygon. border = “red”: Border color of the polygon. col = “yellow”: Fill color of the polygon. Highlighting Areas Polygons can be used to highlight specific areas on the plot. Example: # Generate example data x <- c(2, 4, 6, 4) y <- c(2, 4, 2, 0) # Create a basic plot plot(1:10, 1:10, type = “n”, main = “Highlighted Area”) # Add a polygon to highlight an area polygon(x, y, border = “blue”, col = rgb(0.5, 0.5, 1, 0.5)) rgb(0.5, 0.5, 1, 0.5): Defines a semi-transparent blue color for the polygon fill. Adding Polygons to Existing Plots You can add polygons to an existing plot using the polygon() function. Example: # Generate example data x <- 1:10 y <- x^2 # Create a plot with the data plot(x, y, main = “Polygon on Existing Plot”) # Add a polygon polygon(c(3, 5, 7, 5), c(9, 25, 49, 16), border = “green”, col = “lightgreen”) c(3, 5, 7, 5): x-coordinates for the polygon vertices. c(9, 25, 49, 16): y-coordinates for the polygon vertices. Using Polygons for Area Plots Polygons can be used to create area plots or density plots. Example: # Generate density data x <- seq(0, 10, length.out = 100) y1 <- dnorm(x, mean = 5, sd = 1) y2 <- dnorm(x, mean = 7, sd = 1.5) # Create a density plot plot(x, y1, type = “l”, col = “blue”, ylim = c(0, max(y1, y2)), main = “Area Plot”) # Add density areas with polygons polygon(c(x, rev(x)), c(y1, rep(0, length(x))), col = rgb(0.5, 0.5, 1, 0.5), border = NA) polygon(c(x, rev(x)), c(y2, rep(0, length(x))), col = rgb(1, 0.5, 0.5, 0.5), border = NA) c(x, rev(x)): x-coordinates for the polygon, doubled to close the shape. c(y1, rep(0, length(x))): y-coordinates for the polygon, with a line to 0 to close the shape. rgb(0.5, 0.5, 1, 0.5) and rgb(1, 0.5, 0.5, 0.5): Semi-transparent colors for the polygon fill. Summary polygon() Function: Draws polygons on a plot. x: x-coordinates of the vertices. y: y-coordinates of the vertices. border: Border color. col: Fill color. lty: Line type for the border. lwd: Line width of the border. Basic Example: Drawing a simple polygon. Complex Polygons: Creating polygons with more vertices. Highlighting Areas: Using polygons to emphasize regions. Adding to Existing Plots: Incorporating polygons into existing plots. Area Plots: Creating area plots or density plots with polygons.
Adding a Polygon with the polygon() Function in R Lire la suite »