Introduction to the plotly Package in R
In particular, plotly graphics automatically contain interactive elements that allow users to modify, explore, and experience the visualized data in new ways!
This article provides an example of a plotly graphic, describes the main features of plotly, and outlines the situations when plotly may be the right solution.
You can scroll through their gallery of basic charts to see a few examples.
Note: This article was created in collaboration with Kirby White. Kirby is an organizational effectiveness consultant and researcher, who is currently pursuing a Ph.D. at the Seattle Pacific University. You can read more about Kirby here!
We’ll use the
mtcars dataset for this example, as it is already loaded in R. Let’s store it in a new object called
df and do a simple bit of pre-processing, though:
df <- mtcars df$name <- row.names(mtcars)
Go ahead and install and load the plotly package using this code:
Let’s look at the relationship between the number of cylinders a car has, and its total engine displacement. At its simplest, you can create a scatterplot of this data in plotly with this code:
plot_ly( data = df, x = ~cyl, y = ~disp )
plot_ly() function initialized the graphing library while
data = df indicates the data frame where the data is stored.
x = ~cyl and
y = ~disp indicate the columns to plot on each axis.
You may have gotten some warnings indicating that the “type” and “scatter mode” of the plot had not been specified. These two arguments are optional, but are required to alter the type of graph being created. This is what the code looks like when we specify those arguments to create the same graph:
plot_ly( data = df, x = ~cyl, y = ~disp, type = "scatter", mode = "markers" )
If we want each cylinder to be shown in a different color, we can add that with a single argument in our function. Since we want each number of cylinders to be treated as a discrete category instead of a numeric range, we’ll tell plotly to interpret cylinder as a factor.
plot_ly( data = df, x = ~cyl, y = ~disp, color = ~factor(cyl), type = "scatter", mode = "markers" )
As you interact with your graph, you may have noticed that detailed information pops up over each point as you hover your cursor over it. This is one of the great features of plotly! With another simple addition, we can add the name of each vehicle to the popup:
plot_ly( data = df, x = ~cyl, y = ~disp, color = ~factor(cyl), text = ~name, type = "scatter", mode = "markers" )
Plotly has a rich and complex set of features. You’ve already encountered a few!
The most common features are:
- Tooltip “hover” info
- Zoom in and out of graphs
- Users can export graphs as an image
Some of the more complex ones are:
- Integrating multiple graphs together
- Template hover info
- Animations and moving graphics
- Integration with enterprise coding environments
There are many more features to plotly!
For those who are already familiar with ggplot, you’ll be glad to know about the
ggplotly function, which takes an existing ggplot graph and renders it with the plotly library.
fig <- ggplot2::ggplot(data = df, aes(x=cyl, y=disp)) + geom_point(aes(color = factor(cyl))) ggplotly(fig)
When to Use plotly
As you can see, plotly has several features that make it intriguing and interesting to work with. There are many applications where you could use ggplot or plotly, but here are the factors that may make plotly the best option:
- Graphs presented in a digital/online format
- You want users to interact with the graph
- You need more customizability than ggplot allows
- Want to render graphics in a higher-resolution
- Collaborating with others across languages (e.g., Python, Bash, Node, etc.)
- Building unusual chart types (treemaps, sunburst, sankey, etc.)
The main downside of plotly is its complexity and poor documentation. It is a very powerful library, but is targeted towards for a highly technical audience. Their documentation has improved substantially in the past year, but can still be difficult for new users to understand and find the information they need (as of this article’s publication).
You can check out these other articles for more detailed examples of these popular charts in plotly:
- plotly Scatterplot in R
- plotly Barplot in R
- plotly Line Plot in R
- plotly Histogram in R
- plotly Boxplot in R
- plotly Heatmap in R
Check out this video to see the advantages of plotly and see an example:
Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.
If you accept this notice, your choice will be saved and the page will refresh.
We hope this graphics tutorial provided you with some insights on how to use the plotly library in R. In case you have any further questions, please let us know in the comments section below!
Statistics Globe Newsletter