Create an area graph with R

Create an area graph with R: Area graphs are a great way to visualize data over time, especially when you want to see how different data sets contribute to an overall trend. In this tutorial, we will be using R programming to create an area graph using the ggplot2 library.

Create an area graph with R
Create an area graph with R

First, we need to install and load the ggplot2 library:

install.packages("ggplot2")
library(ggplot2)

Next, we need some data to work with. We will be using the built-in economics data set that comes with R, which contains data on the US economy from 1967 to 2015:

data(economics)

To create an area graph with ggplot2, we first need to prepare the data by converting it from a wide format to a long format using the gather function from the tidyr library:

library(tidyr)
economics_long <- gather(economics, key = "variable", value = "value", -date)

This code creates a new data frame called economics_long that has three columns: date, variable, and value. The date column contains the dates from the original economics data set, the variable column contains the names of the different economic indicators, and the value column contains the corresponding values for each indicator on each date.

Now that we have our data in the right format, we can create our area graph using ggplot2:

ggplot(economics_long, aes(x = date, y = value, fill = variable)) +
  geom_area()

This code creates a new ggplot object that uses the economics_long data frame as its data source. The aes function is used to specify the variables to be plotted: the date column on the x-axis, the value column on the y-axis, and the variable column for the fill color of the areas. The geom_area function is used actually to create the area graph.

By default, ggplot2 stacks the areas on top of each other, but we can change this by adding the position = "identity" argument to the geom_area function

ggplot(economics_long, aes(x = date, y = value, fill = variable)) +   geom_area(position = "identity") 

This code creates the same area graph as before, but with the area’s side by side instead of stacked.

We can also customize the graph’s appearance by adding labels, adjusting the color scheme, and so on. Here’s an example:

ggplot(economics_long, aes(x = date, y = value, fill = variable)) +
  geom_area(position = "identity", alpha = 0.7, color = "white") +
  scale_fill_manual(values = c("#FF5733", "#C70039", "#900C3F", "#581845")) +
  labs(title = "US Economic Indicators",
       subtitle = "1967-2015",
       x = "Year",
       y = "Value",
       fill = "Indicator") +
  theme_minimal()

This code creates an area graph with a reduced alpha value to add transparency, a white border for each area, and a custom color scheme using the scale_fill_manual function. We also added a title and subtitle, labels for the x- and y-axes, and a legend label using the labs function. Finally, we applied the theme_minimal theme to give the graph a clean, modern look.

Comments are closed.