Get startedGet started for free

What is plotly?

1. An Introduction to plotly

Hi, I'm Adam Loy, a statistician, R developer, and professor. Welcome to my course on Interactive Data Visualization using plotly in R.

2. plotly

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 rbokeh and highcharter.

3. Static vs. Interactive graphics

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 scatterplot 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.

4. Wine data

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.

5. ggplot2 scatterplot

We'll begin by converting the static scatterplot of proline against flavanoids we saw earlier 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 parentheses, x = Flavanoids, 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 scatterplot we add a plus sign after the ggplot base layer and specify geom underscore point. Finally, we store this plot in the static object.

6. ggplotly()

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.

7. Remarks

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.

8. Let's practice!

It's time to practice converting ggplot2 graphics into plotly charts.