I'm wondering whether there is a way to set colors for markers in leaflet based on the explicit values of a column. If there isn't, maybe someone could suggest a different way to achieve what I want to do here: color points based on an RGB value created from three different columns in the input data frame.
Here is an example of what I've done so far. My sample data contains three columns, each of which takes a value between 0 and 5. I'd like to color these points on the map such that the R component of RGB comes from the first column value; the G component comes from the second column value, and the B component comes from the third column value.
Since the RGB scale goes from 0-255, I started by dividing my 0-5 values by 5 to get a proportion, and then multiplying that by 255 to get the corresponding value of that color. Then I literally pasted those values together to create a column
rgb containing the rgb codes for the colors I'd like to make.
I've used color palettes in leaflet before, but they seem to involve mapping colors from a specific range or set of values to the values of a column in your data, not taking the literal values in the data as the colors themselves. I suppose an analogous problem would be if I had a column in my data with the character values "black", "white" and "red", and I wanted to have those values literally be the colors of my points.
Can anyone help me figure out how to color my points according to this rgb column in my data? Alternatively, do you have a better suggestion for how to compute a color based on three different cols in the data? I'll be the first to admit that my approach so far seems pretty hacky, so I'm very open to other approaches.
Here's a reproducible example of what I've done so far.
library(dplyr) library(leaflet) # Data frame with three columns, each of which takes values between 0 and 5 df <- data.frame(sentence1.pred = c(4.324, 1.265, 2.540), sentence2.pred = c(1.003, 2.451, 2.034), sentence3.pred = c(4.376, 3.871, 4.998), lat = c(42.6416, 42.7207, 42.7043), long = c(-71.8534, -71.7127, -71.8114)) # Using these values, compute corresponding RGB values as proportions of 255. df2 <- df %>% mutate(r = (sentence1.pred/5)*255, g = (sentence2.pred/5)*255, b = (sentence3.pred/5)*255) # Compose an RGB column from cols r, g, and b df3 <- df2 %>% mutate(rgb = paste0("rgb(", r, ", ", g, ", ", b, ")")) # Make basic leaflet map leaflet(df3) %>% addTiles() %>% addCircleMarkers(lng = ~long, lat = ~lat) # Now here, I'd like to color the circle markers according to the rgb column.
Thank you very much for any suggestions, resources, etc. or for pointing me in the right direction!