How to Use the polygon() Function in R


Often you may want to draw a polygon in a plot in R based on specific locations for vertices.

One of the easiest ways to do so is by using the polygon() function in R, which can be used to perform this exact task.

The polygon() function uses the following syntax:

polygon(x, y, col=NA, border=NULL, …)

where:

  • x: Vector that contains x-coordinates of vertices of polygon
  • y: Vector that contains y-coordinates of vertices of polygon
  • col: Color of polygon
  • border: Border color of polygon

The following example shows how to use the polygon() function to draw a variety of different polygons on a plot in R.

Example: How to Use the polygon() Function in R

Suppose that we would like to create a polygon with the following vertices on a plot in R:

  • (8, 8)
  • (8, 10)
  • (10, 10)
  • (10, 8)

We can use the following syntax with the polygon() function to do so:

#create plot centered at (x, y) coordinates of (10, 10)
plot(10, 10, col='white')
 
#draw a polygon with specific vertices
polygon(x = c(8, 8, 10, 10),
        y = c(8, 10, 10, 8))

This produces the following result:

Notice that a polygon has been created with the exact set of vertices that we specified in the polygon() function.

In this case we created a square by specifying (x, y) coordinates that aligned with the shape of a square.

By default, the polygon() function does not produce any color for the polygon. However, you can use the col argument within the polygon() function to specify a color to use.

For example, we can use the following syntax to create the same polygon with a blue fill color:

#create plot centered at (x, y) coordinates of (10, 10)
plot(10, 10, col='white')
 
#draw a polygon with specific vertices
polygon(x = c(8, 8, 10, 10),
        y = c(8, 10, 10, 8),
        col = 'blue')

This produces the following result:

r polygon() function example

Notice that the same square is created and this time it is filled in with a blue color.

You can also use the border argument to specify a border color to use for the polygon.

For example, we can use the following syntax to create the same  blue square with a red border:

#create plot centered at (x, y) coordinates of (10, 10)
plot(10, 10, col='white')
 
#draw a polygon with specific vertices
polygon(x = c(8, 8, 10, 10),
        y = c(8, 10, 10, 8),
        col = 'blue',
        border = 'red',
        lwd = 6)

This produces the following result:

R polygon() function with fill and border colors

Notice that the same square is created and this time it is filled in with a blue color and has a red border color.

Note: We used the lwd argument to specify the line width of the border. Note that the larger the value supplied to the lwd argument, the thicker the border will be. We chose a value of 6 in this particular example.

Feel free to adjust the values for the col, border and lwd arguments of the polygon() function to create a polygon with the exact fill color, border color and line thickness that you would like.

Additional Resources

The following tutorials explain how to perform other common tasks in R:

How to Create a Frequency Table by Group in R
How to Create a Frequency Polygon in R
How to Create Relative Frequency Tables in R

Featured Posts

Leave a Reply

Your email address will not be published. Required fields are marked *