Displaying Plotly Graphs on GitHub Pages: A Step-by-Step Guide

Displaying Plotly Graphs on GitHub Pages

As a data scientist and R enthusiast, you’ve probably come across the need to share visualizations with others. In this article, we’ll explore how to display Plotly graphs on GitHub pages.

Background

GitHub Pages is a free service provided by GitHub that allows you to host a website or blog directly from your repository. One of the limitations of GitHub Pages is that it doesn’t support rendering external JavaScript code or images out of the box.

However, with the help of some creative workarounds and the right tools, we can overcome this limitation and display our Plotly graphs on GitHub Pages.

Prerequisites

Before we dive into the solution, make sure you have the following dependencies installed:

  • R
  • ggplot2 for data visualization
  • tidyr for data manipulation
  • dplyr for data manipulation
  • maps for geospatial data
  • ggthemes for customized themes
  • plotly for interactive visualizations

Step 1: Setting up the Plotly Graph

Let’s start by setting up our Plotly graph using R. We’ll use a simple example to illustrate how to create and render a Plotly graph.

library(XML)
library(ggplot2)
library(tidyr)
library(dplyr)
library('maps')
library('ggthemes')
library('plotly')

# Load the data
A_loc <- tbl_df(readLines("https://raw.githubusercontent.com/jpatokal/openflights/master/data/airports.dat"))
New_A_loc <- as.data.frame(sapply(A_loc, function(x) gsub("\"", "", x)))
New_A_loc <- separate(data = New_A_loc, col = value, into = c("Airport_id", "Name","City","Country","IATA","ICAO","Lat","Long","Alt","Timezone","DST","TZ","Type","Source"), sep = ",")
New_A_loc$Lat <- as.numeric(New_A_loc$Lat)
New_A_loc$Long <- as.numeric(New_A_loc$Long)
New_A_loc$Alt <- as.numeric(New_A_loc$Alt)
New_A_loc$Name <- as.character(New_A_loc$Name)

# Set up the Plotly graph
g <- list(showframe = FALSE,
          coastlinecolor = toRGB("white"),
          showland = TRUE,
          landcolor = toRGB("gray80"),
          showcountries = TRUE,
          countrycolor = toRGB("white"),
          countrywidth = 0.2,
          projection = list(type = 'Mercator'))

# Create the Plotly graph
plot_geo(New_A_loc, 
         lat = ~Lat, 
         lon = ~Long,
         text = ~City,
         mode='markers',
         marker = list(color = toRGB("purple"),
                   opacity = 0.5,
                   line = list(color = toRGB("purple"),
                               width = 0.5))
    ) %>% layout(geo = g) %>% htmlwidgets::saveWidget("New_2.html")

This code creates a simple Plotly graph using the ggplot2 package, but it doesn’t render the graph directly in R.

Step 2: Rendering the Plotly Graph

To render the Plotly graph, we need to use the htmlwidgets package. This package provides functions for creating HTML widgets from R objects.

# Load the htmlwidgets library
library(htmlwidgets)

# Render the Plotly graph as an HTML widget
plot_html <- readHTML("New_2.html")

This code renders the Plotly graph as an HTML widget using the readHTML function from the htmlwidgets package.

Step 3: Embedding the HTML Widget on GitHub Pages

Now that we have rendered the Plotly graph as an HTML widget, we can embed it on our GitHub Pages site. To do this, we’ll use the github flavion package.

First, install and load the github flavion package:

# Install the github flavion library
install.packages("github flavion")

# Load the github flavion library
library(github flavion)

Next, create a new file called index.html in your repository’s root directory. In this file, add the following code to embed the Plotly graph:

<!DOCTYPE html>
<html>
<head>
  <title>Plotly Graph on GitHub Pages</title>
  <!-- Load the HTML widget -->
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/htmlwidgets@1.3.2/dist/css/htmlwidgets.min.css">
  <script src="https://cdn.jsdelivr.net/npm/htmlwidgets@1.3.2/dist/js/htmlwidgets.min.js"></script>
  <script type="text/javascript" src="New_2.html"></script>
</head>
<body>
  <!-- Render the Plotly graph -->
  <div id="plotDiv"></div>
  <script>
    document.addEventListener('DOMContentLoaded', function() {
      var plot_div = new Plotly.newPlot('plotDiv', [plot_html], { responsive: true });
    });
  </script>
</body>
</html>

This code embeds the Plotly graph in an HTML file called index.html, which is hosted on GitHub Pages.

Conclusion

Displaying a Plotly graph on GitHub Pages requires some creative workarounds and the right tools. By rendering the Plotly graph as an HTML widget using the htmlwidgets package, we can embed it on our GitHub Pages site using the github flavion package. With these steps, you should be able to display your own Plotly graphs on GitHub Pages.

Example Use Case

Here’s an example of how you can use this technique in a real-world scenario:

Let’s say you’re working as a data scientist and want to share your findings with colleagues or clients. You’ve created a Plotly graph that visualizes some key insights from your analysis, but you want to share it on a website or blog.

To do this, you can use the techniques outlined in this article to render your Plotly graph as an HTML widget using the htmlwidgets package. Then, you can embed this HTML widget on a webpage or blog hosted on GitHub Pages.

This approach allows you to easily share your visualizations with others without having to worry about hosting them yourself. Plus, it’s easy to update or change the visualization in real-time, making it a great option for dynamic presentations and reports.

Tips and Variations

Here are some additional tips and variations you might find useful:

  • Use the plotly package instead of htmlwidgets to render your Plotly graph. The plotly package provides more advanced features and customization options.
  • Experiment with different Plotly themes and styles to customize the appearance of your graph.
  • Use the github flavion package in combination with other packages, such as rstudio or shiny, to create interactive presentations and reports.
  • Consider using alternative visualization libraries, such as matplotlib or seaborn, for creating static images.

Last modified on 2023-07-11