Would you like to learn how to create more engaging and better looking tables using the R programming language?
In this tutorial I show you a practical example accessing Spotify data to get the top 10 most listen songs of today and visualizing them in a table using R programming.
You will learn how to:
- fully customize any element of a table and use pre-built themes.
- add any images programmatically from the Web.
- insert graphics such as bar plots, sparklines, etc.
- merge columns to better synthesis information.
You can get all the R code of this tutorial by joining my newsletter on felixanalytix.com. Once you subscribed, you will receive an automatic email with the URL of my GitHub account, where you can download all the R scripts of my tutorials.
You can also watch this tutorial on YouTube:
Install and Load R packages
The first thing we want to do is to install on the load necessary R packages if they are not installed on your computer. You can do it by running the R lines 6–16 below.
- The spotifyr R package is a wrapper for Spotify web API.
- the gt R package(which we will use extensively) will alow us to create beautiful HTML tables.
- the gtextras R package extend the gt package with more customization.
- the tidyverse R package is a set of R packages for data wrangling and data visualisation.
- the scales R package to use scale functions
How to connect R to Spotify API
First you need to create an account to get the credentials. Once you created your account, you can go to this dashboard application where you can create a new app.
After clicking on your Spotify app, you can get a client ID and a client secret.
All you have to do is to copy the client ID and the client secret and pass it inside the code below to create two global environment variables.
The Spotify client ID and the Spotify client secret as R global environment variables are taken by default by the spotifyr R package. Once you run the get_spotify_access_token function from the spotifyr package you will be connected to your Spotify API.
Download Spotify Playlist data
Now we will download a specific Spotify playlist. I found a playlist made by Spotify called Top 50 Global.
If you want to download data from another playlist (for example your own playlist), all you have to do is to copy past the end the playlist ID. You can pass the ID in this function below and call the get_playlist function from the spotifyr package.
By running the tidy() function, your dataset will by structured in a tidy way. We will take only the top 10 using the head() function and perform extra data wrangling: add a new column called “rank” and add a point after each numerical number (for a better-looking table).
We will also download audio metrics such as the valance (i.e. the positivity), dancability, etc.
We will also take some information about the artist from our top global data set and to get their relative picture URLs using get_artist.
Finally we will join all our tables by ID and by artist name.
Below is our final dataset, containing 40 different variables of 10 sound track.
How to Create and Customize Tables using R programming
Now that our data is ready we can just create a first table just by running the gt() function on specific variables. Below I selected the rank, the track_name, the artist_name, the danceability, the valence and the energy.
Now we want to customize it to make it look better. The gt R package have a lot of functions to allow us to work on each elements of the table using:
- cols_label() to rename the different column names.
- tab_style() to style of different elements, for example to make all the cells of the track_name column as italic.
- tab_options() to make the title bigger.
- header_tab() to add a title and a subtitle
- tab_source_note() with the md() function (which stands for markdown) to customize the caption content (one asterik it for italic).
Finally you can add a pre-built theme: here I used bf_theme_pff():
There are other theme options, such as the themes 538, Excel, the guardian, nytimes, etc.
How to Insert Images in Tables using R
Now I want to show you how to insert images in your tables. All you have to do is add a new function from the gt R package.
Here I use the gt_img_circle() function with the img variable as a first argument (the URL of each picture). I also specify the height (as 25 pixels), to control how large the columns are to avoid having to big images.
Color Gradient for Numeric Columns in Table
Now I want to show you how to customize your numeric variables inside your tables. Adding color gradient in your numerical variables allows your audience to see better which number are high and low. You can add a color palette using the scales R package.
Below (R code lines 109–111) used the gt_color_rows() function on the three numeric variables with a color palette obtained using the hue_pal() function (R code line 103) from the scales R package.
How to Add a Percent Bar Chart in a Table with R
Another idea would be to add percent bars as our values are between 0 and 1, using this time the gt_plt_bar_pct() function from the gtextras R package.
The gtextras package contains a lot of different functions so you can see the percent dot, the point, the percentile, sparklines, etc.
You can also easily merge two columns together using the gt_merge_stack() function (here joining track_name and artist_first).
Finally let’s change the theme to dark to make our charts pop up.
You can get all the R code of this tutorial by joining my newsletter on felixanalytix.com. Once you subscribed you will receive an automatic email with the URL of my GitHub account, where you can download all the R scripts of my tutorials.
Thank you for reading! Don’t forget to clap and subscribe if you liked this tutorial.