R Tutorial: An Introduction to plotly

Adam Loy shows how to turn static ggplot2 charts into interactive Plotly graphics with a single function call, `ggplotly()`. He uses wine data to demonstrate the three components of ggplot2, then warns that interactivity alone doesn't make good design, and some plots need Plotly's native API instead.
Chapters
Hi, I'm Adam Loy, a statistician, R developer and professor. Welcome to my course on interactive data visualization using Plotly in R. The Plotly R package provides an interface to the plotly JavaScript graphing library, allowing you to create interactive web based graphics entirely in R. Plotly is a great choice for creating interactive graphics because you can create a wide variety of interactive graphics in multiple formats. For example, you can execute your code in the console and interact with your graphic entirely in the viewer pane. Or you could deploy your graphic to the web as a shiny app. Plotly is also backed by a strong community and is still under heavy development, making it a great time to learn how to harness its power. As of November 2018, plotly downloads were an order of magnitude higher than its competitors like rboke and Highcharter. Before you start creating graphics, it's important to think carefully about what type of graphic best suits your purpose, a static graphic or an interactive graphic. To highlight features of each type of graphic, let's consider a scatter plot of proline against flavonoids, two chemicals found in wine. A static plot such as one rendered in ggplot2 remains permanently fixed. This format is useful for printed materials such as reports, but can only display what you, the creator, have highlighted. On the other hand, the user can update an interactive graphic. For example, you can drill down to specific observations using hover info or focus on subsets of your data by selecting or deselecting groups. Simple interactions can improve your ability to explore your data, and throughout this course you'll learn how to add these to your graphics toolkit. To begin, consider the wine dataset from the UCI Machine Learning Repository containing the results of a chemical analysis of 178 wines, all grown in the same region in Italy but derived from three different cultivars. We'll begin by converting the static scatter plot of proline against flavonoids we saw earlier to to a Plotly interactive graphic. Remember that there are three parts to a ggplot graphic. First, we have the dataset. Here we pass the wine DataSet into the ggplot command using the pipe operator. Second, we map the variables in the dataset to aesthetics in the graph. Here we specify the mappings with AES x flavonoids y proline color type, telling the plot which variable defines each aesthetic. Third, we specify the plot type by adding a layer to create a scatter plot. We add a plus sign after the ggplot base layer and specify geompoint. Finally, we store this plot in the static object. The command ggplotly allows you to convert a ggplot graphic to a plotly interactive graphic in a single line of code. After loading the plotly package, pass the static ggplot object to the ggplotly command and an interactive version is created. Before moving on, there are two important points to note. First, while interactive graphics are wonderful tools for exploring your data and communicating your findings, interactivity does not ensure that you have created a good graphic. It's important to review best practices of data visualization, so be sure to think about both the syntax and design principles as you complete the course. Second, not all ggplot objects can be converted to plotly objects, so it's important to know how to create interactive graphics directly. We'll focus on this ground up approach in all subsequent lessons. It's time to practice converting ggplot2 graphics into plotly.
More from Richie

#359 My Best Friend is AI with Valerie Tiberius, Professor of Philosophy at University of Minnesota

How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon

#358 How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon







