Multiple leaflets in a grid

对着背影说爱祢 提交于 2019-12-10 16:24:52

问题


I have 4 leaflet objects: A, B, C, D. I would like to plot them in a 2 by 2 grid, but I have been having a difficult time trying to do this.

My initial thought was to use ggplot and facet_grid, but ggplot does not know how to deal with objects of class leaflet.

I would appreciate the help!


回答1:


Leaflets (or any other htmlwidgets) can be combined with htmltools::tagList.

In this case, a simple html table can handle the layout:

library(htmltools)

leaflet_grid <- 
  tagList(
    tags$table(width = "100%",
      tags$tr(
        tags$td(A),
        tags$td(B)
      ),
      tags$tr(
        tags$td(C),
        tags$td(D)
      )
    )
  )

You can put the leaflet_grid in knitr chunk directly or use

browsable(leaflet_grid)

to render it from the console.

Using Shiny fluid page layout

Example with shiny fluid page layout functions:

library(shiny)

leaflet_grid_2 <- fluidPage(
  fluidRow(
    column(6, A), column(6, B) 
  ),
  fluidRow(
    column(6, C), column(6, D) 
  )
)

Using mapview

library(mapview)

To synchronise zoom on all panels, use sync:

sync(A, B, C, D)

And latticeView will create panels without synchronising

latticeView(A, B, C, D)

(see https://r-spatial.github.io/mapview/articles/articles/mapview_05-extras.html)



来源:https://stackoverflow.com/questions/45175040/multiple-leaflets-in-a-grid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!