4

I am trying to place the name of the legend in tow rows, but the usual way does not work. Can you help me to identify the reason?

library(leaflet)

    dfXL = data.frame(
    lat = as.numeric(c("-30","-30.1","-30.5")),
    lng = as.numeric(c("14","14.6","14.6")),
    sizeXL = as.numeric(c("1","2","3")),
    color = "red")
    #dfXL
    palXL <- colorNumeric(palette=c("#FFFFFF","#FFA07A"), as.numeric(as.character(dfXL$sizeXL)))

    dfXL$size_color<- palXL(dfXL$sizeXL)
    dfXL
    XL = leaflet(dfXL)%>% 
    setView(lng = 10.132974, lat = 10.706839, zoom = 3) %>% 
    addProviderTiles("Esri.WorldImagery") %>% 
    addCircleMarkers(radius = ~sizeXL/25 , color = ~size_color, fill = F,clusterOptions = markerClusterOptions(freezeAtZoom = 16),opacity=1) %>%
    addLegend("bottomright",labels=dfXL$sizeXL,title=paste(expression(atop("a","b"))),opacity=1,colors=dfXL$size_color)

    XL
yuliaUU
  • 1,581
  • 2
  • 12
  • 33

1 Answers1

7

Use </br> to introduce breaks

Copy and paste this reproducible example:

library(leaflet)
library(raster)
p <- shapefile(system.file("external/lux.shp", package="raster"))

pal <- colorNumeric(
  palette = "Reds",
  domain = p$AREA)

p %>% 
  leaflet() %>% 
  addTiles() %>% 
  addPolygons(stroke=FALSE, color = ~pal(AREA), fillOpacity = 1) %>% 
  addLegend(pal = pal, values = ~AREA, title = "line 1 </br> line 2")
Rich Pauloo
  • 7,734
  • 4
  • 37
  • 69
  • thank you a lot! Do you know why atop() thing does not work? – yuliaUU Nov 05 '18 at 06:04
  • Not sure. But in general, R packages that generate HTML output (knitr, leaflet, mapview, etc) are typically equipped to interpret HTML like ``. Approaching formatting problems within this package space from the perspective of "how can I HTML this?" has been a useful way for me to get through most things. – Rich Pauloo Nov 05 '18 at 18:23
  • Thank you for your explanation and advice! Makes sense that HTML syntax is different compare to plotting using ggplot – yuliaUU Nov 06 '18 at 02:11