admin管理员组文章数量:1399940
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 <;.
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;"
)
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>.
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;"
)
Share
Improve this question
asked Mar 25 at 10:51
adladl
1,4531 gold badge19 silver badges38 bronze badges
1 Answer
Reset to default 1You 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";
});
}
'
)
```
Result
本文标签: r markdownrflowmapblue impossible to set height and width in rmarkdownStack Overflow
版权声明:本文标题:r markdown - r - flowmapblue impossible to set height and width in rmarkdown - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744202359a2595025.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论