Interactive plots with plotly
1. Interactive plots with plotly
We have seen how a shinydashboard can be constructed, from wireframing to adding objects. In this video, we will explain how interactive plots can be added to a shinydashboard via the plotly library.2. An example: Airbnb listings in London
Let's first describe the dataset that we will be using to demonstrate the power of plotly. Imagine we are working at a tour agency based in London, in which we are tasked to help customers with their accommodation plans. To this end, a collaboration was set up with Airbnb, which readily provided us with data. Let's first import this dataset and store it as listings. Here are some of the variables in the dataset. Some notable variables that we will work with are numerical variables like availability_365, minimum_nights and price and categorical variables like room_type and neighbourhood. There is a multitude of visualizations that we can create with this data.3. Getting started with plotly
To get started with plotly, we will first need to import the plotly library. Typically, a plotly object is defined by placing a ggplot object within the ggplotly function.4. Bubble plot with ggplot
Our first example is a bubble plot that we will store as bubble. See that we have aggregated some information from the listings dataset using group_by and summarize to obtain average prices and room availability here. We also used n to obtain the count of each pair. We shall visualize the relationship between average price and room availability for each room_type-neighborhood pair. Let's also set a different color for each room_type. The size of each datapoint, known also as a bubble, is proportional to the number of listings. The bubbles are then plotted using geom_point. Let's also add labels and a classic theme to beautify the plot.5. Bubble plot with ggplot, rendered
Because we have many such pairs, the resulting plot is visually appealing but it is difficult to look at individual components.6. Interactive bubble plot with plotly
We can make the plot interactive by converting it into a plotly object. This is done so by encasing bubble in ggplotly. The resulting interactive plot is one that can be zoomed in to look at certain sectors. We can also hover the mouse over to obtain more information about each point plotted. In this case, we get information on the four variables described by each point.7. Horizontal boxplots with plotly
Some plots cannot be converted correctly and may require some workarounds. Let us demonstrate some of these problems with some examples, of which the first is that of boxplots. Let's create a series of horizontally oriented boxplots called boxH, and create one that describes the distribution of yearly availability with respect to each room type. To this end, we shall use geom_boxplot and add labels. While the plot renders correctly as a ggplot object, it will not be converted correctly with ggplotly.8. Vertical boxplots with plotly
We will instead require vertical boxplots, which we shall call boxV. This is obtained by switching the x and y arguments in aes. Now the Plotly conversion will proceed as intended.9. Fixing horizontal boxplots with plotly
What if we want horizontal box plots? This may be required, for instance, when each category label is very lengthy. This can be achieved via a coordinate flip by adding coord_flip to the vertically oriented boxplots. Using facet_wrap, we can also create facets that convert correctly, as long as the panel dimensions are large enough to accommodate each faceted plot. While there are some limitations with plotly, it provides us with a convenient means to produce an interactive chart.10. Let's practice!
Now it's your turn to create your own interactive plots with Plotly.Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.