map_children_layout <-tm_shape(dutchess_indicators) +tm_polygons("percent_children", title ="% Children") +tm_compass() +#Add a north arrow to your map layouttm_scale_bar(position =c("left", "bottom")) #Add a scale bar to your map layoutmap_children_layout
Mapping with tmap
Static or interactive plots
tmap_mode("plot") #Default mode for tmap, makes static mapstmap_mode("view") #Mode that creates interactive maps using leaflet
#Map the percentage of children by tract in Dutchess County, NYmap_children_layout <-tm_shape(dutchess_indicators) +tm_polygons("percent_children", title ="% Children", palette ="viridis",style ="jenks") +tm_scale_bar(position =c("left", "bottom")) +#Add a scale bar to your map layouttm_layout("Children in Dutchess County, NY") #Set the map titlemap_children_layout
dutchess_with_indicator <-ggplot() +geom_sf(data = dutchess_indicators, aes(fill = percent_children)) +scale_fill_viridis(name ="% Children") +geom_sf(data = tri_dutchess) +#add layer of TRI facilitieslabs(x =NULL, #no label on the x-axisy =NULL, #no label on the y-axistitle ="Environmental Exposure in Dutchess County", #Map titlesubtitle ="Children and TRI Facilities",caption ="Source: U.S Census Bureau ACS 5-year Estimates | EPA TRI Inventory") +#Caption to add data sourceannotation_scale(location ="bl", width_hint =0.4) +#scale bar added in bottom left cornerannotation_north_arrow(location ="br", which_north ="true", #North arrow added in bottom right cornerpad_x =unit(0.0, "in"), pad_y =unit(0.2, "in"), #format arrow placementstyle = north_arrow_minimal) +#select north arrow styletheme_minimal() #set overall map layout theme, blank white backgrounddutchess_with_indicator
Add a base map to a ggplot map
Load the prettymapr package to access base map options
dutchess_with_indicator_basemap <-ggplot() +annotation_map_tile("osm") +#add a basemap using the prettymapr packagegeom_sf(data = tri_dutchess) +#add layer of TRI facilitieslabs(x =NULL, #no label on the x-axisy =NULL, #no label on the y-axistitle ="Environmental Exposure in Dutchess County", #Map titlesubtitle ="TRI Facilities",caption ="Source: EPA TRI Inventory") +#Caption to add data sourceannotation_scale(location ="bl", width_hint =0.4) +#scale bar added in bottom left cornerannotation_north_arrow(location ="br", which_north ="true", #North arrow added in bottom right cornerpad_x =unit(0.0, "in"), pad_y =unit(0.2, "in"), #format arrow placementstyle = north_arrow_minimal) +#select north arrow styletheme_minimal() #set overall map layout theme, blank white backgrounddutchess_with_indicator_basemap
Use grepl to identify rows with matching strings
Subset the Dutchess County, NY schools data to only those that can be identified as high schools, middle schools, and elementary schools.
Use contains() to subset columns with matching strings
Dplyr function
heating_clean <- heating %>%select(-contains(c("Margin of Error"))) %>%mutate_all(str_trim) %>%#Remove extra spaces at the beginning and end of all entriesmutate_at(c(2:83), str_replace,",","") %>%#Remove the , from numeric column entriesmutate_at(c(2:83), as.numeric) # Coerce value columns to numeric
Review of cleaning and wrangling functions
grepl() for pattern matching and replacement: grepl(pattern, variable)
contains() Select variables that match a pattern
Mutate multiple columns:
mutate_all the mutate affects every variable
mutate_at affects variables selected with a character vector or vars()
Working with strings
A core tidyverse package, the stringr package contains several useful functions for working with strings in the tidy framework.
Strings represent a sequence of characters and can contain letters, numbers, symbols and even spaces.
str_extract extract a string match
str_extract(string, pattern)
str_replace replace matches with new text
str_replace() replaces the first match; str_replace_all() replaces all matches.
str_remove remove matched patterns
str_remove(string, pattern)
str_trim removes whitespace from start and end of string
Learn more about these functions using the help resources within RStudio