From the course: R: Interactive Visualizations with htmlwidgets

What is the Plotly library? - R Tutorial

From the course: R: Interactive Visualizations with htmlwidgets

Start my 1-month free trial

What is the Plotly library?

- [Instructor] The plotly library allows us to create a vast range of different types of interactive charts, plots and even maps using the R plotly Library. Let's look at some examples of what we can build. In our exercise files, there's a file called example-charts.R. Let's run quickly through what we can build here. If I decrease the size of my environment because I don't need it, I load these three libraries here, lines one through three. Then line six I generate a stacked bar chart. Now my stacked bar chart, I can hover over and get information about distinct subcategories within each bar. I can change the type of hover information, so I get information about all the subcategories within a category, and I can also deselect series from my legend in order to slice through my data. As well as this kind of stacked bar chart, there's also a percentile stacked bar chart, which is generated on line 15 through 22. Plotly is also very good at generating scatter plots with a few hundred to several thousand data points in very quickly and very efficiently. There's also a lot of interactivity provided for free within Plotly. So I can click and draw an area that I want to zoom in on, and there's also lasso tool, which allows me to select individual data points if I so wish. As well as scatter plots, it's also possible to create line charts, which I do on line 32 through 40. It's also possible to build histograms through Plotly, and if we just load the gapminder data set on line 48, wrangle it a little bit with dplyr on line 50 through 54, we can also create Choropleth using Plotly. But, Plotly also has a superpower. Let me go into my file explorer and open up ggplot2-conversion.R. At the top of this file, we load the data set called british_weather.rdata. Let's run that. And in line three through 16, I use dplyr and then ggplot2 to generate a static chart. So let's run this code, and then create a couple of new lines, and then let's see this static chart, which is called gg_british_weather. So, here we see a static ggplot2 chart. Plotly's magic power is the ability to convert ggplot2 static charts into interactive charts with ggplotly. Let's see this in action by evaluating line 20. Here we can see, as I hover over the points, I have information. I can also zoom in on certain areas of the chart if I so wish. Lines 24 through 38, we use ggplot2 again. Let's have a look at this chart. So this chart is called gg_facet_weather. So this uses ggplot2's very popular and very powerful faceting system. So here we can see separated out, England, Northern Ireland, Scotland and Wales from our data. ggplotly is capable of converting these kind of charts into active Plotly charts as well. So here we have our tool tips, and I can also zoom in on my charts as well. Okay, so that's really impressive, but let's be a little bit more rigorous in our discussion of what Plotly allows us to do. We've established that Plotly lets us create these lovely interactive charts, but how is it able to do that? Well, Plotly is an example of a htmlwidget library, which binds to the extremely popular Plotly JavaScript library to provide us our interactivity. What can we do with these interactive charts? Well, like all HTML widgets, we can easily include these outputs in Rmarkdown reports or even in Shiny apps. So how does Plotly compare to other options for visualizing data? Well, Plotly is deliberately designed to be as general a tool as possible. There are probably 20 plus types of chart you can make with the library. The ability to automatically create interactive charts from the output of ggplot2 really can't be undersold. It's really amazing. Sometimes you do have a little bit of less from the output of ggplotly, but it's really astonishingly useful if you're quickly iterating on charts, for instance, during exploratory data analysis. I think it's also worth celebrating the wide range of onscreen tools that Plotly provides compared to other HTML widgets. Zoom is on by default, as are tools for panning and moving around in a chart. There are even context-aware tools for lassoing selecting points. All of these interactions can be detected and utilized within a Shiny app. Plotly also provides very nice tools for combining multiple axes together, and even for creating what Plotly called subplots, which includes connecting grids of charts and the ability to insert charts. Creating these is slightly advanced. I recommend you study the documentation in detail if this is of interest to you. In addition to providing flexibility with multiple axes, Plotly really does focus on a scientific and technical audience, providing a wide range of statistical charts not found easily in other HTML widget libraries. These are often really useful for exploratory or comparative data analysis, or very specific scientific data sets. For instance, ternary phase diagrams. For this reason, I think Plotly is a great choice for internal use, but it is slightly less easy to use for customer-facing charts. So let's get into the limitations of the library. Whenever talking about the limitations of HTML widgets, it's important to first ask about what license the underlying JavaScript library is subject to. Plotly is copyright Plotly Inc., but is made available under an MIT license and therefore free to use for all users. Plotly does have a fairly annoying limitation, in that you often have to manually adjust margins and the layouts of charts to prevent axes labels being chopped off. In the exercise files, I have an example demonstrating this. So if I go to the file explorer, and I open up manual-label-adjustment, at the top of the script file, we load the oidnChaRts library, the plotly library and highcharter library. We use oidnChaRts to create a stacked bar chart of the same data using both highcharter and plotly. Lines five through to 12 generate the chart using highcharter, and we can see it looks nice and clean and all of our labels for our Y axis fit neatly in the chart. Lines 14 through 21 use the default options of Plotly to create the exact same chart. Let's run that code with Command-Enter. We can see we have a little bit of a mess. If I click on this button here, which opens the chart in a new browser window, we can see that the Y axis labels are still chopped off. It's not that difficult to modify the chart so the labels are no longer chopped off. Line 23 through 31 includes the adjustment we need to make here with the function layout. So if I run this code, and open the chart in a new browser window, we can see now I've given enough space to my category labels to be displayed well. An additional limitation of the plotly library is there's sadly very little customizability when it comes to the tooltips or mouseover text shown when moving your cursor over markers. Despite mentioning quite a few limitations here, I do still highly recommend Plotly as and excellent tool for exploratory data analysis visualization and for subject-specific data visualizations. It's worth mentioning, however, that Plotly has really great integration with Shiny. It's very simple to watch for the following events. Hovering, clicking, or selecting markers within charts. Let's see a quick demonstration of this. If we go to the file explorer and click on server.R, and run our Shiny app, we're asked if we want to save this file before we continue. We'll select Don't Save because it's not actually used in the Shiny app. And here, let's look at my Shiny app. So, when I click on one of the points within the chart, what it does is it tells me which curve I've selected and the coordinates of my click. If I move to the fourth curve along, we can see when I click on this point it tells me I've selected curve three and it tells me the coordinates of my click as well. This information is really easy to extract from Plotly in Shiny. How about when you want to go and do advanced things with Plotly? Well, Plotly tries to balance a desire to give you the simplest and most easy-to-use plotting tool with providing the full range of features from plotly.js. This does lead to some conflicting design choices. Sometimes it ends up being more practical to write things verbosely so you can add highly customized behavior to your charts. The documentation for the underlying JavaScript library is available on the Plotly website, and there's currently a book being drafted together specifically focused on using the Plotly htmlwidget library.

Contents