Reputation: 893
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.
Upvotes: 26
Views: 15992
Reputation: 1370
To elaborate on the other answers:
My understanding is that fig.width
and fig.height
don't work for leaflet
(or plotly
) because they are html objects, not 'true figures' as far as knitr
is concerned. leaflet
(and plotly
) do however respond to out.width
, which is why it works.
In case anyone ever has a similar situation to me:
I have a code chunk which conditionally includes either a leaflet
map (for html output), or 4x saved ggplot
.png images using knitr::include_graphics(p1, p2, p3, p4)
(for pdf output). In order to tile the 4 ggplot
objects in a 2x2 grid, I had to set out.width = '50%'
, but this also reduced the width of the leaflet
output.
The solution was to include leaflet(width = '100%')
in the code, and out.width = '50%'
in the chunk header. leaflet(width = '100%')
seems to override out.width = '50%'
, giving me either a full sized leaflet
in html outputs, or 50% width ggplot
figures.
Upvotes: 3
Reputation: 17388
This works fine:
SomeData %>%
leaflet(height=2000, width=2000) %>%
addTiles() %>%
addMarkers(clusterOption=markerClusterOptions())
Here SomeData is a dataframe containing columns: Lat and Long.
Upvotes: 4
Reputation: 11613
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%") %>%
Upvotes: 33
Reputation: 11762
you can set like a global figure size to the whole document... But I think your code chunks will rescale, the images not.
library(knitr)
opts_chunk$set(fig.width=12, fig.height=8)
Actually, I didn't checked it with leaflets
. Hope this code is still working.
Upvotes: 1