0

I'm new to the leaflet package and have never added a legend. I'm mapping a couple data sets of real estate sales and want a corresponding legend based on the colors I've indicated in each fillColor argument on the map. I figured out how to add the legend, but the legend colors are not the same color as the dots even though I've specified the same character string in the legend as the circle markers.

Here is the dput output of the two data frames that I'm using:

  1. sales_mapped2
structure(list(address = c("721 N 74th St", "8317 13th Ave NW", 
"907 N 102nd St", "7421 Bagley Ave N", "7546 26th Ave NW", "329 N 103rd St", 
"120 NW 81st St", "8715 13th Ave NW", "8743 1st Ave NW"), `Lot Square Footage` = c(2912, 
3725, 3767, 2900, 3880, 3844, 3840, 2780, NA), lat = c(47.682342, 
47.68948, 47.702658, 47.683272, 47.684479, 47.703414, 47.687876, 
47.692847, 47.69636), lon = c(-122.348865, -122.37307, -122.347, 
-122.331837, -122.389456, -122.354084, -122.358986, -122.373146, 
-122.35784), `Sale Type` = c("MLS Sale", "Non-MLS Sale", "MLS Sale", 
"MLS Sale", "MLS Sale", "MLS Sale", "MLS Sale", "MLS Sale", "Non-MLS Sale"
)), row.names = c(NA, -9L), class = c("tbl_df", "tbl", "data.frame"
))
  1. other_sales
structure(list(address = c("107 NW 50th St", "7047 16th Ave NW", 
"834 NE 79th St #Lot20", "3 XX NE 91st St"), `Lot Square Footage` = c(3000, 
4668, 2910, 8346), lat = c(47.664796, 47.681053, 47.686243, 47.695064
), lon = c(-122.357562, -122.378084, -122.318783, -122.324378
)), row.names = c(NA, -4L), class = c("tbl_df", "tbl", "data.frame"
))

I first had to create an additional smaller data frame for my subject property to use in the color palette creation (unless I didn't need to do this...not sure as I'm not familiar with these functions). The code for that step is below:

subject <- sales_mapped2 %>% 
  filter(address == "8743 1st Ave NW")

Then I created my color palettes to be used in the leaflet map. Also below is the code use to create the leaflet map with the data points colored and the corresponding legend with an attempt to match the colors. However, as stated, the legend colors and data point colors are not the same even though they have the same color string named:

library(leaflet)
library(tidyverse)

# Colors Palettes
## Additional Sales
pal <-  colorFactor(palette = 'blue', 
                    domain = sales_mapped2$address)  
## Other Sales
pal2 <- colorFactor(palette = "red",
                    domain = other_sales$address)
## Subject
pal3 <- colorFactor(palette = "orange",
                    domain = subject$address)
leaflet() %>%
addTiles() %>%
setView(lng = -122.35784, lat = 47.69636, zoom = 13) %>% 
addCircleMarkers(data = sales_mapped2 %>% 
                     filter(address != "8743 1st Ave NW"),
                   lng = ~lon, lat = ~lat, popup = ~address, fillColor = "blue",
                   fillOpacity = 0.75, stroke = FALSE) %>% 
addCircleMarkers(data = sales_mapped2 %>% 
                     filter(address == "8743 1st Ave NW"),
                   fillColor = "orange", popup = ~address,
                   fillOpacity = 1, stroke = FALSE) %>% 
addCircleMarkers(data = other_sales, 
                   lng = ~lon, lat = ~lat, popup = ~address,
                   fillColor = "red", fillOpacity = 2, stroke = FALSE) %>% 
addLegend(data = sales_mapped2, 
            "bottomright", pal = pal, values = ~address, title = "Additional Sales") %>% 
addLegend(data = sales_mapped2 %>% filter(address == "8743 1st Ave NW"),
          "topleft", pal = pal3, values = ~address, title = "Subject") %>% 
addLegend(data = other_sales,
            "topleft", pal = pal2, values = ~address, title = "Other Sales")

I can't figure out why the colors are not the same. Please help.

EastBeast
  • 89
  • 7
  • did you read the help page and the examples at `?leaflet::addLegend()`? what did you try? – mnist Sep 07 '21 at 20:46
  • Yes, I read the help section for this and the examples. I don't understand what argument I need to pass to the `addLegend()` function to make the legend display, which is why I posted the question. – EastBeast Sep 07 '21 at 20:49
  • good! but still: what did you try? Please provide a [reproducible minimal example](https://stackoverflow.com/q/5963269/8107362). Especially, provide some sample data, e.g. with `dput()` and use the [reprex-package](https://reprex.tidyverse.org/). – mnist Sep 07 '21 at 20:51

0 Answers0