Skip to content

colorNumeric with a palette function is 'bleeding' colors #954

Description

@mtennekes

Consider this 'binary' palette function:

library(leaflet)

# Example data
df <- data.frame(
	lat = c(40.7, 34.0, 41.9),
	lng = c(-74.0, -118.2, 12.5),
	value = c(10, 20, 30)
)

pfun = function(x) {
	ifelse(x < 0.5, "blue", "red")
}

pal = colorNumeric(palette = pfun,
				   domain = df$value)

When feed into addLegend, it seems to 'bleed' colors:

leaflet(df) %>%
  addTiles() %>%
  addCircleMarkers(
    ~lng, ~lat,
    color = ~pal(value),
    radius = 8,
    fillOpacity = 0.8
  ) %>%
  addLegend(
    "bottomright",
    pal = pal,
    values = ~value,
    title = "Value",
    labFormat = labelFormat(suffix = ""),
    opacity = 1
  )
Image

I was expecting a hard distinction between blue and red in the middle of the value range. Is this a bug, or am I missing something? Triggered by r-tmap/tmap#1175 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions