# How to Create an Ogive Graph in R

An ogive (or cumulative frequency graph) is a graph used in statistics to illustrate cumulative frequencies, which are measured by adding each frequency from a frequency distribution table to the sum of its predecessors. The result is a graph that represents the cumulative frequency of data in ordered classes, and it is most useful when you want to visualize the total frequency up to a given level of a factor variable.

In this guide, we will walk you through the step-by-step process of creating an ogive graph in R, from preparing your data to fine-tuning your final graph for presentation. We’ll be using both base R functions and the popular ggplot2 package, which provides an aesthetically pleasing and versatile system for creating graphics in R.

### Importing Libraries

First, let’s make sure we have the ggplot2 package loaded into our R environment.

# Loading the ggplot2 package
library(ggplot2)

### Creating a Dataset

We’ll use a simple dataset for this tutorial: a list of ages of 50 individuals.

# Create a vector of ages
ages <- c(23, 25, 21, 22, 27, 28, 24, 23, 22, 21, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
23, 24, 25, 26, 27, 24, 23, 22, 23, 24, 28, 27, 26, 29, 31, 32, 34, 35, 36, 37,
23, 25, 27, 29, 30, 31, 33, 32, 34, 35, 36)

## Creating an Ogive Graph in Base R

To create an ogive graph, we first need to calculate the cumulative frequencies. In base R, we can use the cumsum() function to calculate the cumulative sum of the frequencies, which we obtain using the table() function.

Here’s how we can create an ogive graph in base R:

# Create a frequency distribution table
freq <- table(ages)

# Calculate cumulative frequencies
cum_freq <- cumsum(freq)

# Create an ogive graph
plot(names(cum_freq), cum_freq, type = "b", xlab = "Age", ylab = "Cumulative Frequency", main = "Ogive Graph of Ages")

In the above code, the plot() function is used to create the ogive graph. The names(cum_freq) provides the x-axis (age), and cum_freq provides the y-axis (cumulative frequency). The type = "b" argument means that both points (denoted by “p”) and lines (denoted by “l”) are plotted.

## Creating an Ogive Graph Using ggplot2

While the base R plot function can produce the necessary graph, the ggplot2 package offers more control over the aesthetics of the plot and is generally preferred for its ability to create more sophisticated graphics.

Here’s how you can create an ogive graph using ggplot2:

# Create a data frame of ages and their cumulative frequencies
df <- data.frame(age = as.numeric(names(cum_freq)), cum_freq = cum_freq)

# Create an ogive graph with ggplot2
ggplot(df, aes(x = age, y = cum_freq)) +
geom_point() +
geom_line() +
labs(title = "Ogive Graph of Ages", x = "Age", y = "Cumulative Frequency")

In the above code, we first create a data frame of ages and their cumulative frequencies. Then, we use the ggplot() function to initiate the plot, aes() to map the aesthetics, geom_point() to create the points, geom_line() to create the lines, and labs() to add the title and labels.

## Enhancing the Ogive Graph

ggplot2 allows you to customize nearly every aspect of your graph. Let’s modify our graph to add color, adjust point shapes and sizes, and change the theme.

# Create an enhanced ogive graph with ggplot2
ggplot(df, aes(x = age, y = cum_freq)) +
geom_point(color = "blue", shape = 19, size = 3) +
geom_line(color = "red", size = 1.2) +
labs(title = "Ogive Graph of Ages", x = "Age", y = "Cumulative Frequency") +
theme_minimal()

In the above code, we added color to the points and lines using the color argument and adjusted their shapes and sizes using the shape and size arguments. We also changed the theme of the graph to a minimal theme using theme_minimal().

## Conclusion

In this guide, we explored how to create an ogive graph in R using both base R functions and the ggplot2 package. We also learned how to enhance the graph by adjusting colors, point shapes and sizes, and the theme.

Posted in RTagged