The idea is about using package plotly
for producing interactive plots for a Shiny application.
Official documentation about plotly
is superb. Nevertheless, as this graphics grammar, within the R
community is not as spread as ggplot
is, few topics require a bit of extra research.
Given
x, y , w, z
x
and y
onto pl1
w
and z
onto pl2
pl
as a subplot of pl1
and pl2
I would like to:
pl1
I will be using the classic iris
data for this example.
Load the required packages
Add a unique id to data
Transform data into a ShareData
object
data <- data %>% highlight_key(~id)
Build your plots as you would normally do
pl1 <- plot_ly(data,
x = ~ Sepal.Length, y = ~Sepal.Width,
type = 'scatter', mode = 'markers',
marker = list ( color = 'green')) %>%
layout(showlegend=F)
pl2 <- plot_ly(data,
x = ~Petal.Length, y = ~Petal.Width,
type = 'scatter', mode = 'markers',
marker = list ( color = 'blue')) %>%
layout(showlegend=F)
Bind your plots into a single subplot
pl <- subplot(pl1, pl2, nrows = 2)
Link the plots
pl <- pl %>%
highlight(on = "plotly_select", off = 'plotly_doubleclick',
opacityDim = .5,
selected = attrs_selected(marker = list ( size = 20, color = 'orange')))
Display results
pl
If you see mistakes or want to suggest changes, please create an issue on the source repository.
For attribution, please cite this work as
Spano (2021, April 4). andreaspano blog: Interactive Plotly. Retrieved from https://andreaspano.github.io/posts/2021-02-27-interactive-plotly/
BibTeX citation
@misc{spano2021interactive, author = {Spano, Andrea}, title = {andreaspano blog: Interactive Plotly}, url = {https://andreaspano.github.io/posts/2021-02-27-interactive-plotly/}, year = {2021} }