Virginia: Children in Foster Care

This map provides data from Virginia on the number of children in the child welfare system.

The data collected by the State Department of Social Services is made available publically on a monthly basis as “snapshot data”. This data is from September 2017. Eventually, I would like to expand this dashboard to include additional statistics besides number of children.

Hover over individual counties and indepndent cities to get the number of children reported by each county/city.

library(leaflet)
library(tigris)
## As of version 0.5.1, tigris does not cache downloaded data by default. To enable caching of data, set `options(tigris_use_cache = TRUE)` in your R script or .Rprofile.
## 
## Attaching package: 'tigris'
## The following object is masked from 'package:graphics':
## 
##     plot
library(maptools)
## Loading required package: sp
## Warning: package 'sp' was built under R version 3.4.1
## Checking rgeos availability: TRUE
library(htmltools)

#Load foster care data
fostercare <- read.csv("FCsexrace.csv")

#Load shapefile
shapefile <- counties(state="VA")
#Convert County FIPS to Integer
newfile <- shapefile
newfile$FIPS <- as.integer(shapefile$COUNTYFP)
newfile <- newfile[order(newfile$FIPS), ]

#Remove Subtotals and Totals Rows in fostercare data
fostercare <- fostercare[-which(is.na(fostercare$FIPS)), ]
#Remove an errant FIPS county not found in the shapefile
fostercare <- fostercare[-which(fostercare$FIPS==515), ]

fostercare <- fostercare[order(fostercare$FIPS), ]

#Change row names in the foster care DataFrame to convert to SpatialPolygonDataFrame
row.names(fostercare) <- row.names(newfile)

#Convert regular DataFrame to SpatialPolygonDataFrame
fostercare <- SpatialPolygonsDataFrame(newfile, fostercare)

#Set the color palette to "reds" for the number of children column
pal <- colorNumeric(palette="Reds", domain=fostercare$Total.Children.in.Care)

#Generate lable text in HTML
popupText <- fostercare$hoverText <-
  mapply(function (county, children) {
    htmltools::HTML(
      sprintf("<div style='font-size:12px;width:200px;float:left'>
               <span style='font-size:14px;font-weight:bold'>               
               %s
               </span>
               <br />
               <br />
               Total Children in Care: %s </div>", county, children))},
    fostercare$LOCALITY, fostercare$Total.Children.in.Care, SIMPLIFY = FALSE
    )

#Populate legend text
legendText <- htmltools::HTML("<div style='font-size:12px;width:200px;float:left'>Total Number<br />Of Children</div>")

m <- leaflet(options=leafletOptions(crs = leafletCRS("L.CRS.Simple"), minZoom = 5, maxZoom = 10)) %>%
  addTiles() %>%
  #Add county polygons
  addPolygons(data=fostercare, smoothFactor = 0.2, weight=0, fillOpacity = 1, fillColor = ~pal(Total.Children.in.Care), label=popupText) %>%
  #Add legend
  addLegend("topleft", pal = pal, values = fostercare$Total.Children.in.Care,
    title = legendText,
    opacity = 1) %>%
  #Add title marker
  addLabelOnlyMarkers(
    lng = -81.724658, lat = 40.990512,
    label = "Virginia: Children in Foster Care",
    labelOptions = labelOptions(noHide = T, textOnly = TRUE, textsize="26px")) 

#Display map
m