Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adjust size of leaflet map in rmarkdown html

I'd like to change the height and width of leaflet map outputs in html document. Is there a simple way to do this in R markdown without getting into whole CSS business?

```{r}
library(leaflet)
library(dplyr)

m <- leaflet() %>% setView(lng = -71.0589, lat = 42.3601, zoom = 12)
m %>% addTiles() 
```

Ideally, I want the width of map to be the same width of code block as shown below.

enter image description here

like image 277
Ken Avatar asked Feb 01 '16 16:02

Ken


2 Answers

I found that changing fig.width and fig.height did not change the size of my leaflet maps. However, changing a different parameter/option did.

Try changing the width/height using this in the header for the code chunk:

{r, width = 40, height = 30}

Or alternatively, another thing that has worked for me is to use this (in this case, do not put anything in the chunk header:

m <- leaflet(width = "100%") %>%

like image 174
Julia Silge Avatar answered Sep 20 '22 19:09

Julia Silge


This works fine:

SomeData %>% 
  leaflet(height=2000, width=2000) %>% 
  addTiles() %>% 
  addMarkers(clusterOption=markerClusterOptions())

Here SomeData is a dataframe containing columns: Lat and Long.

like image 35
cs0815 Avatar answered Sep 19 '22 19:09

cs0815