Search code examples
rr-leaflet

How to change the density to a column name of my own?


I have been working on leaflet in R.

https://rstudio.github.io/leaflet/choropleths.html

The above us-Map contains density of a state. The Format of the data is Geo-Json. I want to remove the density variable and I want to pass my columnname with corresponding variable value. (For Example when you hover on the New Mexico I am getting density as 17.16 (density:17.16), instead I want to display as (mycolumnname:value) ).


Solution

  • This is a pretty common need in working with leaflet. There are a few ways to do this, but this is the simplest in my mind: All of the information you would like to plot is stored in the section of the SpatialPolygonsDataFrame found at states@data, which you can see by looking at the head of this data frame section:

    I made a data frame (traditional r data frame) using the state names from the original SpatialPolygonsDataFrame names states in your code above and created my_var.

    a<-data.frame( States=states@data$name)
    a$my_var <- round(runif(52, 15, 185),2)
    

    This is the first few rows of my new data frame, which is like yours but has data OTHER than density in it.

    head(a)
          States my_var
    1    Alabama 120.33
    2     Alaska 179.41
    3    Arizona  67.92
    4   Arkansas  30.57
    5 California  72.26
    6   Colorado  56.33
    

    Now that you have this data frame you can call up the library maptools and do a polygon cbind as follows:

    states2<-spCbind(states,a$my_var)
    

    Now looking at the head of states2 (which you could name states and replace the original states SpatialPolygonsDataFrame I kept both to compare before and after)

    head(states2@data)
      id       name density data.my_var
    0 01    Alabama  94.650       58.01
    1 02     Alaska   1.264       99.01
    2 04    Arizona  57.050       81.05
    3 05   Arkansas  56.430      124.68
    4 06 California 241.700      138.19
    5 08   Colorado  49.330      103.78
    

    this added the data.my_var variable into the spatial data frame. Now you can use find/replace, to go through and replace the references in your code where it says density with data.my_var and the new variables will be used.

    Important things to consider Your data has 50 state names, the spatial data frame has 52, you will need to add in the missing states to your data frame before cBinding them, they must be the same length AND in the same order.

    If you grab the names like this:

    a<-data.frame( States=states@data$name)
    

    from the states object, you can then left merge on States, with your data and it will keep the order a and all the cells which are empty where the new regions have not data in your data set will remain empty.

    Use merge to be sure that data lines up properly.

     a<- merge(a, your_data ,by=c("States","name"))
    

    Also, once they are merged and you have checked that states@data$name is in the same order as a$States, you can use any name you want as new heading in the SpatialPolygonDataFrame by extracting the data into a vector with the name you want prior to binding them:

    my_var <- a$my_var
    states2<-spCbind(states, my_var)
    

    this will leave you with a data frame which looks like this:

      id       name density      my_var
    0 01    Alabama  94.650       58.01
    1 02     Alaska   1.264       99.01  
    

    This is easier to address as a column name from inside leaflet without long strings.