Open In App

How to Create a Frequency Polygon in R?

Last Updated : 19 Dec, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will discuss how to create a Frequency Polygon in the R Programming Language.

Frequency polygons are the plots of the values in a data frame to visualize the shape of the distribution of the values. It helps us in comparing different data frames and visualizing the cumulative frequency distribution of the data frames. The frequency polygon indicates the number of occurrences for each distinct class in the data frame. 

Create Frequency Polygon in Base R:

To create a basic frequency polygon in the R Language, we first create a line plot for the variables under construction. Then we use the polygon() function to create the frequency polygon.

Syntax: plot( x, y ) polygon( c( xmin, x, xmax ), c( ymin, y, ymax ), col )

where,

  • x and y: determines the data vector for x and y axes data.
  • xmin and ymin: determines the minimum limit of x and y axis.
  • xmax and ymax: determines the maximum limit of x and y axis.
  • col: determines the color of frequency polygon.

Example:

Here, is a basic frequency polygon made in Base R by using the polygon() function.

R




set.seed(999)
x<-1:40
  
y<-sample(5:40,40,replace=TRUE)
plot(x,y,type="l"
polygon(c(1,x,40),c(0,y,0),col="green")


Output:

Create Frequency Polygon using ggplot2 :

To create a basic frequency polygon in the R Language using the ggplot2 package, we use the geom_freqpoly() function. By default, ggplot2 uses 30 bins to create the frequency polygon. By reducing the number of bins, you can make the lines on the plot smoother.

Syntax: ggplot( df, aes(value)) +  geom_freqpoly( bins )

where,

  • df: determines the data frame to be visualized.
  • value: determines the y-axis column name.
  • bins: determines the smoothness of the plot.

Example:

Here, is a basic frequency polygon made using the ggplot2 package. 

R




library(ggplot2)
  
# make this example reproducible
set.seed(0)
  
# create data frame
index<-1:40
y<-sample(5:40,40,replace=TRUE)
df <- data.frame( index, y )
  
# create frequency polygon using geom_freqpoly()
# function
ggplot(df, aes(y)) + 
  geom_freqpoly(bins=10)


Output:

Frequency Polygon with Fill Color using ggplot2 package:

To create a basic frequency polygon with fill color in the R Language using the ggplot2 package, we use the geom_area() function. we use the fill parameter of the geom_area() function to fill the frequency polygon with the desired color.

Syntax: ggplot( df, aes(value)) +  geom_area(aes(y=..count..), bins, fill )

where,

  • df: determines the data frame to be visualized.
  • value: determines the y-axis column name.
  • bins: determines the smoothness of the plot.
  • fill: determines the fill color of the plot:

Example:

Here, is a basic frequency polygon made using the ggplot2 package and colored using the geom_area() function.

R




library(ggplot2)
  
# make this example reproducible
set.seed(0)
  
# create data frame
index<-1:40
y<-sample(5:40,40,replace=TRUE)
df <- data.frame( index, y )
  
# create frequency polygon filled with custom color
ggplot(df, aes(y)) + 
  geom_area(aes(y=..count..), bins=10, stat='bin', fill='green')


Output:



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads