Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding labels to polylines increases line opacity #922

Open
rbechtel-MITRE opened this issue Jun 12, 2024 · 2 comments
Open

Adding labels to polylines increases line opacity #922

rbechtel-MITRE opened this issue Jun 12, 2024 · 2 comments

Comments

@rbechtel-MITRE
Copy link

Adding labels or popups to polylines increase the line's opacity. The increase in opacity seems to be related to the number of points used to plot the polyline as if each time a label is added the opacity bumps up. It seems you can account for this by decreasing the opacity proportional to the number of points that makeup the polyline.

library(leaflet)

data <- tibble(x = c(10,20,30,40), y = c(10,20,30,40), label = rep("label",4))

#has default opacity
m <- leaflet() %>%
  addProviderTiles(providers$CartoDB.Positron) %>%
  addPolylines(data = data, lng = ~x, lat = ~y)

#has opacity closer to 1
m <- leaflet() %>%
  addProviderTiles(providers$CartoDB.Positron) %>%
  addPolylines(data = data, lng = ~x, lat = ~y, label = ~label)

#decreases opacity 
m <- leaflet() %>%
  addProviderTiles(providers$CartoDB.Positron) %>%
  addPolylines(data = data, lng = ~x, lat = ~y,# label = ~label,
               opacity = 0.5 / nrow(data), fill = 0.2 / nrow(data)) #default values divided by rows in data
@jack-davison
Copy link
Contributor

jack-davison commented Jun 24, 2024

I think this is because leaflet recycles the label four times - this works fine:

leaflet() %>%
  addProviderTiles(providers$CartoDB.Positron) %>%
  addPolylines(
    data = data,
    lng = ~ x,
    lat = ~ y,
    label = ~ label[1]
  )

Or see this version, where you can toggle between the different labels.

data <- tibble(x = c(10,20,30,40), y = c(10,20,30,40), label = paste0("label", 1:4))

#has default opacity
leaflet() %>%
  addProviderTiles(providers$CartoDB.Positron) %>%
  addPolylines(data = data, lng = ~x, lat = ~y, label = ~label, group = ~label) %>%
  addLayersControl(baseGroups = data$label)

If you want different labels per segment, you could do something like:

data <-
  tibble(
    x = c(10, 20, 30, 40),
    y = c(10, 20, 30, 40),
    label = paste0("label", 1:4),
    color = c("red", "blue", "green", "orange")
  )

m <- leaflet() %>% addTiles()

for (i in seq_along(data$label)) {
  if (i > 1) {
    m <- 
      addPolylines(
        map = m,
        lng = data$x[(i-1):i],
        lat = data$y[(i-1):i],
        label = data$label[i],
        color = data$color[i]
      )
  }
}

print(m)

@rbechtel-MITRE
Copy link
Author

Yep, adding [1] resolves the issue for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants