在rmarkdown文档中居中leaflet地图

7
我想创建一个带有居中的leaflet小部件的HTML文档。
---
title: "Exemple"
author: "Antoine Bichat"
date: "31/08/2018"
output: html_document
---

```{r leaflet, fig.align="center"}
library(leaflet)

leaflet() %>%
  addTiles() %>% 
  addMarkers(lng=174.768, lat=-36.852, popup="The birthplace of R")
```

fig.align="center" 的第一种想法不起作用。

我找到了这个问题,并将其适应于我的情况:

<style>
/* resize the widget container */
.leaflet { 
  width: 80% !important;
}

/* center the widget */
div.leaflet-control-container {
  margin: auto !important;
}
</style>

但是,如果我不想让out.width为100%(我并不想这样做),它就不起作用了。

或者可能是我做错了... 实际上,我更喜欢一个100%的R/Rmarkdown解决方案,但我对一切都持开放态度 :)

1个回答

14

尝试使用CSS代码margin:auto来应用于.html-widget

---
title: "Exemple"
author: "Antoine Bichat"
date: "31/08/2018"
output: html_document
---

<style>
.html-widget {
    margin: auto;
}
</style>

```{r leaflet, fig.align="center"}
library(leaflet)

leaflet() %>%
  addTiles() %>% 
  addMarkers(lng=174.768, lat=-36.852, popup="The birthplace of R")
```

那将居中每个 htmlwidget。 您还可以放置:


在此输入图片描述
<style>
.leaflet {
    margin: auto;
}
</style>

仅为居中地图服务。


很棒的答案!对于使用leaflet依赖项的tmap也适用。 - Ariel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接