Use dropdown menu to print different ggplotly plots in Rmarkdown

I have a series of data with this format:

GeneFam s0 Prev Yvar
fam_VO056_k119_707118_2 -2.94891455 1.897019 Latitude_c
fam_VO118_k119_1069373_4 -2.63244195 1.897019 Latitude_c
fam_VO015_k119_739705_2 -2.54050825 1.897019 Aridity_Index_v3
fam_VO203_k119_389361_1 -1.25734198 4.336043 MAT_wc2
fam_VO036_k119_254678_4 -1.15872311 1.897019 MAT_wc2

Up to 13 different values for Yvar.

I am trying to plot 13 plots, one for each Yvar value, with GeneFam in x axis, s0 in y axis, and bars filled by Prev gradient:

ple_wts <- ggplot(mydata, aes(x = s0, y = reorder(GeneFam, s0), fill = Prev)) +
  geom_bar(stat = "identity") +
  scale_fill_gradientn(colours = c("skyblue", "dodgerblue3", "brown1", "firebrick4")) +
  facet_wrap(~Yvar, scales = "free")

But I want to plot them interactively, using plotly or something similar, so each Yvar facet appears individually when selecting it from a dropdown menu.

I am working in Rmarkdown and I want the output to be an html. The initial markdown parameters are:

---
title: "<span style="font-size: 25px"><b>Something</b></span>"
subtitle: "<span style="font-size: 22px"><u>Something 2</u></span>"
author: "Myname"
date: '`r format(Sys.time(), "%d/%m/%Y")`'
output:
  rmdformats::material:
    highlight: kate
    self_contained: yes
    fig_caption: yes
    fig_height: 8
    fig_width: 12
    thumbnails: false
    lightbox: true
---

How can I achieve this?

Note: It does not need to be a facet-based plot, it can also be a list of ggplots if needed.

  • 1

    I would use a carousel (slideshow) with the swipeR package. You can select the plot with bullets, not a dropdown menu.

    – 

  • I tried with the sample command they have at github.com/stla/swipeR. The carousel shows the navigation arrows but the plot frame is always blank…

    – 

Leave a Comment