I started with this post and the code from @Hallie Swan works great, but I want to change the last shape in the legend into a hollow triangle.
My impression is that it's challenging to draw a hollow triangle purely in CSS. Then I found that it might work if I can inline an svg tag (that draws a hollow triangle) in the customized CSS part in R. But I failed.
Here's the code from the original post, excluding the shiny part:
library(leaflet)
colors <- c("red", "white", "blue", "white", "blue", "red")
labels <- c("filled_square", "empty_square", "big_square", "empty_circle", "filled_circle", "big_circle")
sizes <- c(10, 20, 30, 10, 20, 30)
shapes <- c("square", "square", "square", "circle", "circle", "circle")
borders <- c("red", "blue", "black", "blue", "blue", "black")
addLegendCustom <- function(map, colors, labels, sizes, shapes, borders, opacity = 0.5){
make_shapes <- function(colors, sizes, borders, shapes) {
shapes <- gsub("circle", "50%", shapes)
shapes <- gsub("square", "0%", shapes)
paste0(colors, "; width:", sizes, "px; height:", sizes, "px; border:3px solid ", borders, "; border-radius:", shapes)
}
make_labels <- function(sizes, labels) {
paste0("<div style='display: inline-block;height: ",
sizes, "px;margin-top: 4px;line-height: ",
sizes, "px;'>", labels, "</div>")
}
legend_colors <- make_shapes(colors, sizes, borders, shapes)
# I added my modification here, see below
legend_labels <- make_labels(sizes, labels)
return(addLegend(map, colors = legend_colors, labels = legend_labels, opacity = opacity))
}
leaflet() %>% addTiles() %>%
addLegendCustom(colors, labels, sizes, shapes, borders)
What I've tried:
Based on the original code, I tried modifying the last element in the legend_colors
vector, like so:
legend_colors[length(legend_colors)] <- "url('data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' height='20' width='30'><polygon points='15,3 25,15 5,15' style='fill:transparent;stroke:black;stroke-width:3' /></svg>')"
But no triangle was displayed after I added the above line to the original script. The place where the triangle is supposed to appear is empty:
A screenshot of what it looks like at the moment:
Based on the information in another post, for this last legend shape I'm trying to modify, the CSS code constructed under the hood by leaflet should be (please correct me if I'm wrong here):
{
background: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' height='20' width='30'><polygon points='15,3 25,15 5,15' style='fill:transparent;stroke:black;stroke-width:3' /></svg>");
opacity: 0.5;
}