I am trying to use a loop in Rmarkdown that prints a leaflet map and plotly graph side by side (the loop iterates through vegetation types). I am having trouble getting the side-by-side to have the height and width I desire.
In the joint plot I would like to have the leaflet and plotly with two different sizes. Any suggestions?
Both chunks use the following heading: {r, echo=F, warning=F, message=F, fig.height=12, fig.width=20)
The markdown header is such:
title: "test"
date: "February 7, 2021"
output:
html_document:
toc: true
toc_float:
collapsed: false
smooth_scroll: false
latex_engine: xelatex
header-includes:
- \usepackage{fontspec}
- \setmainfont{Georgia}
fig.width: 16
fig.height: 12
library(leaflet)
library(plotly)
library(leafsync)
library(htmltools)
leaflet( ) %>%
addTiles(options = tileOptions(noWrap = TRUE)) %>%
fitBounds(lng1 = 120, lat1 = -12, lng2 = 130, lat2 = -42) %>%
addProviderTiles(providers$Esri.WorldImagery, group = "Esri World Imagery")
maps <- lapply(c(1,2),function(x){
print(x)
p = leaflet( ) %>% # width=500, height=500
addTiles(options = tileOptions(noWrap = TRUE)) %>%
fitBounds(lng1 = 120, lat1 = -12, lng2 = 130, lat2 = -42) %>%
addProviderTiles(providers$Esri.WorldImagery, group = "Esri World Imagery")
p2 <- plot_ly() %>% layout(title = x)
p2 <- plotly::add_trace(p2, x = c(1, 2, 3), y = c(4, 3, 2), mode='lines')
p2 <- plotly::add_trace(p2, x = c(20, 30, 40), y = c(30, 40, 50), xaxis='x2', yaxis='y2', mode='lines')
# inset
p2 <- plotly::layout(p2,
xaxis2 = list(domain = c(0.7, 0.95), anchor='y2'),
yaxis2 = list(domain = c(0.7, 0.95), anchor='x2'))
leafsync::latticeview(p,p2)
})
htmltools::tagList(maps )
Here's a partial screen grab (just shows that the standalone leaflet chunk is creating a map per the height and width in the header. The joint leaflet-plotly does not reflect the height-width, and is coming out to small.)
In the joint plot I would like to have the leaflet and plotly with two different sizes. Any suggestions?
