How to create an interactive scientific plot online

In my previous post I showed a fully interactive online graph of one of the plots in my recent paper on leadership in sticklebacks. In this follow-up post I will explain how to easily create such an interactive plot yourself. To be able to do this you will need some experience with the R-language and ideally with ggplot2.

First create an account at plot.ly, which is free. After you have created your account, go to “settings” and click on “generate API key”. You will need your username and this key to link your account to R.

Now you have your account ready start-up R and set-up the R workspace:


Now just create the ggplot as you would normally. I am recreating the plot exactly as it was published in my recent paper:

The plot in R now looks like this:

Plot of following behaviourNow you have created the plot in R it is very easy to make the plot interactive and online:

A browser window will now open-up showing your newly created plot! To make the plot work best online, I suggest spending some final 5 to 10 minutes to further adapt the plot. This is very easy to do with plot.ly.

For this particular plot I removed the zero-lines and the gridlines, styled the margins and axes, added a plot title and added trend lines for both bold and shy focal fish using the “Fit data” option:

plotly-fit-traceFinally, instead of the traditional legend I added dynamic plot labels that show which points refer to the bold and shy subset using the “Notes” option:

plotly-add-notes

Now you are finished with creating your online plot, the final thing that is left to do is to embed it! To do this, simply click the blue “Share” button in the top right and click on “Embed” on the following screen:

plotly-embed

And the final result! Hover over the plot and use your mouse to move the plot around, zoom-in and out etc:

Here is the full R-code to create this specific plot

I hope this post was helpful to researchers wanting to make their science more interactive and for anyone who’d just like to show beautiful interactive online plots. Let me know in the comments below what you think or if you need help!