plotly Table in R (2 Examples)

 

Hi! This tutorial will show you how to build an interactive plotly table in the R programming language.

Here is an overview:

Let’s jump into the R code!

 

Install & Load plotly

To install and load the R plotly library, run the lines of code below in your preferred R programming IDE:

# install plotly
install.packages("plotly")
 
# load plotly
library(plotly)

With plotly installed and loaded into our R programming environment, we can now use its table-building function.

First, though, we need to create a sample dataset.
 

Create Sample Dataset

We will make use of the iris dataset, which comes preloaded in R Studio.

You can preview the first 10 rows of the dataset:

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

Now that the dataset is loaded and previewed, we can build an interactive plotly table.
 

Example 1: Build Basic Table

In this first example, we will build a basic plotly table showing the Sepal.Length and Petal.Width columns of the iris dataset:

fig <- plot_ly(
  type = "table",
  columnwidth = c(100, 100),
  columnorder = c(0, 1),
  header = list(
    values = c("Sepal.Length", "Petal.Width"),
    align = c("center", "center"),
    line = list(width = 1, color = "black"),
    fill = list(color = c("blue", "green")),
    font = list(family = "Arial", size = 14, color = "black")
  ),
  cells = list(
    values = rbind(iris$Sepal.Length, iris$Petal.Width),
    align = c("center", "center"),
    line = list(width = 1, color = "black"),
    font = list(family = "Arial", size = 12, color = "black")
  )
)
 
fig


In the above example, the table has two columns with widths set to 100 pixels, in a specific order set by the indexes 0 and 1.

The header of the table is a list object, which contains column names (Sepal.Length and Petal.Width), alignment information (center), and the information of border line color (black) and background colors (blue and green).

The font for the header is also specified in this list, which is Arial in black with the font size 14.

The table cells contain the values of iris$Sepal.Length and iris$Petal.Width, which are center-aligned and in Arial font with the size of 12 and color of black. The border line color is also set to black.

 

Example 2: Style Table

In this second example, we will add some style to the table:

fig <- plot_ly(
  type = "table",
  columnwidth = c(100, 100),
  columnorder = c(0, 1),
  header = list(
    values = c("<b>Sepal.Length</b>", "<b>Petal.Width</b>"),
    align = c("center", "center"),
    line = list(width = 1.5, color = "black"),
    fill = list(color = c("blue", "green")),
    font = list(family = "Arial", size = 15, color = "black")
  ),
  cells = list(
    values = rbind(iris$Sepal.Length, iris$Petal.Width),
    align = c("center", "center"),
    line = list(width = 1.5, color = "black"),
    fill = list(color = c("#25FEFD", "lightgray")),
    font = list(family = "Arial", size = 13, color = "blue")
  )
)
 
fig


Here, we made the table header bold by wrapping the header values in the bold (b) HTML tag.

Next, we increased the line width of the columns from 1 to 1.5, and then colored the cells of the columns of the table by defining the fill = argument, which took a list of colors for the cells.

We also increased the font sizes of the column header and cells and changed their colors as well.

You can play around with these parameters, and style the table as much as your use case demands.

 

Video, Further Resources & Summary

Do you need more explanations on how to build a basic plotly table 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 build a basic plotly table in R.

 

The YouTube video will be added soon.

 

With that, we have demonstrated how to build a plotly table in R. You may also want to check out some other interesting R plotly tutorials on Statistics Globe:

This post has shown how to build a basic plotly table in R. I hope you enjoyed reading it! 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