I found this really useful and nice looking way to plot flows using the flowmapblue
package in R, but when i try to plot multiple plots in an html rmarkdown and render, the widget automatically spreads over the whole document instead of staying within boundaries.
My example:
---
title: "test"
output: html_document
date: "2025-03-25"
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
mat <- "hidden"
```
## R Markdown
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
```{r cars}
summary(cars)
```
## Including Plots
You can also embed plots, for example:
```{r , fig.width=8, fig.height=5, echo=FALSE}
library(flowmapblue)
# load locations and flows for Switzerland
data(ch_locations)
data(ch_flows)
library(htmltools)
```
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
```{r}
library(htmlwidgets)
# Create the map widget
flowmap <- flowmapblue(
ch_locations,
ch_flows,
mapboxAccessToken = mat,
clustering = TRUE,
darkMode = TRUE,
animation = FALSE
)
flowmap
```
The end result looks like below, so the intermediate text and chapters disappear behind the map. Can the width/height be somehow forced onto the visual ?
I tried with this approach but nothing happens:
htmltools::div(
flowmapblue(locations, flows, flowmap_style = "light"),
style = "width: 800px; height: 600px;"
)
You can add htmlwidgets::onRender()
to reset the div's of class .css-1bqwm27
' position to be relative
. The problem is, that they are absolute, which fills the entire page. You also have to set the body's background color
because apparently flowmap makes it black. Finally we should set the div's width of the map-bounding containers with class .html-widget-static-bound
to be 900px, otherwise the maps will not fill the whole width - but you can provide your own value here. Use forEach
to apply the style to all objects within the HTML. This needs to be done, since you have multiple flowmaps. You want to add this Javascript to the last call of flowmapblue
within your RMarkdown.
---
title: "Flowmap"
output: html_document
date: "2025-03-25"
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
mat <- "hidden"
library(htmlwidgets)
library(flowmapblue)
```
## Flowmap
```{r, echo = FALSE}
# Create the map widget
flowmapblue(
ch_locations,
ch_flows,
mapboxAccessToken = mat,
clustering = TRUE,
darkMode = TRUE,
animation = FALSE
)
```
## Flowmap 2
```{r, echo = FALSE}
# Create the map widget
flowmapblue(
ch_locations,
ch_flows,
mapboxAccessToken = mat,
clustering = TRUE,
darkMode = TRUE,
animation = FALSE
) |> htmlwidgets::onRender('
function(el, x) {
// Set body background to white
document.querySelector("body").style.backgroundColor = "white";
// Select all elements with .css-1bqwm27 class and set position to relative
document.querySelectorAll(".css-1bqwm27").forEach(div => {
div.style.position = "relative";
});
// Select all elements with .html-widget-static-bound class and set width to 900px
document.querySelectorAll(".html-widget-static-bound").forEach(outdiv => {
outdiv.style.width = "900px";
});
}
'
)
```