Introduction to Plotly


Plotly is part of the “html widgets” family in R. It is a library of functions in R that allows you to translate ggplot2 graphics to interactive web visualizations. It llows you to make beautiful, interactive, exportable figures in just a few lines of code.

For further information, you can allows check some online resources such as here.

ggplot2 and plotly


Some of you might confuse ggplot2 with plotly and the difference is simple. Although ggplot2 package is your first choice when it comes to data visualisations, however, it has some limitations to when it comes to user ineractvitity. Such problems arise while creating interactive documents using R Markdown or dashboard apps in R Shiny.

The static ggplot2 chart can quickly be made interactive by simply placing it inside the ggplotly function (after installing and loading plotly).

The below static plot has now been brought to life. Users can now zoom, hover, pan and export the plot, and more.


  • Before using ggplotly


  • After using ggplotly

Reference


Installing & Loading

You can install “plotly” with the function ‘install.packages()’ and load it with ‘library ()’.

Example1 : with/without plotly

The below line graphs demonstrate how the average bill is different for females and males during lunch and dinner. However, the first line graph is illustrated using ggplot and the second one is illustrated using ggplotly.

  • This line graph is static, where it only displays data but doesn’t include any interactive functionalities.


  • This line graph allows user to interact with data. For example, you can zoom in/out, compare data at a certain point, reset axes to original state, and hover over points to see their value.All you need to do is to wrap your existing code in ggplotly().

Reference

Example2

** plot_ly : Initiate a plotly visualization

Other Plotly Fucntions


  • as_widget : Convert a list to a plotly htmlwidget object

  • gg2list: Convert a ggplot to a list.

  • add_data : Add data to a plotly visualization

  • plotly-shiny :Shiny bindings for plotly

  • geom_line : Connect observations, ordered by x value.

  • plotly_data : Obtain data associated with a plotly graph

Plotly & DASH

Dash for R is an open-source framework for building analytical applications, with no Javascript required, and it is tightly integrated with the Plotly graphing library.

Dash apps are rendered in the web browser. You can deploy your apps to servers and then share them through URLs. Since Dash apps are viewed in the web browser, Dash is inherently cross-platform and mobile ready.

Plotly: Color-Blind Friendly

Community member Jamie Nuñez introduced a new colorscale to plotly.js, which is called “Cividis”. It was developed by her and her colleagues. Their purpose was “Optimizing colormaps with consideration for color vision deficiency to enable accurate interpretation of scientific data.” As a side note, plotly.js is the open source javascript graphing library that powers plotly.

Reference

Further Resources

Please check the below URLs if you feel a bit curious to know more about plotly and what has been discussed above.

Sample datasets
Getting Started
Functions
Start with ggplot2