tomroh / leaflegend

Provides extensions to the leaflet R package to customize legends with images, text styling, orientation, sizing, and symbology.
https://leaflegend.delveds.com
Other
34 stars 3 forks source link

labels for addLegendFactor throw error #63

Closed bac3917 closed 1 year ago

bac3917 commented 1 year ago

Hi Tom... Not sure if it's my code or the package, but I thought you could help either way...I wanted to use your package to generate smaller fonts in my map legend.

temp<-structure(list(event_name = c("2022 Refugee Educati", "2022 Refugee Educati", "EMPOWER 2022", "2022 Refugee Educati", "EMPOWER 2022", "2022 Refugee Educati", "2022 Refugee Educati"), start_lat = c(41.427199, 40.040505, 40.468968, 40.238207, 40.332502, 40.343261, 40.327741), start_lon = c(-75.61821, -75.63425, -79.91863, -76.511942, -79.8646, -76.7808, -75.87002)), row.names = c(521L, 311L, 33L, 455L, 88L, 333L, 444L), class = "data.frame")

mypal<-colorFactor(palette = "Dark2",domain = temp$event_name)

leaflet() %>% addCircleMarkers(data=temp,lng = ~start_lon,lat=~start_lat,weight=.3, radius = 3,fillOpacity = .85, fillColor = ~mypal(temp$event_name)) %>% addLegendFactor(data=temp, position="bottomright", pal=mypal, labelStyle = 'font-size=9px', labels=temp$event_name, # the command works if I remove this line values=~temp$event_name,title = "Event")

tomroh commented 1 year ago
leaflet() |> 
  addCircleMarkers(data=temp,lng = ~start_lon,lat=~start_lat,weight=.3,
    radius = 3,fillOpacity = .85,
    fillColor = ~mypal(temp$event_name)) |> 
  addLegendFactor(data=temp,
    position="bottomright",
    pal=mypal,
    labelStyle = 'font-size: 9px;', # change this line to css syntax
    values=~event_name,title = "Event")
tomroh commented 1 year ago

This also might be easier to remember for R users:

htmltools::css(`font-size` = '9px')
bac3917 commented 1 year ago

Thanks Tom. Here's my final code:

leaflet() |> addCircleMarkers(data=temp,lng = ~start_lon,lat=~start_lat,weight=.3, radius = 3,fillOpacity = .85, fillColor = ~mypal(reg4$event_name)) |> addLegendFactor(data=temp, position="bottomright", pal=mypal, labelStyle = htmltools::css("font-size"= '9px'), # change this line to css syntax values=~event_name,title = "Event")