Search code examples
rggplotlychord-diagram

How to plot ggplotly and class "c('chorddiag', 'htmlwidget')" plots together


I am trying to plot the following two graphs chord and plt_interactive

library(igraph)
library(tidygraph)
library(chorddiag)
library(ggplot2)
m <- matrix(c(11975,  5871, 8916, 2868,
               1951, 10048, 2060, 6171,
               8010, 16145, 8090, 8045,
               1013,   990,  940, 6907),
               byrow = TRUE,
               nrow = 4, ncol = 4)
groupnames <- c("black", "blonde", "brown", "red")
row.names(m) <- groupnames
colnames(m) <- groupnames

chord= chorddiag(m,type = "bipartite")

plt <- ggplot(mtcars) + geom_bar(aes(x= factor(cyl)))+ coord_flip()
plt_interactive <- ggplotly(plt)
  1. I used to do the following code for two ggplotly graphs:

    plotly::subplot(f3, f4, nrows=2, heights = c(0.1, 0.9), margin = c(0,0,0.1,0))

But now I was not sure how to combine those two into subplots as they are different types of graph

  1. I also tried the patchwork library and do f3 + f4 but it did now work either

  2. I also tried cowplot library and do plot_grid(f3, f4) with no luck

Any help is very appreciated! enter image description here


Solution

  • You can glue the "plots" together using htmltools::browsable. Try this:

    library(htmltools)
    browsable(
      tagList(list(
        tags$div(
          #style = 'width:80%;display:block;float:left;',
          plt_interactive
        ),
        tags$div(
          #style = 'width:20%;display:block;float:left;',
          chord
        )
      ))
    )
    

    An alternative approach would be to use crosstalk::bscols(plt_interactive, chord).