Today, we will learn how to create a frequency plot in the R programming language. And it is used a lot in data analysis industry. So, follow this article to know to get more information.

**What is the frequency plot in R?**

As a frequency plot bins the count of continuous data, it is comparable to a histogram in appearance. But a line plot will be used in place of bars for the display. We will study how to make frequency graphs in R in this article.

**How to plot a frequency plot in R?**

**Load a package**

First, we will install and load ggplot2 packages to the plot to plot a frequency plot in R. If you don’t know how to load this package, you can click this link **here** or run the code below:

```
# Install the package
install.packages("ggplot2")
# Load the package
library(ggplot2)
```

**Create a data**

Then we load a ggplot2 package, and we will create data to plot a frequency plot as follows:

library(ggplot2) # Create a data set.seed(255) x <- sample(1:200, 50, replace = TRUE) y <- sample(1:200, 50, replace = TRUE) z <- sample(1:200, 50, replace = TRUE) # Create a dataframe df <- data.frame(x, y, z) # View a data frame head(df)

Output

```
x y z
1 56 93 102
2 150 37 47
3 31 32 110
4 5 174 93
5 181 47 91
6 171 188 199
```

**Plot a frequency plot **

Now, we will use the ggplot() function and geom_freqpoly() function in the ggplot2 package to plot a frequency plot in R. Check out the code example below.

library(ggplot2) # Create a data set.seed(255) x <- sample(1:200, 50, replace = TRUE) y <- sample(1:200, 50, replace = TRUE) z <- sample(1:200, 50, replace = TRUE) # Create a dataframe df <- data.frame(x, y, z) # Plot a frequency plot ggplot(diamonds, aes(x = x)) + geom_freqpoly()

Output

Here, bins variables will be automatically chosen by Frequency Plot, which will then plot the proportion of observations that fall into each bin. You can supply the bins parameter to the geom if you want to modify the bin width.

library(ggplot2) # Create a data set.seed(255) x <- sample(1:200, 50, replace = TRUE) y <- sample(1:200, 50, replace = TRUE) z <- sample(1:200, 50, replace = TRUE) # Create a dataframe df <- data.frame(x, y, z) # Plot a frequency plot ggplot(diamonds, aes(x = x)) + geom_freqpoly(bins = 5)

Output

**Customize a frequency plot**

Here, we will use color, linetype, lwd, … arguments in a geom_frepoly() function to customize a frequency plot as follows:

library(ggplot2) # Plot a frequency plot ggplot(diamonds, aes(x = x)) + geom_freqpoly( bins = 5, color = 4, lwd = 2, linetype = 4 )

Output

**Customize the label on a frequency plot **

We can use the labs() function in R to customize the label on a frequency plot. Look at the code example below:

library(ggplot2) ggplot(diamonds, aes(x = x)) + geom_freqpoly( bins = 5, color = 4, lwd = 2, linetype = 4 ) + labs( title = "The Frequency Plot", x = "X-axis", y = "Y-axis" )

Output

**Summary**

In conclusion, this article shared with us how to create the frequency plot in R. So, we hope you understand how to use it intensely and apply it in your field. Thank you for reading this post, and please comment if you have any questions.

Have a great day!

Hi, guys! My name’s Scott Miller. My current job is a software developer and I have shared a lot of quality articles about Javascript, C, C++, C#, Python, PHP, R, Java programming languages. I’m hoping they can assist you.

**Name of the university: **HCMUS

**Major**: IT

**Programming Languages**: C, C++, Python, R, Java, JavaScript