rggplot2plotlyhtmlwidgets

Disable interactivity in Plotly chart


I have the following data (dotplot_data)

structure(list(Kanton = structure(c(8L, 8L, 13L, 13L, 18L, 18L, 
25L, 25L, 17L, 17L, 24L, 24L, 20L, 20L, 19L, 19L, 5L, 5L, 22L, 
22L, 7L, 7L, 16L, 16L, 4L, 4L, 9L, 9L, 21L, 21L, 26L, 26L, 14L, 
14L, 23L, 23L, 15L, 15L, 11L, 11L, 6L, 6L, 3L, 3L, 10L, 10L, 
2L, 2L, 1L, 1L, 12L, 12L), .Label = c("GE", "NE", "VD", "BL", 
"ZG", "TI", "SO", "ZH", "SH", "VS", "TG", "JU", "BE", "SG", "AG", 
"BS", "SZ", "LU", "GL", "NW", "AR", "FR", "GR", "OW", "UR", "AI"
), class = "factor"), Jahr = structure(c(1L, 7L, 1L, 7L, 1L, 
7L, 1L, 7L, 1L, 7L, 1L, 7L, 1L, 7L, 1L, 7L, 1L, 7L, 1L, 7L, 1L, 
7L, 1L, 7L, 1L, 7L, 1L, 7L, 1L, 7L, 1L, 7L, 1L, 7L, 1L, 7L, 1L, 
7L, 1L, 7L, 1L, 7L, 1L, 7L, 1L, 7L, 1L, 7L, 1L, 7L, 1L, 7L), .Label = c("2009", 
"2010", "2011", "2012", "2013", "2014", "2015"), class = "factor"), 
    Verhältnis = c(0.18245716727989, 0.234567735045611, 0.24932014665891, 
    0.282991259307219, 0.291667314781689, 0.34810778972169, 0.434262510974539, 
    0.47438822065533, 0.290624277074589, 0.333502152443657, 0.405097486492835, 
    0.444295001120825, 0.318638617178079, 0.366800362271963, 
    0.344886447973876, 0.366559250036619, 0.158794683184927, 
    0.200541172129635, 0.310803517571986, 0.373802102210946, 
    0.17589414669752, 0.223276715892796, 0.29494992542084, 0.306316282635547, 
    0.194661570513323, 0.197384284340806, 0.193617140613607, 
    0.236622748050551, 0.33538817183587, 0.367132867132867, 0.444847450394709, 
    0.511295616914462, 0.243694965319972, 0.283000330472218, 
    0.326857179802159, 0.380211750671041, 0.253870012888241, 
    0.301849570094465, 0.213009063138175, 0.262879934417959, 
    0.166988156513917, 0.20723066336873, 0.121289379668724, 0.160165035560148, 
    0.214926378302262, 0.262849338486695, 0.125824437342762, 
    0.145818371743814, 0.0331743010407139, 0.0607599044903447, 
    0.229837043837503, 0.277344272528649), GDEKT = c("ZH", "ZH", 
    "BE", "BE", "LU", "LU", "UR", "UR", "SZ", "SZ", "OW", "OW", 
    "NW", "NW", "GL", "GL", "ZG", "ZG", "FR", "FR", "SO", "SO", 
    "BS", "BS", "BL", "BL", "SH", "SH", "AR", "AR", "AI", "AI", 
    "SG", "SG", "GR", "GR", "AG", "AG", "TG", "TG", "TI", "TI", 
    "VD", "VD", "VS", "VS", "NE", "NE", "GE", "GE", "JU", "JU"
    ), Verhältnis_label = c("   18.2%", "   23.5%", "   24.9%", 
    "   28.3%", "   29.2%", "   34.8%", "   43.4%", "   47.4%", 
    "   29.1%", "   33.4%", "   40.5%", "   44.4%", "   31.9%", 
    "   36.7%", "   34.5%", "   36.7%", "   15.9%", "   20.1%", 
    "   31.1%", "   37.4%", "   17.6%", "   22.3%", "   29.5%", 
    "   30.6%", "   19.5%", "   19.7%", "   19.4%", "   23.7%", 
    "   33.5%", "   36.7%", "   44.5%", "   51.1%", "   24.4%", 
    "   28.3%", "   32.7%", "   38%", "   25.4%", "   30.2%", 
    "   21.3%", "   26.3%", "   16.7%", "   20.7%", "   12.1%", 
    "   16%", "   21.5%", "   26.3%", "   12.6%", "   14.6%", 
    "   3.3%", "   6.1%", "   23%", "   27.7%"), Kanton_label = c("ZH 18.2%   ", 
    "ZH 23.5%   ", "BE 24.9%   ", "BE 28.3%   ", "LU 29.2%   ", 
    "LU 34.8%   ", "UR 43.4%   ", "UR 47.4%   ", "SZ 29.1%   ", 
    "SZ 33.4%   ", "OW 40.5%   ", "OW 44.4%   ", "NW 31.9%   ", 
    "NW 36.7%   ", "GL 34.5%   ", "GL 36.7%   ", "ZG 15.9%   ", 
    "ZG 20.1%   ", "FR 31.1%   ", "FR 37.4%   ", "SO 17.6%   ", 
    "SO 22.3%   ", "BS 29.5%   ", "BS 30.6%   ", "BL 19.5%   ", 
    "BL 19.7%   ", "SH 19.4%   ", "SH 23.7%   ", "AR 33.5%   ", 
    "AR 36.7%   ", "AI 44.5%   ", "AI 51.1%   ", "SG 24.4%   ", 
    "SG 28.3%   ", "GR 32.7%   ", "GR 38%   ", "AG 25.4%   ", 
    "AG 30.2%   ", "TG 21.3%   ", "TG 26.3%   ", "TI 16.7%   ", 
    "TI 20.7%   ", "VD 12.1%   ", "VD 16%   ", "VS 21.5%   ", 
    "VS 26.3%   ", "NE 12.6%   ", "NE 14.6%   ", "GE 3.3%   ", 
    "GE 6.1%   ", "JU 23%   ", "JU 27.7%   "), hjust = c(1.2, 
    0, 1.2, 0, 1.2, 0, 1.2, 0, 1.2, 0, 1.2, 0, 1.2, 0, 1.2, 0, 
    1.2, 0, 1.2, 0, 1.2, 0, 1.2, 0, 1.2, 0, 1.2, 0, 1.2, 0, 1.2, 
    0, 1.2, 0, 1.2, 0, 1.2, 0, 1.2, 0, 1.2, 0, 1.2, 0, 1.2, 0, 
    1.2, 0, 1.2, 0, 1.2, 0)), .Names = c("Kanton", "Jahr", "Verhältnis", 
"GDEKT", "Verhältnis_label", "Kanton_label", "hjust"), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -52L))

With that data, I am producing the following dot plot with ggplot2.

enter image description here

Code:

p <- ggplot(dotplot_data) +
  geom_path_interactive(aes(x = Verhältnis, y = Kanton, group = Kanton), colour = "#AAAAAA"
            # arrow = arrow(length = unit(0.2, "cm"), type = "closed")
            ) +
  geom_point(aes(x = Verhältnis, y = Kanton, colour = Jahr)) +
  geom_text(data = subset(dotplot_data, Jahr == 2015),
            aes(x = Verhältnis, y = Kanton, label = Verhältnis_label),  
            hjust = 0, colour = "#555555", size = 4) +
  geom_text(data = subset(dotplot_data, Jahr == 2009), 
            aes(x = Verhältnis, y = Kanton, label = Kanton_label),
            hjust = 1, colour = "#555555", size = 4) +
  scale_x_continuous(labels = percent, limits = c(-.05,.6)) +
  scale_colour_discrete(guide = F) +
  labs(x = NULL,
    y = NULL,
   title = "Anteil umweltfreundlicher Heizungen",
   subtitle = "2009, 2015",
   caption = "") +
  xlim(c(-.05, .6)) + 
  theme_minimal(
    # axis.text = element_blank(),
    # axis.ticks = element_blank()
  )
ggsave(p, filename = "output/dotplot.png", width = 7, height = 5)

Now I want to save that chart as (responsive) HTML, so I can include it both on Desktop and on mobile devices (where it will shrink horizontally). I came across the plotly package as it produces plots that are automatically resized (but not 1:1 as, for example, in ggiraph).

Actually, I only use it for that purpose. I don't know if it's the best tool for that or if there are way better solutions. Something that turns me off, too, is the quite big library src file that comes with every plot (~2MB).

Anyhow, here's the code:

ply <- ggplotly(p, collaborate = F, doubleClick = F, displayModeBar = F)
plotly::config(ply, collaborate = F, doubleClick = F, displayModeBar = F)
saveWidget(ply, "test.html", libdir = "src", selfcontained = F)

With that I get a plotly html file which is responsive but has tooltips and that ugly bar at the top right corner, and I wonder if I can't just disable all the interactive features of the plot. As you can see, I already tried to use some config options but somehow they don't work or I am using them the wrong way.

Any direction - also to other packages or workflows - is greatly appreciated.


Solution

  • Disabling legend toggle in Plotly is currently not possible but you can do it manually.

    prependContent(htmltools::tags$style('
    .plot-container .legend .traces .legendtoggle {display: none;}
    .plot-container .legend .traces .legendtext {cursor: default;}'))             
    

    You got the plotly::config call right but it would need to be assigned to the plotly object in order to save the changes.

    ply <- plotly::config(ply, collaborate = F, doubleClick = F, displayModeBar = F)
    

    For aesthetic reasons, let's move the labels further away from the lines and remove the useless hoverinfo

    ply[['x']][['data']][[5]][['x']] <- ply[['x']][['data']][[5]][['x']] - 0.05
    ply[['x']][['data']][[4]][['x']] <- ply[['x']][['data']][[4]][['x']] + 0.05
    ply[['x']][['data']][[4]][['hoverinfo']] <- 'none'
    ply[['x']][['data']][[5]][['hoverinfo']] <- 'none'
    

    Concerning the size of the Plotly Javascript library, you could point to https://cdn.plot.ly/plotly-latest.min.js instead of supplying it yourself, hopefully one of the users visited a site before which was using Plotly and the browser still has it in the cache.


    enter image description here

    library(plotly)
    library(ggiraph)
    library(htmlwidgets)
    
    dotplot_data <- structure(list(Kanton = structure(c(8L, 8L, 13L, 13L, 18L, 18L, 
                                        25L, 25L, 17L, 17L, 24L, 24L, 20L, 20L, 19L, 19L, 5L, 5L, 22L, 
                                        22L, 7L, 7L, 16L, 16L, 4L, 4L, 9L, 9L, 21L, 21L, 26L, 26L, 14L, 
                                        14L, 23L, 23L, 15L, 15L, 11L, 11L, 6L, 6L, 3L, 3L, 10L, 10L, 
                                        2L, 2L, 1L, 1L, 12L, 12L), .Label = c("GE", "NE", "VD", "BL", 
                                                                              "ZG", "TI", "SO", "ZH", "SH", "VS", "TG", "JU", "BE", "SG", "AG", 
                                                                              "BS", "SZ", "LU", "GL", "NW", "AR", "FR", "GR", "OW", "UR", "AI"
                                        ), class = "factor"), Jahr = structure(c(1L, 7L, 1L, 7L, 1L, 
                                                                                 7L, 1L, 7L, 1L, 7L, 1L, 7L, 1L, 7L, 1L, 7L, 1L, 7L, 1L, 7L, 1L, 
                                                                                 7L, 1L, 7L, 1L, 7L, 1L, 7L, 1L, 7L, 1L, 7L, 1L, 7L, 1L, 7L, 1L, 
                                                                                 7L, 1L, 7L, 1L, 7L, 1L, 7L, 1L, 7L, 1L, 7L, 1L, 7L, 1L, 7L), .Label = c("2009", 
                                                                                                                                                         "2010", "2011", "2012", "2013", "2014", "2015"), class = "factor"), 
                   Verhältnis = c(0.18245716727989, 0.234567735045611, 0.24932014665891, 
                                  0.282991259307219, 0.291667314781689, 0.34810778972169, 0.434262510974539, 
                                  0.47438822065533, 0.290624277074589, 0.333502152443657, 0.405097486492835, 
                                  0.444295001120825, 0.318638617178079, 0.366800362271963, 
                                  0.344886447973876, 0.366559250036619, 0.158794683184927, 
                                  0.200541172129635, 0.310803517571986, 0.373802102210946, 
                                  0.17589414669752, 0.223276715892796, 0.29494992542084, 0.306316282635547, 
                                  0.194661570513323, 0.197384284340806, 0.193617140613607, 
                                  0.236622748050551, 0.33538817183587, 0.367132867132867, 0.444847450394709, 
                                  0.511295616914462, 0.243694965319972, 0.283000330472218, 
                                  0.326857179802159, 0.380211750671041, 0.253870012888241, 
                                  0.301849570094465, 0.213009063138175, 0.262879934417959, 
                                  0.166988156513917, 0.20723066336873, 0.121289379668724, 0.160165035560148, 
                                  0.214926378302262, 0.262849338486695, 0.125824437342762, 
                                  0.145818371743814, 0.0331743010407139, 0.0607599044903447, 
                                  0.229837043837503, 0.277344272528649), GDEKT = c("ZH", "ZH", 
                                                                                   "BE", "BE", "LU", "LU", "UR", "UR", "SZ", "SZ", "OW", "OW", 
                                                                                   "NW", "NW", "GL", "GL", "ZG", "ZG", "FR", "FR", "SO", "SO", 
                                                                                   "BS", "BS", "BL", "BL", "SH", "SH", "AR", "AR", "AI", "AI", 
                                                                                   "SG", "SG", "GR", "GR", "AG", "AG", "TG", "TG", "TI", "TI", 
                                                                                   "VD", "VD", "VS", "VS", "NE", "NE", "GE", "GE", "JU", "JU"
                                  ), Verhältnis_label = c("   18.2%", "   23.5%", "   24.9%", 
                                                          "   28.3%", "   29.2%", "   34.8%", "   43.4%", "   47.4%", 
                                                          "   29.1%", "   33.4%", "   40.5%", "   44.4%", "   31.9%", 
                                                          "   36.7%", "   34.5%", "   36.7%", "   15.9%", "   20.1%", 
                                                          "   31.1%", "   37.4%", "   17.6%", "   22.3%", "   29.5%", 
                                                          "   30.6%", "   19.5%", "   19.7%", "   19.4%", "   23.7%", 
                                                          "   33.5%", "   36.7%", "   44.5%", "   51.1%", "   24.4%", 
                                                          "   28.3%", "   32.7%", "   38%", "   25.4%", "   30.2%", 
                                                          "   21.3%", "   26.3%", "   16.7%", "   20.7%", "   12.1%", 
                                                          "   16%", "   21.5%", "   26.3%", "   12.6%", "   14.6%", 
                                                          "   3.3%", "   6.1%", "   23%", "   27.7%"), Kanton_label = c("ZH 18.2%   ", 
                                                                                                                        "ZH 23.5%   ", "BE 24.9%   ", "BE 28.3%   ", "LU 29.2%   ", 
                                                                                                                        "LU 34.8%   ", "UR 43.4%   ", "UR 47.4%   ", "SZ 29.1%   ", 
                                                                                                                        "SZ 33.4%   ", "OW 40.5%   ", "OW 44.4%   ", "NW 31.9%   ", 
                                                                                                                        "NW 36.7%   ", "GL 34.5%   ", "GL 36.7%   ", "ZG 15.9%   ", 
                                                                                                                        "ZG 20.1%   ", "FR 31.1%   ", "FR 37.4%   ", "SO 17.6%   ", 
                                                                                                                        "SO 22.3%   ", "BS 29.5%   ", "BS 30.6%   ", "BL 19.5%   ", 
                                                                                                                        "BL 19.7%   ", "SH 19.4%   ", "SH 23.7%   ", "AR 33.5%   ", 
                                                                                                                        "AR 36.7%   ", "AI 44.5%   ", "AI 51.1%   ", "SG 24.4%   ", 
                                                                                                                        "SG 28.3%   ", "GR 32.7%   ", "GR 38%   ", "AG 25.4%   ", 
                                                                                                                        "AG 30.2%   ", "TG 21.3%   ", "TG 26.3%   ", "TI 16.7%   ", 
                                                                                                                        "TI 20.7%   ", "VD 12.1%   ", "VD 16%   ", "VS 21.5%   ", 
                                                                                                                        "VS 26.3%   ", "NE 12.6%   ", "NE 14.6%   ", "GE 3.3%   ", 
                                                                                                                        "GE 6.1%   ", "JU 23%   ", "JU 27.7%   "), hjust = c(1.2, 
                                                                                                                                                                             0, 1.2, 0, 1.2, 0, 1.2, 0, 1.2, 0, 1.2, 0, 1.2, 0, 1.2, 0, 
                                                                                                                                                                             1.2, 0, 1.2, 0, 1.2, 0, 1.2, 0, 1.2, 0, 1.2, 0, 1.2, 0, 1.2, 
                                                                                                                                                                             0, 1.2, 0, 1.2, 0, 1.2, 0, 1.2, 0, 1.2, 0, 1.2, 0, 1.2, 0, 
                                                                                                                                                                             1.2, 0, 1.2, 0, 1.2, 0)), .Names = c("Kanton", "Jahr", "Verhältnis", 
                                                                                                                                                                                                                  "GDEKT", "Verhältnis_label", "Kanton_label", "hjust"), class = c("tbl_df", 
                                                                                                                                                                                                                                                                                   "tbl", "data.frame"), row.names = c(NA, -52L))
    
    
    p <- ggplot(dotplot_data) +
      geom_path_interactive(aes(x = Verhältnis, y = Kanton, group = Kanton), colour = "#AAAAAA"
                            # arrow = arrow(length = unit(0.2, "cm"), type = "closed")
      ) +
      geom_point(aes(x = Verhältnis, y = Kanton, colour = Jahr)) +
      geom_text(data = subset(dotplot_data, Jahr == 2015),
                aes(x = Verhältnis, y = Kanton, label = Verhältnis_label),  
                hjust = 0, colour = "#555555", size = 4) +
      geom_text(data = subset(dotplot_data, Jahr == 2009), 
                aes(x = Verhältnis, y = Kanton, label = Kanton_label),
                hjust = 1, colour = "#555555", size = 4) +
      scale_x_continuous(labels = percent, limits = c(-.05,.6)) +
      scale_colour_discrete(guide = F) +
      labs(x = NULL,
           y = NULL,
           title = "Anteil umweltfreundlicher Heizungen",
           subtitle = "2009, 2015",
           caption = "") +
      xlim(c(-.05, .6)) + 
      theme_minimal(
        # axis.text = element_blank(),
        # axis.ticks = element_blank()
      )
    p
    ply <- ggplotly(p) %>% 
    prependContent(htmltools::tags$style('
    .plot-container .legend .traces .legendtoggle {display: none;}
    .plot-container .legend .traces .legendtext {cursor: default;}')
                   )
    ply <- plotly::config(ply, collaborate = F, doubleClick = F, displayModeBar = F)
    ply[['x']][['data']][[5]][['x']] <- ply[['x']][['data']][[5]][['x']] - 0.05
    ply[['x']][['data']][[4]][['x']] <- ply[['x']][['data']][[4]][['x']] + 0.05
    ply[['x']][['data']][[4]][['hoverinfo']] <- 'none'
    ply[['x']][['data']][[5]][['hoverinfo']] <- 'none'
    ply
    saveWidget(ply, "test.html", libdir = "src", selfcontained = F)