0

I am trying to produce a static map via the leaflet package so I can knit it to a PDF using markdown but the circle markers on the map keep getting cut off and I can't for the life of me figure out why.

Here is the code I used to generate the map in markdown:

{r leaflet, echo=FALSE,fig.align = 'center',message=FALSE}

site=c(1,4,5,7,6,9,10,11,12,13,14,16,17,18,19,20,21,22,23,25,26,27,28,30,31,32,33,34,35,38,39,40,42,43,44,46,47,48,50,51,52,53,54,56,55,57,58,59,60,61,62,63,64,65,66,69,70,71,72,73,75,76,74,77,78,79,82,83,84,85,86,87,88,89,90,91,92,93,94,96,95,97,98,99,100,101,102,105,106,107,108,109,110,112,113,114,115,116,117,119,120,122,123,121,124,125,126,126,128,129,131,132,133,135,136,137,138,139,141,140,142,143,144,145,146,147,148,149,150,151,152,156,159,160,161,162,163,164,165,166,168,169,170,172,173,171,174,175,177,178,180,181,182,184,185,186,187,189,190,192,193,194,196,197,198,201,200,202,203,204,206,209,210,211,212,213,214,215,216,217,218,219,220,221,223,224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,240)

lat=c(43.66184,44.79494,45.10964,43.69858,43.70498,44.62863,43.47742,43.47189,43.4778,46.1,45.32731,43.70013,44.98749,45.16991,45.36598,45.05,43.46897,43.73095,45.14393,45.08514,45.34322,44.24378,43.83065,43.67648,43.408,43.39672,45.33459,44.98288,44.95615,44.65252,43.67452,43.73467,45.14016,43.83735,43.88855,45.04484,45.111,43.69183,45.37833,43.6472,45.26212,44.66697,43.51302,43.69985,43.42405,43.50834,44.95199,43.6919,43.63744,43.43144,44.69951,43.50658,45.13892,43.68822,44.69283,45.10628,44.68769,43.4204,43.68891,43.6772,43.66724,44.94892,43.673,45.40933,44.96536,45.72368,43.66106,44.68769,44.62727,44.62965,43.61895,45.20126,45.15177,45.01517,45.1233,45.1333,44.59527,44.95621,44.71402,45.14022,45.14101,43.641,45.12103,44.9856,43.63273,43.68505,43.64198,43.61047,45.25096,43.55112,43.69663,44.95698,44.58521,43.69835,44.97415,43.6186,43.69422,43.70401,43.62252,43.65461,43.62553,44.73491,45.1492,44.3334,43.42677,44.7083,44.5023,44.50182,45.20878,43.63436,45.71507,45.2669,45.11048,45.09923,45.1,45.0832,44.99921,43.48639,43.66999,43.67264,45.16516,44.69605,45.26793,45.12006,43.4647,44.61949,45,44.27258,43.59923,43.71557,44.61159,45.3667,43.62905,43.68616,44.2575,43.73882,45.3796,45.37942,43.49335,43.74912,43.63405,43.6772,44.61894,43.68241,43.70326,43.52035,43.50868,43.78343,43.72467,43.83205,43.50578,44.2605,45.2708,45.10362,45.10574,43.42055,44.97344,43.41084,44.58236,43.51516,44.61175,44.98887,43.63253,43.6283,45.33006,44.95833,44.97313,44.99147,43.47264,45.02376,43.50256,43.63685,43.633,45.38256,43.6296,44.97619,43.69172,44.95984,44.58356,45.41866,45.21372,44.97467,45.0073,43.67669,43.80858,43.52703,43.53301,44.61022,43.67633,44.60393,44.43992,43.51764,44.6325,44.60235,43.66345,44.99206,44.9766,44.93675,44.6167,44.70273,44.48513)

long=c(-65.82654,-66.76915,-64.25404,-65.9014,-65.90719,-65.7084,-65.6738,-65.67257,-65.6778,-64.9,-64.48189,-65.83812,-66.96382,-64.76276,-64.41178,-66.78266,-65.75105,-65.86791,-64.26228,-67.08128,-63.94631,-66.36808,-65.99769,-65.82789,-65.61631,-65.62322,-64.49627,-66.96304,-66.93141,-66.71888,-65.78074,-66.09383,-66.9969,-66.12741,-66.09718,-66.86069,-66.35931,-65.90693,-64.12124,-66.05395,-66.08877,-66.75014,-65.76929,-65.94188,-65.64708,-66.00485,-66.73269,-65.82077,-66.14861,-65.6245,-65.6109,-65.77518,-66.26114,-65.93082,-66.74722,-64.34153,-66.69499,-65.67671,-66.14406,-65.9948,-66.00251,-66.94392,-65.99518,-64.92706,-66.72582,-64.61453,-65.91562,-66.72309,-66.68641,-66.68477,-66.03735,-64.62047,-64.81232,-66.93072,-66.9964,-66.99933,-66.76369,-66.96656,-66.71938,-66.96113,-66.96551,-66.06152,-67.01127,-66.91766,-65.78451,-65.77797,-65.79008,-66.02289,-65.00188,-65.79455,-65.88197,-65.23964,-66.75834,-65.86214,-66.95249,-66.02373,-65.94689,-65.85686,-66.02926,-65.99359,-65.81501,-66.72604,-66.9577,-66.26667,-65.62647,-66.71053,-67.1013,-67.10187,-66.10616,-66.04112,-64.65026,-66.09046,-67.03546,-66.39665,-64.949,-64.9667,-66.90648,-65.98843,-66.05015,-66.05649,-66.23939,-66.71873,-66.07333,-66.54549,-65.98472,-66.76206,-66.99933,-66.31754,-66.02321,-65.89888,-66.81908,-64.332,-66.02703,-65.83184,-66.33866,-65.94848,-64.12557,-64.12941,-65.68482,-65.86594,-65.79096,-65.86615,-66.72928,-65.83945,-66.04713,-65.77216,-65.76416,-66.14067,-66.08676,-65.92331,-65.98543,-66.32979,-66.06732,-66.50543,-66.50797,-65.63906,-66.9132,-66.01256,-66.76968,-65.76324,-66.75236,-66.91457,-66.06356,-66.0573,-64.69175,-66.73,-66.90683,-66.94061,-65.71102,-66.93858,-65.77045,-65.82332,-65.82441,-64.21233,-65.82546,-66.90538,-65.8667,-66.7197,-66.76596,-64.92214,-66.10414,-66.92311,-65.13797,-66.01695,-66.00262,-65.76791,-65.77155,-66.84181,-65.97445,-66.84205,-66.01569,-65.78002,-66.69222,-66.80682,-65.86613,-66.87169,-66.70255,-66.73583,-66.83263,-66.7374,-66.85033)

data=data.frame(site,lat,long)

library(leaflet)

# start basemap (note the argument to hide the zoom buttons)
leaflet(options = leafletOptions(zoomControl = FALSE)) %>% 
  
  # add ocean basemap
  addProviderTiles(providers$Esri.OceanBasemap) %>%
  
  # focus map in a certain area / zoom level
  setView(lng = -66.0418475, lat = 45.110292, zoom = 7) %>%
  
  # add inset map
  addMiniMap(
    tiles = providers$Esri.OceanBasemap,
    position = 'bottomright', 
    width = 100, height = 100,
    toggleDisplay = FALSE) %>%
  
  ### Add scale bar
  addScaleBar()%>%
  
  # add points (as circle markers)
  addCircleMarkers(data = data, ~long, ~lat,
                   weight = 0.5,
                   col = 'white', 
                   fillColor = 'blue',
                   radius = 10, 
                   fillOpacity = 0.9, 
                   stroke = T) 

When I knit this to html I get a map that looks like this, which is what I want it to look like in the PDF: enter image description here

But when I knit this to a PDF it looks like this, you can notice the circle markers are cut off near the bottom center.

enter image description here

I've tried messing around with the zoom level and the setView, neither of which have worked. I've also tried messing around with the fig.height/fig.width/out.height/out.width settings, none of which worked (and also created blank grey areas in the map which is a whole other can of worms I'm trying to figure out).

I realize I should probably use a map package that isn't interactive like leaflet but at this point I've come too far so I would like to figure out why this is happening. Any help or advice would be greatly appreciated!

UPDATE: sessionInfo() output

R version 4.0.3 (2020-10-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18363)

Matrix products: default

locale:
[1] LC_COLLATE=English_Canada.1252  LC_CTYPE=English_Canada.1252    
LC_MONETARY=English_Canada.1252 LC_NUMERIC=C                   
[5] LC_TIME=English_Canada.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] sp_1.4-4

loaded via a namespace (and not attached):
[1] pillar_1.5.0      compiler_4.0.3    tools_4.0.3       digest_0.6.27     
evaluate_0.14     lifecycle_1.0.0   tibble_3.0.4      gtable_0.3.0     
[9] lattice_0.20-41   pkgconfig_2.0.3   rlang_0.4.10      DBI_1.1.1         
yaml_2.2.1        xfun_0.19         dplyr_1.0.4       stringr_1.4.0    
[17] knitr_1.31        generics_0.1.0    vctrs_0.3.5       grid_4.0.3        
tidyselect_1.1.0  glue_1.4.2        R6_2.5.0          fansi_0.4.1      
[25] rmarkdown_2.7     purrr_0.3.4       tidyr_1.1.2       ggplot2_3.3.3     
magrittr_2.0.1    scales_1.1.1      ellipsis_0.3.1    htmltools_0.5.1.1
[33] ggthemes_4.2.4    assertthat_0.2.1  colorspace_2.0-0  utf8_1.1.4        
stringi_1.5.3     munsell_0.5.0     crayon_1.4.1     
3
  • I was able to reproduce your maps. Setting setView(..., zoom = 6) fixed the issue. Can you please run sessionInfo() and edit your question with its output?. Also, are you using any latex template or package in the YAML header?
    – canovasjm
    Commented Mar 5, 2021 at 21:55
  • Reducing the zoom does work but unfortunately I need it to be more zoomed in than that. It seems as though anything within a certain distance from the border is getting cut off because if I zoom in really far some of the top right points get cut off too. I don't believe I am using a latex template or package in the YAML header, but I could definitely be wrong as I am quite new to markdown and am still trying to wrap my brain around how it works. I'll post the output to sessionInfo() above though, thanks! Commented Mar 6, 2021 at 23:05
  • Just in case anyone else runs into this issue, I was able to fix the problem by using the answer provided here: stackoverflow.com/questions/35527206/… Commented Mar 8, 2021 at 13:23

0

Browse other questions tagged or ask your own question.