Embed an Interactive Plot with ggplot, plotly, & RStudio

Melanie Walsh
3 min readMay 3, 2022

--

Last year I started diving into R fully for the first time, and one of my favorite R tricks is that you can make interactive data visualizations super easily with ggplot2 and plotly.

Like this one that I made from the Pudding’s data about how often women get to speak in Hollywood films:

This data is drawn from the Pudding

Basically, you can take any ggplot viz, add one line of plotly code, and voila — you’ve got an interactive visualization!

##### Regular scatterplot
year_plot <- ggplot(data = women_film_year) + # Make scatterplot
geom_point(aes(x=release_year, y=num, color = num)

###### Cool interactive scatterplot with tooltips!
ggplotly(year_plot) # Make interactive plot

But what happens if you want to embed your cool interactive data visualization in a Medium blog post like this one?

Well, it turns out that’s relatively easy, too, so I wanted to share how you can do it.

Upload Your Plot to Chart Studio’s Website and Then Include the URL

To embed an interactive plotly viz in a Medium blog post like this one, you simply need to publish the plot as a URL hosted by plotly’s Chart Studio and then include that URL in the blog post. The data viz and any corresponding interactivity should then render automatically when you publish your blog post.

To upload a plotly data viz from your computer to Chart Studio, you need to follow a few steps.

1. First, you need to sign up for a free Chart Studio account

2. Then, you need to set up a connection between your computer and the Chart Studio website so that you can upload your plot to the website

To set up this connection, you need to create and establish your Chart Studio “API Key” — a unique identifier that will basically function like a password. You should be able to create your API key by going to Settings -> API Keys and then choosing “Regenerate Key.”

3. Once you’ve copied that API key to your clipboard, you need to open the R file that contains your ggplotlycode and add the lines of code shown below:

Sys.setenv("plotly_username"="your-user-name")
Sys.setenv("plotly_api_key"="your-API-key")

Make sure to change “your-user-name” to your actual Chart Studio user name and “your-API-key” to your actual API key.

When you run these lines of code, your plotly username and API key will be established in your environment (that’s the “env” of Sys.setenv).

4. Once those important details are established, you should be able to upload your plotly viz to the Chart Studio website with theapi_create() function, passing in whatever variable name contains your ggplotly viz as well as a filename for your plot:

api_create(your_plot, filename = "your-filename")

This line of code should prompt a web browser to open and for the plot to appear on your account on the Chart Studio website:

5. Click on the “Share” link in the bottom-right corner of this screen, and you’ll find a URL that you can then copy and paste into your Medium blog post: `https://plotly.com/~melwalsh/5/`

After you copy and paste the link into your blog post, you can hit enter, and the plot should render; however, the interactivity of the plot will not work properly until you officially publish your blog post.

6. Once you publish your blog post, though, your plotly viz should be dynamic! Sweet interactivity!

This data is drawn from the Pudding

--

--

Melanie Walsh

Assistant Professor at University of Washington's Information School // https://melaniewalsh.org/