Draw a Polygon between specified points in R Programming – polygon() Function
polygon() function in R Language is used to plot a polygon between specified points in an existing plot.
Syntax: polygon(x_coordinates, y_coordinates) Parameters : x_coordinates, y_coordinates: x, y coordinates of plot to draw polygon Returns: a polygon in a given plot
Example 1: Draw a Square Polygon in an R Plot
r
plot (2, 2, col = "white", xlab = "X", ylab = "Y")
polygon (x = c (2.7, 2.3, 2.2, 2.8),
y = c (2.6, 2.8, 2.4, 2),
col = "darkgreen")
|
Output: Example 2: Color borders of polygon
r
plot (2, 2, col = "white", xlab = "X", ylab = "Y")
polygon (x = c (2.7, 2.3, 2.2, 2.8),
y = c (2.6, 2.8, 2.4, 2),
col = "darkgreen",
border = "red",
lwd = 8)
|
Output:
Here, border specifies the border color and lwd specifies the border thickness.
Example 3: Draw frequency polygon
r
x1 <- 1:10
y1 <- c (2, 4, 7, 4, 5, 8, 6, 6, 1, 2)
plot (x1, y1,
type = "l",
lwd = 4)
polygon ( c (1, x1, 10), c (0, y1, 0),
col = "darkgreen")
points (x1, y1,
cex = 1,
pch = 12)
segments (x1, 0, x1, y1)
|
Output: Example 4: Draw polygon below density
r
set.seed (15000)
N <- 1000
x1 <- rpois (N, 2)
plot ( density (x1),
main = "",
xlab = "x1")
polygon ( c ( min ( density (x1)$x), density (x1)$x),
c (0, density (x1)$y),
col = "darkgreen")
|
Output:
Here, above example is used to make Probability density function.
Example :
R
x <- c (1, 3, 4, 2)
y <- c (1, 2, 4, 3)
plot (x, y, type = "n" )
polygon (x, y, col = "blue" )
|
output :
Last Updated :
11 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...