How to Draw a plotly Area Chart in R (3 Examples)

 

Hello! In this tutorial, I will show you how to make area charts in plotly in the R programming language.

First, though, here is a quick overview of this tutorial:

Let’s dive right into it!

 

Install & Load plotly & dplyr Packages

If you do not have plotly and dplyr libraries already installed and loaded in your R environment, you can run the lines of code below in either R Studio or in any other preferred code editor that can run R; otherwise, you may skip to the next section:

# install plotly and dplyr
install.packages(c("plotly", "dplyr"))
 
# load plotly and dplyr
library(plotly)
library(dplyr)

Dplyr is a powerful library in R programming for data manipulation and analysis; and it allows us to use the pipe operator to pipe lines of code together and run them as one chunk.
 

Example 1: Basic Filled plotly Area Plot

In this example, we will make use of the diamonds dataset, which comes preloaded in R Studio; but it can also be downloaded from an online dataset repository.

We will first create the density objects of the carats for two kinds of diamonds: Premium cut and Good cut, as shown below.

d1 <- diamonds[diamonds$cut == "Premium",]
dense1 <- density(d1$carat)
 
d2 <- diamonds[diamonds$cut == "Good",]
dense2 <- density(d2$carat)

Let’s now plot the objects as a filled area plot!

fig <- plot_ly(x = ~dense1$x, 
               y = dense1$y,
               type = "scatter",
               mode = "lines",
               name = "Premium Cut",
               fill = "tozeroy",
               fillcolor = "blue") |> 
  add_trace(x = ~dense2$x,
            y = ~dense2$y,
            name = "Good Cut",
            fill = "tozeroy",
            fillcolor = "red") |> 
  layout(xaxis = list(title = "Carat"),
         yaxis = list(title = "Density"),
         title = "Diamonds")
 
 
fig


 

Example 2: Basic Filled plotly Area Plot without Borders

In this example, we will demonstrate how to remove the border lines of the area plot that we created in the previous section.

fig <- plot_ly(x = ~dense1$x, 
               y = dense1$y,
               type = "scatter",
               mode = "none",
               name = "Premium Cut",
               fill = "tozeroy",
               fillcolor = "blue") |> 
  add_trace(x = ~dense2$x,
            y = ~dense2$y,
            name = "Good Cut",
            fill = "tozeroy",
            fillcolor = "red") |> 
  layout(xaxis = list(title = "Carat"),
         yaxis = list(title = "Density"),
         title = "Diamonds")
 
fig


We only needed to set the argument mode = in the plot_ly() function to “none” to remove the border lines.

 

Example 3: Stacked plotly Area Chart

In this example, we will build a stacked area chart, using the US personal expenditure dataset, which comes preloaded in R Studio. You can always take a look at the data frame by running print(data) if you want.

data <- t(USPersonalExpenditure)
data <- data.frame("year"=rownames(data), data)

Let’s now build a stacked area chart!

fig <- plot_ly(data, x = ~year, y = ~Food.and.Tobacco, name = 'Food and Tobacco', type = 'scatter', mode = 'none', stackgroup = 'one', fillcolor = '#F5FF8D')
fig <- fig %>% add_trace(y = ~Household.Operation, name = 'Household Operation', fillcolor = '#50CB86')
fig <- fig %>% add_trace(y = ~Medical.and.Health, name = 'Medical and Health', fillcolor = '#4C74C9')
fig <- fig %>% add_trace(y = ~Personal.Care, name = 'Personal Care', fillcolor = '#700961')
fig <- fig %>% add_trace(y = ~Private.Education, name = 'Private Education', fillcolor = '#312F44')
fig <- fig %>% layout(title = 'United States Personal Expenditures by Categories',
                      xaxis = list(title = "",
                                   showgrid = FALSE),
                      yaxis = list(title = "Expenditures (in billions of dollars)",
                                   showgrid = FALSE))
 
fig


With that, we have demonstrated how to build an area chart in plotly, using R. I hope you found this tutorial helpful!

 

Video, Further Resources & Summary

Do you need more explanations on how to make plotly area charts in R? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

In the video, we explain in some more detail how to make plotly area charts in R.

 

The YouTube video will be added soon.

 

Furthermore, I encourage you to check out other interesting plotly in R tutorials on Statistics Globe, starting with these ones:

This post has shown how to make plotly area charts in R. In case you have further questions, you may leave a comment below.

 

R & Python Expert Ifeanyi Idiaye

This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top