*59*

A **frequency polygon** is a type of chart that helps you visualize the distribution of values in a dataset.

You can use the following syntax to create a frequency polygon using the ggplot2 data visualization package in R:

library(ggplot2) ggplot(df, aes(value)) + geom_freqpoly()

The following examples show how to use this syntax in practice.

**Example 1: Basic Frequency Polygon**

The following code shows how to create a basic frequency polygon for a dataset:

library(ggplot2) #make this example reproducible set.seed(0) #create data frame df frame(index=1:100, value=rnorm(100, mean=50, sd=10)) #create frequency polygon ggplot(df, aes(value)) + geom_freqpoly()

**Example 2: Frequency Polygon with Custom Bins**

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. For example, the following code creates a frequency polygon using **10** bins:

library(ggplot2) #make this example reproducible set.seed(0) #create data frame df frame(index=1:100, value=rnorm(100, mean=50, sd=10)) #create frequency polygon ggplot(df, aes(value)) + geom_freqpoly(bins=10)

**Example 3: Frequency Polygon with Fill Color**

If youâ€™d like to fill in the frequency polygon with a certain color, youâ€™ll need to instead use the **geom_area()** function as follows:

library(ggplot2) #make this example reproducible set.seed(0) #create data frame df frame(index=1:100, value=rnorm(100, mean=50, sd=10)) #create frequency polygon filled with custom color ggplot(df, aes(value)) + geom_area(aes(y=..count..), bins=10, stat='bin', fill='steelblue')

**Additional Resources**

How to Create a Frequency Table by Group in R

How to Create Relative Frequency Tables in R

How to Create a Relative Frequency Histogram in R