Emma Hockly
‌
‌
‌
‌
‌
‌
‌
‌
‌
‌
‌
‌
‌
‌
Sign up
Beta
Spinner

Course Notes

Use this workspace to take notes, store code snippets, or build your own interactive cheatsheet! The datasets used in this course are available in the datasets folder.

# Import any packages you want to use here
library(leaflet)

Take Notes

Add notes here about the concepts you've learned and code cells with code you want to keep.

Add your notes here

#Chapter 2 trying out notes first time
# Add your code snippets here
clearBounds()
clearMarkers()
glimpse(ipeds)
ipeds %>% group_by(sector_label) %>% count %>% arrange(desc(n))

# Create a list of US States in descending order by the number of colleges in each state
ipeds  %>% 
    group_by(state)  %>% 
    count()  %>% 
    arrange(desc(n))

# Create a dataframe called `ca` with data on only colleges in California
ca <- ipeds %>%
        filter(state == "CA")

# Use `addMarkers` to plot all of the colleges in `ca` on the `m` leaflet map
map %>%
    addMarkers(lng = ca$lng, lat = ca$lat)

# Set the zoom level to 8 and store in the m object
map_zoom <-
    map %>%
    addMarkers(data = ca) %>%
     setView(lat = la_coords$lat, lng = la_coords$lon, zoom = 8)

map_zoom

# Change the radius of each circle to be 2 pixels and the color to red
map2 %>% 
    addCircleMarkers(lng = ca$lng, lat = ca$lat,
                     radius = 2, color = "red")

# Add circle markers with popups for college names
map %>%
    addCircleMarkers(data = ca, radius = 2, popup = ~name)

# Change circle color to #2cb42c and store map in map_color object
map_color <- map %>% 
    addCircleMarkers(data = ca, radius = 2, color = "#2cb42c", popup = ~name)

# Print map_color
map_color

# Customize the legend
m %>% 
    addLegend(pal = pal, 
              values = c("Public", "Private", "For-Profit"),
              # opacity of .5, title of Sector, and position of topright
              opacity = 0.5, title = "Sector", position = "topright")

Chapter 3 
library(leaflet.extras)

leaflet() %>%
  addTiles() %>% 
  addSearchOSM() %>% 
  addReverseSearchOSM() 

m2 <-
    ipeds %>% 
        leaflet() %>% 
            # use the CartoDB provider tile
            addProviderTiles("CartoDB") %>% 
            # center on the middle of the US with zoom of 3
            setView(lat = 39.8282, lng = -98.5795, zoom = 3)

# Map all American colleges 
m2 %>% 
    addCircleMarkers() 

# Load the htmltools package
library(htmltools)

# Create data frame called public with only public colleges
public <- filter(ipeds, sector_label == "Public")  

# Create a leaflet map of public colleges called m3 
m3 <- leaflet() %>% 
        addProviderTiles("CartoDB") %>% 
        addCircleMarkers(data = public, radius = 2, label = ~htmlEscape(name),
                         color = ~pal(sector_label), group = "Public")

m3

# Create data frame called private with only private colleges
private <- filter(ipeds, sector_label == "Private")  

# Add private colleges to `m3` as a new layer
m3 <- m3 %>% 
        addCircleMarkers(data = private, radius = 2, label = ~htmlEscape(name),
                         color = ~pal(sector_label), group = "Private") %>% 
        addLayersControl(overlayGroups = c("Public", "Private"))

m3

# Map with choice of baselayers and groups of colleges in their own layer
m4 <- leaflet() %>% 
        addTiles(group = "OSM") %>% 
        addProviderTiles("CartoDB", group = "Carto") %>% 
        addProviderTiles("Esri", group = "Esri") %>% 
        addCircleMarkers(data = public, radius = 2, label = ~htmlEscape(name),
                         color = ~pal(sector_label),  group = "Public") %>% 
        addCircleMarkers(data = private, radius = 2, label = ~htmlEscape(name),
                           color = ~pal(sector_label), group = "Private")  %>% 
        addCircleMarkers(data = profit, radius = 2, label = ~htmlEscape(name),
                         color = ~pal(sector_label), group = "For-Profit")  %>% 
        addLayersControl(baseGroups = c("OSM", "Carto", "Esri"), 
                         overlayGroups = c("Public", "Private", "For-Profit")) %>% 
        setView(lat = 39.8282, lng = -98.5795, zoom = 4) 

m4

ipeds %>% 
    leaflet() %>% 
        addTiles() %>% 
        # Sanitize any html in our labels
        addCircleMarkers(radius = 2, label = ~htmlEscape(name),
                         # Color code colleges by sector using the `pal` color palette
                         color = ~pal(sector_label),
                         # Cluster all colleges using `clusterOptions`
                         clusterOptions = markerClusterOptions())

#Chapter 4
    script.R

1
2

    R Console
    Slides
    Notes

# Print a summary of the `shp` data
summary(shp)
Object of class SpatialPolygonsDataFrame
Coordinates:
        min       max
x -84.32187 -75.46089
y  33.84232  36.58812
Is projected: FALSE 
proj4string :
[+init=epsg:4326 +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84
+towgs84=0,0,0]
Data attributes:
    GEOID10         ALAND10   
 27006  :  1   100240769:  1  
 27007  :  1   100252722:  1  
 27009  :  1   1003885  :  1  
 27011  :  1   100620829:  1  
 27012  :  1   100707703:  1  
 27013  :  1   101001856:  1  
 (Other):802   (Other)  :802  
# Print the class of `shp`
class(shp)
[1] "SpatialPolygonsDataFrame"
attr(,"package")
[1] "sp"
# Print the class of `shp`
class(shp)
[1] "SpatialPolygonsDataFrame"
attr(,"package")
[1] "sp"

# Print the class of the data slot of shp
class(shp@data)
[1] "data.frame"
# Print the class of the data slot of shp
class(shp@data)
[1] "data.frame"
# Print GEOID10
shp@data$GEOID10
  [1] 27925 28754 28092 27217 28711 28666 28602 27841 27831 28785 27504 27330
 [13] 28768 28658 28716 28139 27565 28394 27982 28025 28159 28382 28312 28342
 [25] 27839 27852 28723 28077 28039 28452 27306 28375 28713 28743 28717 28150
 [37] 28447 27205 27379 28425 27827 27540 28114 28451 27892 27249 28628 27873
 [49] 28781 27916 28705 28714 28101 28102 28445 28448 28458 28719 28478 28479
 [61] 28501 28748 28752 28207 28753 28757 28209 28212 28560 28504 27983 27985
 [73] 28018 28019 28562 28906 28530 28771 28779 28782 28376 28581 28152 28169
 [85] 28170 28657 28021 28204 28533 28540 28543 28551 28262 28280 28575 28790
 [97] 28792 28667 28672 28108 28462 28681 28465 28734 28739 28694 28697 28702
[109] 28745 28127 28420 28422 28424 28428 28435 28088 28089 28090 27562 28334
[121] 28787 28433 27360 27534 28043 27370 28444 27531 28675 28712 28449 27053
[133] 27944 28367 28326 28740 28659 28282 27244 27597 27017 28761 28457 28441
[145] 27956 27889 28652 28146 28513 28777 28786 27596 27530 28369 28327 27340
[157] 27028 27823 27879 28244 27810 27886 28306 27025 27239 27967 27824 27826
[169] 27834 27030 28358 28365 27520 27524 27525 27526 27292 27874 27882 27883
[181] 27885 27253 27576 27577 27582 27295 27298 27332 27910 27052 27055 27344
[193] 27516 27850 27856 27265 27603 27605 27537 27539 27541 28601 28604 27809
[205] 27278 27284 27371 27201 27312 28320 28325 27207 28330 28607 28611 28612
[217] 27549 27555 27317 27320 27703 27709 28350 28643 28337 28621 27569 28645
[229] 28651 27948 28630 27923 27929 27936 27943 28721 28512 27546 27891 28379
[241] 27822 27909 28655 28662 27587 27589 28625 28742 28553 27941 28134 27043
[253] 27893 28328 28135 28007 28338 27110 28472 28756 28110 28519 27861 27407
[265] 28374 28211 28668 27214 27965 27949 27806 28340 27917 27288 27563 28669
[277] 27229 27283 27109 27843 27047 28303 28585 28676 28689 28305 28635 28640
[289] 27016 27863 27968 28528 27915 27981 28411 28577 27326 27954 28556 27105
[301] 27545 27813 27974 27301 28168 28670 28801 27050 28610 28665 28125 28538
[313] 27849 28036 28586 27801 27807 28904 27875 28557 27958 28468 27536 28213
[325] 28341 28747 28707 27262 28006 28360 28031 27845 28166 28616 27572 27014
[337] 27503 27011 28572 28386 27291 28432 27804 27343 28073 28467 28173 28539
[349] 28352 27828 28515 28555 27855 27583 28310 28396 28348 28138 28642 27542
[361] 27408 28215 27821 28105 28270 28206 28301 27876 28627 27019 28574 28647
[373] 28806 27349 28091 28660 28726 28508 27840 28803 28511 27964 27978 28086
[385] 27927 28774 28383 27559 28523 28332 28749 27962 27455 28056 27501 28027
[397] 27527 27282 27837 28682 27310 28356 27233 27231 27006 28144 27857 27042
[409] 28314 27612 28525 27281 28147 28366 28629 27523 27937 28119 28012 27048
[421] 27880 27350 27027 27606 27938 28638 28720 28580 27103 27986 28001 28034
[433] 28393 28032 28040 28677 28395 28391 28678 28399 28455 28098 28401 28103
[445] 28684 28685 28409 28071 28683 28083 28708 28097 28450 28431 28453 28454
[457] 28709 28439 28377 28715 28443 28436 28438 28751 28129 28133 28763 28109
[469] 28120 28466 28746 28137 28480 28759 28731 28762 28405 28054 28698 28081
[481] 28403 28052 28701 28690 28412 28704 28078 28421 28693 28544 28516 28773
[493] 28775 28905 28174 28203 28570 28208 28210 28202 28804 28805 28791 28901
[505] 28547 28107 28722 28729 28461 28730 28463 28552 28554 28115 28732 28112
[517] 28214 28733 28308 28304 28571 28584 28582 28583 28273 28587 28278 28578
[529] 28579 28323 28164 28605 28518 28520 28526 28783 28529 28167 28521 28531
[541] 28311 28163 28537 28772 28626 27942 27928 28634 28649 28339 28357 27935
[553] 28623 28618 28654 28624 28619 27922 28307 28226 27946 27947 28347 28349
[565] 28227 28637 27926 27920 28646 28573 27921 28351 28269 28590 27341 28364
[577] 27604 27976 28615 27357 28344 28613 28609 28343 27409 27376 27377 27701
[589] 27610 27979 27405 27704 27705 27959 27960 27403 27966 27953 27970 27972
[601] 27973 27707 27957 27401 27517 27502 27507 27508 27509 27510 27518 27505
[613] 27020 27613 27024 27514 27519 27713 27614 27803 27616 27617 27513 27511
[625] 27023 27046 27844 27869 27853 27051 27041 27521 27871 27872 27842 27106
[637] 27830 27846 27013 27862 27104 27832 27847 27858 27865 27851 27825 27829
[649] 27012 27816 27817 27557 27808 27209 27208 27820 27888 27814 27551 27556
[661] 27045 27235 27560 27215 27054 27248 27242 27260 27243 27258 27581 27812
[673] 27601 27592 27591 27544 27316 27313 27325 27314 27311 27896 27007 28650
[685] 28606 27009 28735 28673 28725 28033 27870 27864 28429 28384 28663 27022
[697] 28333 27574 28524 28527 28277 27263 28023 27573 27615 28020 28464 28128
[709] 28009 28205 28104 27299 27884 28076 28080 28160 28532 27302 28124 27932
[721] 27924 28037 27819 27608 28789 28079 28398 27553 27878 27018 27040 28392
[733] 27315 28594 27950 28442 27410 27805 28371 27305 28778 28692 28072 28456
[745] 28589 28363 27355 27358 28385 28736 27890 27522 28617 28671 28387 28390
[757] 27212 27609 27568 28679 27881 27101 28622 28644 28631 28636 28373 28345
[769] 27712 28117 27866 27021 27406 28741 28372 27897 28430 27980 28017 27203
[781] 28909 27127 27607 27939 28217 28216 27252 28423 28718 27919 28510 28460
[793] 28434 28470 28766 28546 27818 27529 28469 28016 28075 28318 27107 27356
[805] 28315 27571 27860 28902

# Glimpse the nc_income data
glimpse(nc_income)

# Summarize the nc_income data
summary(nc_income)

# Left join nc_income onto shp@data and store in shp_nc_income
shp_nc_income <- shp@data %>% 
                left_join(nc_income, by = c("GEOID10" = "zipcode"))

# Print the number of missing values of each variable in shp_nc_income
shp_nc_income  %>%
  summarize(across(everything(), ~sum(is.na(.x))))

# summarize the mean income variable
summary(shp$mean_income)

# subset shp to include only zip codes in the top quartile of mean income
high_inc <- shp[!is.na(shp$mean_income) & shp$mean_income > 55917,]

# map the boundaries of the zip codes in the top quartile of mean income
high_inc %>%
  leaflet() %>%
  addTiles() %>%
  addPolygons()

# create color palette with colorNumeric()
nc_pal <- colorNumeric("YlGn", domain = high_inc@data$mean_income)

high_inc %>%
  leaflet() %>%
  addTiles() %>%
  # set boundary thickness to 1 and color polygons
  addPolygons(weight = 1, color = ~nc_pal(mean_income),
              # add labels that display mean income
              label = ~paste0("Mean Income: ", dollar(mean_income)),
              # highlight polygons on hover
              highlight = highlightOptions(weight = 5, color = "white",
              bringToFront = TRUE))

# Use the log function to create a new version of nc_pal
nc_pal <- colorNumeric("YlGn", domain = log(high_inc@data$mean_income))

# comment out the map tile
high_inc %>%
  leaflet() %>%
  #addProviderTiles("CartoDB") %>%
  # apply the new nc_pal to the map
  addPolygons(weight = 1, color = ~nc_pal(log(mean_income)), fillOpacity = 1,
              label = ~paste0("Mean Income: ", dollar(mean_income)),
              highlightOptions = highlightOptions(weight = 5, color = "white", bringToFront = TRUE))

# Print a summary of the `mean_income` variable
summary(wealthy_zips@data$mean_income)

# plot zip codes with mean incomes >= $200k
wealthy_zips %>% 
  leaflet() %>% 
  addProviderTiles("CartoDB") %>% 
  # set color to green and create Wealth Zipcodes group
  addPolygons(weight = 1, fillOpacity = .7, color = "green",  group = "Wealthy Zipcodes", 
              label = ~paste0("Mean Income: ", dollar(mean_income)),
              highlightOptions = highlightOptions(weight = 5, color = "white", bringToFront = TRUE))

# Add polygons using wealthy_zips
final_map <- m4 %>% 
   addPolygons(data = wealthy_zips, weight = 1, fillOpacity = .5, color = "Grey",  group = "Wealthy Zip Codes", 
              label = ~paste0("Mean Income: ", dollar(mean_income)),
              highlightOptions = highlightOptions(weight = 5, color = "white", bringToFront = TRUE)) %>% 
    # Update layer controls including "Wealthy Zip Codes"
    addLayersControl(baseGroups = c("OSM", "Carto", "Esri"), 
                         overlayGroups = c("Public", "Private", "For-Profit", "Wealthy Zip Codes"))     

# Print and explore your very last map of the course!
final_map
  • AI Chat
  • Code