library(tidyverse)
<- readRDS("data/coretta2022/glot_status.rds") glot_status
Interactive plotting with Plotly
Learn how to create interactive plots with Plotly
1 Overview
Plotly is an Open Source graphing system, with interfaces to major languages like Python and R.
The plotly R package allows you to make your ggplot2 plots interactive, or you can always create interactive plots with plotly directly.
This tutorial will show a simple example of each approach, but we refer you to the R package documentation for a full tour.
2 Add interactivity to a ggplot2 plot
Let’s recreate the Aggregated Endangerment Status (AES) plot from Bar charts.
First let’s read the data.
Then let’s create the plot. This time we will plot all macro-areas and we need to save the ggplot()
outout to a variable.
<- glot_status |>
aes_plot ggplot(aes(x = Macroarea, fill = status)) +
geom_bar(position = "fill") +
labs(
x = "Macro-area",
y = "Proportion",
fill = "Endangerment"
)
Finally, we can use the ggplotly()
function from the plotly package to make our ggplot2 plot interactive!
ggplotly(aes_plot)
3 Use plot_ly()
to create interactive plots
The alternative approach is to use plot_ly()
from plotly to create more complex graphs. A typical example of when you might want to use plot_ly()
is when making 3D scatter plots.
Let’s make a 3D formant plot of the Albanian vowels. First, the data
<- readRDS("data/coretta2021/alb_formants.rds")
alb_formants
alb_formants
Here’s the code to make a 3D scatter plot with plotly. For a detailed explanation, see the package documentation!
|>
alb_formants plot_ly(
x = ~F1, y = ~F2, z = ~F3, color = ~vowel, text = ~vowel,
marker = list(size = 5, opacity = 0.7),
hovertemplate = paste("<b>%{text}</b>", "<br>F1: %{x:.1r}", "<br>F2: %{y:.1r}", "<br>F3: %{z:.1r}")
)