Visualize Data Frame in plotly Graph in R (Example)

 

Hello! Welcome again to another interesting tutorial from Statistics Globe. In this one, I will show you how to visualize a data frame in plotly graph in the R programming language.

But before we start, here is a quick look at what we shall do in this tutorial:

Let’s now dive into it!

 

Install plotly

To use the R plotly library, we need to first install it. Therefore, if you have not already installed plotly, then run the line of code below in your R Studio IDE or any other preferred code editor or IDE that can run the R code; otherwise, you may skip to the next section:

install.packages("plotly")

Running the code above will install plotly and all its dependencies in your R programming environment.
 

Install dplyr

Next, we also need to install the R dplyr library, which is the foremost library in R for data analysis and manipulation. With dplyr, we can use the pipe operator to pipe lines of R code together and run them as a chunk. So, please run the code below to install dplyr:

install.packages("dplyr")

 

Load plotly & dplyr

Having installed both plotly and dplyr, the next thing we need to do is to load both libraries in our R programming environment. Therefore, run the lines of code below to load both plotly and dplyr:

library(plotly)
library(dplyr)

This will ensure that we have access to all the plot-building functions in plotly as well as the R pipe operator.
 

Visualize Data Frame via plotly

We are going to use the iris dataset to be used in the plotly visualization. If you are using the R Studio IDE, the iris dataset comes preloaded. You can take a look at the first 10 rows of the data frame by running the below code:

print(head(iris,10))
#   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#1           5.1         3.5          1.4         0.2  setosa
#2           4.9         3.0          1.4         0.2  setosa
#3           4.7         3.2          1.3         0.2  setosa
#4           4.6         3.1          1.5         0.2  setosa
#5           5.0         3.6          1.4         0.2  setosa
#6           5.4         3.9          1.7         0.4  setosa
#7           4.6         3.4          1.4         0.3  setosa
#8           5.0         3.4          1.5         0.2  setosa
#9           4.4         2.9          1.4         0.2  setosa
#10          4.9         3.1          1.5         0.1  setosa

For the demonstration, we are going to visualize the data frame in a plotly scatter plot. Unlike in the Python pandas library, the built-in R data frame function does not have a plotly backend. For the application in pandas, see the tutorial: How to visualize pandas DataFrame in a plotly graph in Python.

In R, we can pipe the plotly plot-building function to visualize our data in a scatter plot. Please run the code chunk below to build the simple scatter plot:

fig <- iris |> 
  plot_ly(x = ~Sepal.Length,
          y = ~Petal.Width,
          type = "scatter",
          color = ~Species) |> 
  layout(title = "Scatterplot of Iris Sepal Length and Petal Width")
 
fig


We could also write our code this way:

fig <- plot_ly(iris,
                x = iris$Sepal.Length,
                y = iris$Petal.Width,
                type = "scatter",
                color = iris$Species) |> 
  layout(title = "Scatterplot of Iris Sepal Length and Petal Width")
 
fig

Whichever way you choose to write your code, you can easily visualize your data by plotly as you can see. You can also build other types of visualizations of your data frame, like bar plots, line plots, and histograms by simply changing the “type” argument in the plot_ly() function.

So, there you have it, we have been able to visualize a data frame in a plotly scatter plot using the R programming language. I want to believe that you have found this tutorial helpful. If yes, then be sure to check out the other plotly in R tutorials on Statistics Globe, and I will see you soon in the next one!
 

Video, Further Resources & Summary

Do you need more explanations on how to visualize data frames in plotly graphs 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 visualize data frames in plotly graphs in R.

 

The YouTube video will be added soon.

 

Furthermore, you could have a look at some other tutorials on Statistics Globe:

This post has shown how to visualize data frames in plotly graphs 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.

Menu
Top