Same data

First create shared data. key argument specifies the variable based on which the match will be made.

aggSD<-SharedData$new(aggDataOrg, key=~cntry)

Two scatter plots

Very simple - two scatterplots. subplot does not look great in interactive window, but does work fine in shiny app.

gg1<-ggplot(aggSD, aes(y=imbgeco, x=ppltrst, name=cntry))+
  geom_point() 
gg1pl<-ggplotly(gg1, tooltip = "name")

gg2<-ggplot(aggSD, aes(x=ppltrst, y=happy, name=cntry))+
  geom_point() 
gg2pl<-ggplotly(gg2, tooltip = "name")

# problems with bottom margins in interactive window, works fine in Shiny app
subplot(gg1pl,gg2pl,margin=0.1,shareX=TRUE, nrows=2)%>%highlight(on = "plotly_selected", off="plotly_deselect", dynamic = TRUE)
## Adding more colors to the selection color palette.

The other alternative is to use bscols (below) or just independently position the two plots.

bscols(gg1pl %>% highlight(on = "plotly_selected", off = "plotly_deselect"),
       gg2pl %>% highlight(on = "plotly_selected", off = "plotly_deselect"))

Plots that aggregate

In principle, this works nicely when each “row” in the data is represented by one “point” on a graph. If thugs are aggregated, e.g. in a histogram, things do now work using ggplotly, as seen below.

aggSD<-SharedData$new(aggDataOrg, key=~cntry)
## Create ggplot objects and convert them with ggploty
gg3<-ggplot(aggSD, aes(x=ppltrst, text=cntry))+
  geom_histogram(binwidth = 0.5)
gg3pl<-ggplotly(gg3)
## ggplot histograms do not work with linked views as expected
bscols(gg1pl %>% highlight(on = "plotly_selected", off = "plotly_deselect"),
       gg2pl %>% highlight(on = "plotly_selected", off = "plotly_deselect"),
       gg3pl  %>% highlight(on = "plotly_selected", off = "plotly_deselect"))

However, they might work with graphs created with plot_ly.

## Alternative - create plotly objects
pl1<-plot_ly(data=aggSD, x=~ppltrst, y=~imbgeco, text=~cntry)
pl2<-plot_ly(data=aggSD, x=~ppltrst, y=~happy, text=~cntry)
pl3<-plot_ly(data=aggSD, x=~ppltrst) %>% add_histogram(xbins = list(start = 3.5, end = 8.5, size = 0.5)) %>% layout(barmode = "overlay")

## linked views also work with histograms
bscols(gg1pl%>% highlight(on = "plotly_selected", off = "plotly_relayout"),
       gg2pl%>% highlight(on = "plotly_selected", off = "plotly_relayout"),
       pl3%>% highlight(on = "plotly_selected", off = "plotly_relayout"))

Multiple graphs with the one function

Some functions create multiple connected graphs automaticaly, but my need shared data to be connected, e.g. ggpairs.

## pairs
aggSD2<-SharedData$new(aggDataOrg, key=~cntry)
ggpairs(aggSD2, columns = 2:4)%>%ggplotly()%>%  highlight(on = "plotly_hover", off = "plotly_doubleclick")
## Warning: Can only have one: highlight

## Warning: Can only have one: highlight

metricsgraphics

metricsgraphics is a relative “old” package that among other things supports linked plots.

## using metricsgraphics
g1<-mjs_plot(aggDataOrg, x=ppltrst, y=imbgeco, linked=TRUE, width=350) %>%
  mjs_point() %>% 
  mjs_add_mouseover("function(d, i) {
                $('{{ID}} svg .mg-active-datapoint')
                    .text(d.point.cntry);
                 }")

g2<-mjs_plot(aggDataOrg, x=ppltrst, y=happy, linked=TRUE, width=350) %>%
  mjs_point() %>% 
  mjs_add_mouseover("function(d, i) {
                $('{{ID}} svg .mg-active-datapoint')
                    .text(d.point.cntry);
                 }")

#the bellow does not work in Shiny
mjs_grid(g1, g2, ncol=2)
## Warning in widget_html(name = class(x)[1], package = attr(x, "package"), :
## metricsgraphics_html returned an object of class `list` instead of a
## `shiny.tag`.

## Warning in widget_html(name = class(x)[1], package = attr(x, "package"), :
## metricsgraphics_html returned an object of class `list` instead of a
## `shiny.tag`.

Two or more data frames usign shared key and a group

We can use also tow (or more) data frames, which however must share a the same key (variable that indetifies cases) and must be marked as belonging to the same group.

## two data objects with the same key forming the same group
dataOrgRegSDg1<-SharedData$new(dataOrgReg, key=~cntry,group = "g1")
aggSDg1<-SharedData$new(aggDataOrg, key=~cntry,group = "g1")
p1g1<-plot_ly(data=aggSDg1, x=~ppltrst, y=~imbgeco, text=~cntry, name=~cntry)
p2g1<-plot_ly(data=dataOrgRegSDg1, x=~ppltrst, y=~imbgeco, text=~cntry, name=~cntry)

bscols(p1g1%>% highlight(on = "plotly_hover"),
       p2g1%>% highlight(on = "plotly_hover"))
## No trace type specified:
##   Based on info supplied, a 'scatter' trace seems appropriate.
##   Read more about this trace type -> https://plotly.com/r/reference/#scatter
## No scatter mode specifed:
##   Setting the mode to markers
##   Read more about this attribute -> https://plotly.com/r/reference/#scatter-mode
## Setting the `off` event (i.e., 'plotly_doubleclick') to match the `on` event (i.e., 'plotly_hover'). You can change this default via the `highlight()` function.
## No trace type specified:
##   Based on info supplied, a 'scatter' trace seems appropriate.
##   Read more about this trace type -> https://plotly.com/r/reference/#scatter
## No scatter mode specifed:
##   Setting the mode to markers
##   Read more about this attribute -> https://plotly.com/r/reference/#scatter-mode
## Setting the `off` event (i.e., 'plotly_doubleclick') to match the `on` event (i.e., 'plotly_hover'). You can change this default via the `highlight()` function.

Geospatial data (and clasical plot)

One data frame

Here we use one data, but plot it on a map and as a scatterplot. Here we select units by clicking.

## Geo data and classical plots
nuts2ClDataSD<-SharedData$new(nuts2ClData, key=~CNTR_CODE)
ggMap<-ggplot(nuts2ClDataSD, aes(label=CNTR_CODE, fill=ppltrst))+
  geom_sf() #+ geom_sf_text()
plMap<-ggMap %>% ggplotly(tooltip = "label")%>% style(hoveron = "fill")%>% highlight()
ggPlot<-ggplot(nuts2ClDataSD, aes(label=CNTR_CODE,x=ppltrst, y=imbgeco, color=Region))+
  geom_point()
plPlot<-ggPlot%>% ggplotly(tooltip = "label")%>% highlight()
bscols(plMap, plPlot)
## Setting the `off` event (i.e., 'plotly_doubleclick') to match the `on` event (i.e., 'plotly_click'). You can change this default via the `highlight()` function.
## Setting the `off` event (i.e., 'plotly_doubleclick') to match the `on` event (i.e., 'plotly_click'). You can change this default via the `highlight()` function.

But we could also decide on different selection mechanism, we this does not work nice with geom_sf, so it is used only on scatterplot.

## Geo data and classical plots
plMap2<-ggMap %>% ggplotly(tooltip = "label")%>% style(hoveron = "fill")%>% highlight(on = 'plotly_click', off = 'plotly_relayout')
plPlot2<-ggPlot%>% ggplotly(tooltip = "label")%>% highlight(on = 'plotly_selected', off = 'plotly_relayout')
bscols(plMap2, plPlot2)

The key can identify larger groups of units.

## The same, different key
nuts2ClDataSDReg<-SharedData$new(nuts2ClData, key=~Region)
ggMapReg<-ggplot(nuts2ClDataSDReg, aes(label=CNTR_CODE, fill=ppltrst))+
  geom_sf() #+ geom_sf_text()
plMapReg<-ggMapReg %>% ggplotly(tooltip = "label")%>% style(hoveron = "fill")%>% highlight()
ggPlotReg<-ggplot(nuts2ClDataSDReg, aes(label=CNTR_CODE,x=ppltrst, y=imbgeco, color=Region))+
  geom_point()
plPlotReg<-ggPlotReg%>% ggplotly(tooltip = "label")%>% highlight()

bscols(plMapReg, plPlotReg)
## Setting the `off` event (i.e., 'plotly_doubleclick') to match the `on` event (i.e., 'plotly_click'). You can change this default via the `highlight()` function.
## Setting the `off` event (i.e., 'plotly_doubleclick') to match the `on` event (i.e., 'plotly_click'). You can change this default via the `highlight()` function.

Two data frames

## two data objects with the same key forming the same group
dataRegSDg2<-SharedData$new(dataOrgReg, key=~cntry,group = "g2")
nuts2ClDataSDg2<-SharedData$new(nuts2ClData, key=~cntry, group="g2")

ggMapG2<-ggplot(nuts2ClDataSDg2, aes(label=CNTR_CODE, fill=ppltrst))+
  geom_sf() #+ geom_sf_text()
plMapG2<-ggMapG2 %>% ggplotly(tooltip = "label")%>% style(hoveron = "fill")%>% highlight()


p2g2<-plot_ly(data=dataRegSDg2, x=~ppltrst, y=~imbgeco, text=~cntry, name=~cntry)

bscols(plMapG2%>% highlight(on = "plotly_click"),
       p2g2%>% highlight(on = "plotly_click"))
## Setting the `off` event (i.e., 'plotly_doubleclick') to match the `on` event (i.e., 'plotly_click'). You can change this default via the `highlight()` function.
## No trace type specified:
##   Based on info supplied, a 'scatter' trace seems appropriate.
##   Read more about this trace type -> https://plotly.com/r/reference/#scatter
## No scatter mode specifed:
##   Setting the mode to markers
##   Read more about this attribute -> https://plotly.com/r/reference/#scatter-mode
## Setting the `off` event (i.e., 'plotly_doubleclick') to match the `on` event (i.e., 'plotly_click'). You can change this default via the `highlight()` function.