7.6 Interactive Plot with plotly()
Until now, all the plots we created are static. In this section, we will introduce a powerful tool named plotly() that can make interactive plots. If you haven’t done so, you need to first install the R package plotly.
7.6.1 Converting ggplot2 plots with ggplotly()
The easiest way to create interactive plots is to convert an existing ggplot2 plot using ggplotly(). Let’s start with a static scatterplot and make it interactive.
library(r02pro)
library(ggplot2)
library(plotly)
#>
#> Attaching package: 'plotly'
#> The following object is masked from 'package:ggplot2':
#>
#> last_plot
#> The following object is masked from 'package:stats':
#>
#> filter
#> The following object is masked from 'package:graphics':
#>
#> layout
my_plot <- ggplot(data = gm2004) +
geom_point(mapping = aes(x = sugar,
y = cholesterol))
ggplotly(my_plot)Upon a first look, this may look identical to a regular scatterplot. However, you can try to move your cursor to the points which will show the corresponding coordinates. Some other features offered are available via a bar of buttons on the top right of the plot. Some useful features include
- Download plot as png.
- Zoom: Zoom a region of the plot.
- Pan: Move the plot around.
7.6.2 Interactive plots with aesthetics
In addition to the vanilla scatterplots, you can use plotly with more complicated plots that involve aesthetics.
my_plot_continent <- ggplot(data = gm2004) +
geom_point(mapping = aes(x = sugar,
y = cholesterol,
color = continent))
ggplotly(my_plot_continent)Now, you can easily see the continent in addition to the sugar and cholesterol values for each data point.
7.6.3 Customizing hover text with tooltip
By default, ggplotly() displays all mapped aesthetics when you hover over a point. You can control which aesthetics appear in the tooltip using the tooltip argument.
my_plot_tooltip <- ggplot(data = gm2004) +
geom_point(mapping = aes(x = sugar,
y = cholesterol,
color = continent,
text = country))
ggplotly(my_plot_tooltip, tooltip = c("text", "x", "y"))Here, we mapped the country variable to the text aesthetic and told ggplotly() to only show text, x, and y in the tooltip. This is useful when you want to display additional information beyond the x and y coordinates.
7.6.4 Interactive boxplots
Let’s try to use plotly with some other types of plots.
my_box_plot <- ggplot(data = na.omit(sahp),
aes(x = house_style,
y = sale_price)) +
scale_x_discrete(limits=c("1Story", "2Story")) + geom_boxplot(mapping = aes(color = oa_qual > 5))
ggplotly(my_box_plot)For boxplots, we can easily see the values of summary statistics. You are welcome to try out plotly on other types of plots.
7.6.5 Saving interactive plots
Interactive plots created with plotly are HTML widgets. You can save them as standalone HTML files using the htmlwidgets::saveWidget() function.
library(htmlwidgets)
my_interactive <- ggplotly(my_plot)
saveWidget(my_interactive, "my_interactive_plot.html")The saved HTML file can be opened in any web browser and shared with others — no R installation required.
Interactive plots created with plotly only work in HTML output (e.g., the gitbook format). They will not render in PDF or other static formats. If you need to include plots in a PDF document, use static ggplot2 plots instead.
In this section, we introduced the plotly package for creating interactive plots. The ggplotly() function provides a convenient way to convert any static ggplot2 plot into an interactive one, enabling features like hovering to see data values, zooming, and downloading as PNG.
7.6.6 Exercises
Using the
sahpdataset from the r02pro package, create an interactive scatterplot ofliv_area(x-axis) vs.sale_price(y-axis) usingggplotly().Enhance the plot from Exercise 1 by mapping
house_styleto thecoloraesthetic. Hover over points to verify that the house style information is displayed.Create an interactive scatterplot of
sugarvs.cholesterolfrom thegm2004dataset, and customize the tooltip to display thecountryname along with the x and y values.Create an interactive violin plot of
cholesterolgrouped bycontinentusing thegm2004dataset. Restrict to three continents of your choice.Save the interactive plot from Exercise 3 as an HTML file named
"gm_interactive.html"usinghtmlwidgets::saveWidget().