JWH2006
JWH2006

Reputation: 239

interactive leaflet heatmap in R showing change between two datasets

I am struggling to get my leaflet heatmap to work. The purpose of the map is to show the percent change in population requiring services when moving from an old value to a new value.

The issue is that I am getting an error that says

Don't know how to get path data from object of class data.frame

How I go about solving this issue, I have no idea. Currently my data is in this form

   Id ethnicity hispanic value state
    1 Black No 15 AL
    2 White Yes 21 AL
    3 Black Yes 25 AL
    4 Asian No 24 AL
    5 Black No 22 AL
    6 Black No 15 AL
    7 White Yes 21 AL
    8 Black Yes 25 AL
    9 Asian No 24 AL
    10 White Yes 21 AL
    11 White No 20 AL
    12 White No 21 AL
    13 Black No 25 AL
    14 Asian No 24 AL
    15 Black No 22 AL
    16 Black No 16 AL
    17 Asian Yes 20 AL
    18 Black Yes 19 AL

Where I am at in the code is here. I could make this work perfectly with a pin map, but a heat map is causing me some serious issues.

library("tigris")

states <- states(cb = TRUE)
View (states)
class( states )

data <- read.csv (file ="data.csv")
colnames(data)[colnames(data)=="ZIP_REAL"] <- "region"
data_F <- merge (data, data_ll, by = "region")
data_F <- merge (data_F, states, by = "STUSPS")


data_OLD <- subset (data_F, Value >= 20)
data_X <-   subset (data_F, Value >= 22)

bins <- c(100, 25, 15, 5, -5, -15, -25, -100)
pal <- colorBin("RdYlBu", domain = states$change, bins = bins, na.color="black")

ui <- fluidPage(
                selectInput(inputId = "Ethnicity", 
                            label = "Ethnicity", 
                            choices = c("Asian","Black", "Amiercan_Indian", "White", "All")),
            selectInput(inputId = "Hispanic", 
                            label = "Hispanic", 
                            choices = c("Yes","No")),
            selectInput(inputId = "TS.FeeGrant", 
                            label = "Fee Grant", 
                            choices = c("Yes","No"))
                    ,
  leafletOutput(outputId = "map")
)

server <- function(input, output, session){ 

  df <- reactive({
     old <- nrow(subset(data_OLD, (Ethnicity == input$Ethnicity & 
                    Hispanic == input$Hispanic))) %>% 
                    group_by(state) %>% 
                    summarise(Freq=n())
     NewX <- nrow(subset(data_X, (Ethnicity == input$Ethnicity & 
                    Hispanic == input$Hispanic))) %>% 
                    group_by(state) %>% 
                    summarise(Freq=n())


     change <- ((NewX$Freq - old$Freq)/(old$Freq)) * 100



  })  


output$map <- renderLeaflet({
leaflet(data_F) %>%
  setView(-96, 37.8, 4) %>%
  addProviderTiles("MapBox", options = providerTileOptions(
    id = "mapbox.light",
    accessToken = Sys.getenv('MAPBOX_ACCESS_TOKEN'))) %>%
  addPolygons(
    fillColor = colorBin("RdYlBu", domain = df, 
            bins = c(100, 25, 15, 5, -5, -15, -25, -100), na.color="black"),
    weight = 2,
    opacity = 1,
    color = "white",
    dashArray = "3",
    fillOpacity = 0.7,
    highlight = highlightOptions(
      weight = 5,
      color = "#666",
      dashArray = "",
      fillOpacity = 0.7,
      bringToFront = TRUE),
    label = labels,
    labelOptions = labelOptions(
      style = list("font-weight" = "normal", padding = "3px 8px"),
      textsize = "15px",
      direction = "auto")) %>%
  addLegend(pal = pal, values = ~df, opacity = 0.7, title = "Presentation",
    position = "bottomright")
    })
}

shinyApp(ui = ui, server = server)

Upvotes: 0

Views: 496

Answers (1)

Stedy
Stedy

Reputation: 7469

Its hard to say without actual data but it seems to me that you are passing df to the domain for fillColor parameter of addPolygons(). Instead try passing a vector such as df$change.

Example from the Rstudio documentation:

bins <- c(0, 10, 20, 50, 100, 200, 500, 1000, Inf)
pal <- colorBin("YlOrRd", domain = states$density, bins = bins)

m %>% addPolygons(
  fillColor = ~pal(density),
  weight = 2,
  opacity = 1,
  color = "white",
  dashArray = "3",
  fillOpacity = 0.7)

Upvotes: 1

Related Questions