Lecture 16 Spatial Data and Cartography (Part 2) 3/22/2018 1
Plotting 2
Example Data - NC SIDS 1237. (((-76.74506 36.23392, -~ 2. 350. 115. 0. 286. 7 Camd~ ## 5. ## 954. 1838. 7. 6 Hert~ 1452. ## 1197. (((-77.21767 36.24098, -~ 3. 1066. 1606. 9. 139. (((-76.00897 36.3196, -7~ 8 Gates ## 748. 1190. ## # ... with 90 more rows 176. (((-80.02567 36.25023, -~ 5. 160. 2038. 1. ## 10 Stok~ 1612. 844. (((-78.30876 36.26004, -~ 2. 4. 420. 968. 9 Warr~ ## 371. (((-76.56251 36.34057, -~ 2. 594. 254. 0. 5 Nort~ 1421. 145. (((-76.00897 36.3196, -7~ nc = st_read ( system.file (”shape/nc.shp”, package=”sf”), quiet = TRUE) %>% <dbl> <dbl> <dbl> 10. 1364. 1. 1091. 1 Ashe ## <MULTIPOLYGON [°]> <dbl> <fct> <dbl> <dbl> 19. (((-81.47276 36.23436, -~ ## geometry BIR74 SID74 NWBIR74 BIR79 SID79 NWBIR79 NAME ## ## # A tibble: 100 x 8 tbl_df (nc) select (-(AREA:CNTY_ID), -(FIPS:CRESS_ID)) 0. ## 2. 208. 3616. 830. 123. 1. 508. 4 Curr~ ## 260. (((-80.45634 36.24256, -~ 6. 5. 2 Alle~ 3 Surry 3188. ## 12. (((-81.23989 36.36536, -~ 3. 542. 10. 0. 487. 3
Base Plots plot (nc) 4 NAME BIR74 SID74 NWBIR74 BIR79 SID79 NWBIR79
Geometry Plot plot ( st_geometry (nc), axes=TRUE) 5 37 ° N 36.5 ° N 36 ° N 35.5 ° N 35 ° N 34.5 ° N 34 ° N 33.5 ° N 84 ° W 82 ° W 80 ° W 78 ° W 76 ° W
Graticules plot (nc[,”SID79”], graticule= st_crs (nc), axes=TRUE) 6 SID79 37 ° N 36.5 ° N 36 ° N 35.5 ° N 35 ° N 34.5 ° N 34 ° N 33.5 ° N 84 ° W 82 ° W 80 ° W 78 ° W 76 ° W 0 10 20 30 40 50 60
Graticules (EPSG:3631) plot ( st_transform (nc[,”SID79”], 3631), graticule= st_crs (nc), axes=TRUE) 7 SID79 37 ° N 36.5 ° N 36 ° N 35.5 ° N 35 ° N 34.5 ° N 34 ° N 33.5 ° N 84 ° W 82 ° W 80 ° W 78 ° W 76 ° W 0 10 20 30 40 50 60
ggplot2 (dev) devtools:: install_github (”tidyverse/ggplot2”) 8 ggplot (nc) + geom_sf ( aes (fill=SID79)) 36.5 ° N SID79 36 ° N 50 35.5 ° N 40 30 35 ° N 20 10 34.5 ° N 0 34 ° N 84 ° W 82 ° W 80 ° W 78 ° W 76 ° W
ggplot2 + projections ggplot ( st_transform (nc, 3631)) + 9 geom_sf ( aes (fill=SID79 / BIR79)) 36.5 ° N SID79/BIR79 36 ° N 0.006 35.5 ° N 0.004 35 ° N 0.002 34.5 ° N 0.000 34 ° N 84 ° W 82 ° W 80 ° W 78 ° W 76 ° W
Example Data - Meuse 0 8 ## 0 2 9.20 1 8.22 0.190 346. 132. 31. 3.20 7 ## 2 29. 7.80 1 7.79 0.364 281. 137. 61. 3.00 6 ## 0 2 8.70 1 7.48 0.277 269. 2.80 150. 48. 0 dist.m <dbl>, geometry <POINT [m]> ## # ## # ... with 145 more rows, and 3 more variables: landuse <fct>, 0 2 6.30 1 9.05 0.310 183. 80. 24. 1.60 ## 10 1 406. 1 10.6 8.67 0.185 347. 133. 37. 2.40 9 ## 0 1 9.50 1 8.49 0.0922 117. 2.80 data (meuse, meuse.riv, package=”sp”) <dbl> <dbl> <dbl> <dbl> 2 ## 1 1 1 7.91 0.00136 13.6 299. 1022. 85. 11.7 1 ## <dbl> <dbl> <fct> <fct> <fct> <dbl> 5 * ## lime om ffreq soil dist elev zinc lead cadmium copper ## ## # A tibble: 155 x 13 tbl_df (meuse) 8.60 81. 277. 1141. 6.98 0.0122 ## 0 2 8.00 1 7.66 0.190 257. 116. 81. 2.60 4 ## 1 1 1 13.0 7.80 0.103 640. 199. 68. 6.50 3 ## 1 1 1 14.0 10 meuse = st_as_sf (meuse, coords= c (”x”, ”y”), crs=28992) meuse_riv = st_polygon ( list (meuse.riv)) %>% st_sfc () %>% st_set_crs (28992)
Meuse plot (meuse, pch=16) 11 cadmium copper lead zinc elev dist om ffreq soil lime
Layering plots plot (meuse[,”lead”], pch=16, axes=TRUE) 12 plot (meuse_riv, col= adjustcolor (”lightblue”, alpha.f=0.5), add=TRUE, border = NA) lead 700 600 333000 500 332000 400 300 331000 200 330000 100 0 177000 178000 179000 180000 181000 182000 183000
Layering plots (oops) plot (meuse, pch=16) plot (meuse_riv, col= adjustcolor (”lightblue”, alpha.f=0.5), add=TRUE, border = NA) 13 cadmium copper lead zinc elev dist om ffreq soil lime
ggplot2 ggplot () + 14 geom_sf (data=meuse, aes (color=lead), size=1) geom_sf (data= st_sf (meuse_riv), fill=”lightblue”, color=NA) + 51.02 ° N 51 ° N lead 600 50.98 ° N 400 200 50.96 ° N 50.94 ° N 50.92 ° N 5.72 ° E 5.73 ° E 5.74 ° E 5.75 ° E 5.76 ° E 5.77 ° E
ggplot2 - axis limits ggplot () + geom_sf (data= st_sf (meuse_riv), fill=”lightblue”, color=NA) + geom_sf (data=meuse, aes (color=lead), size=1) + ylim (50.95, 50.99) 15 lead 600 400 5.7 ° E 5.71 ° E 5.72 ° E 5.73 ° E 5.74 ° E 5.75 ° E 200
ggplot2 - axis limits ylim (329714, 333611) ggplot () + 16 geom_sf (data=meuse, aes (color=lead), size=1) + geom_sf (data= st_sf (meuse_riv), fill=”lightblue”, color=NA) + 50.99 ° N 50.985 ° N 50.98 ° N lead 600 50.975 ° N 400 200 50.97 ° N 50.965 ° N 50.96 ° N 50.955 ° N 5.72 ° E 5.73 ° E 5.74 ° E 5.75 ° E 5.76 ° E 5.77 ° E
ggplot2 - bounding box ylim ( st_bbox (meuse)[”ymin”], st_bbox (meuse)[”ymax”]) ggplot () + 17 geom_sf (data=meuse, aes (color=lead), size=1) + geom_sf (data= st_sf (meuse_riv), fill=”lightblue”, color=NA) + 50.99 ° N 50.985 ° N 50.98 ° N lead 600 50.975 ° N 400 200 50.97 ° N 50.965 ° N 50.96 ° N 50.955 ° N 5.72 ° E 5.73 ° E 5.74 ° E 5.75 ° E 5.76 ° E 5.77 ° E
Geometry Manipulation 18
Casting ## 139. (-76.00897 36.3196, -75.~ 2. 350. 115. 0. 286. 7 Camd~ 1237. (-76.74506 36.23392, -76~ 8 Gates 5. 954. 1838. 7. 6 Hert~ 1452. ## 1197. (-77.21767 36.24098, -77~ 3. 1066. 1606. ## 420. 5 Nort~ 1421. 2. ## # ... with 90 more rows 176. (-80.02567 36.25023, -80~ 5. 160. 2038. 1. ## 10 Stok~ 1612. 844. (-78.30876 36.26004, -78~ 748. 1190. 0. 4. 968. 9 Warr~ ## 371. (-76.56251 36.34057, -76~ 2. 594. 254. 9. ## nc_pts = st_cast (nc, ”MULTIPOINT”) <dbl> 0. 10. 1364. 1. 1091. 1 Ashe ## <MULTIPOINT [°]> <dbl> <dbl> <dbl> ## * <fct> <dbl> <dbl> ## geometry BIR74 SID74 NWBIR74 BIR79 SID79 NWBIR79 NAME ## ## # A tibble: 100 x 8 tbl_df (nc_pts) 19. (-81.47276 36.23436, -81~ 2 Alle~ 145. (-76.00897 36.3196, -76.~ 6. 2. 830. 123. 1. 508. 4 Curr~ ## 260. (-80.45634 36.24256, -80~ 208. 3616. 487. 5. 3 Surry 3188. ## 12. (-81.23989 36.36536, -81~ 3. 542. 10. 0. 19
plot ( st_geometry (nc), border='grey') plot ( st_geometry (nc_pts), pch=16, cex=0.5, add=TRUE) 20
Casting - POINT 19 POINT (-81.69828 36.47178) POINT (-81.7028 36.51934) 19 0 1364 10 1 1091 Ashe ## 7 0 Ashe 1364 10 1 1091 Ashe ## 6 19 POINT (-81.74107 36.39178) 0 1364 10 ## 8 1091 1091 0 19 POINT (-81.34754 36.53791) 0 1364 10 1 1091 ## 10 Ashe POINT (-81.3453 36.57286) 19 1364 1 10 1 1091 Ashe ## 9 POINT (-81.67 36.58965) 19 0 1364 10 1 Ashe st_cast (nc, ”POINT”) ## First 10 features: 1364 10 1 1091 Ashe ## 1 geometry NAME BIR74 SID74 NWBIR74 BIR79 SID79 NWBIR79 ## +proj=longlat +datum=NAD27 +no_defs 19 POINT (-81.47276 36.23436) ## proj4string: 4267 ## epsg (SRID): xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965 ## bbox: XY ## dimension: POINT ## geometry type: ## Simple feature collection with 2529 features and 7 fields 0 ## 2 ## 5 1364 19 POINT (-81.63306 36.34069) 0 1364 10 1 1091 Ashe ## 4 19 POINT (-81.56198 36.27359) 0 10 Ashe 1 1091 Ashe ## 3 19 POINT (-81.54084 36.27251) 0 1364 10 1 1091 21
plot ( st_geometry (nc), border='grey') plot ( st_geometry ( st_cast (nc, ”POINT”)), pch=16, cex=0.5, add=TRUE) 22
Casting - LINESTRING ## 139. ((-76.00897 36.3196, -75~ 2. 350. 115. 0. 286. 7 Camd~ 1237. ((-76.74506 36.23392, -7~ 8 Gates 5. 954. 1838. 7. 6 Hert~ 1452. ## 1197. ((-77.21767 36.24098, -7~ 3. 1066. 1606. ## 420. 5 Nort~ 1421. 2. ## # ... with 90 more rows 176. ((-80.02567 36.25023, -8~ 5. 160. 2038. 1. ## 10 Stok~ 1612. 844. ((-78.30876 36.26004, -7~ 748. 1190. 0. 4. 968. 9 Warr~ ## 371. ((-76.56251 36.34057, -7~ 2. 594. 254. 9. ## st_cast (nc, ”MULTILINESTRING”) %>% as_tibble () <MULTILINESTRING [°]> 19. ((-81.47276 36.23436, -8~ 0. 10. 1364. 1. 1091. 1 Ashe ## <dbl> 2 Alle~ <dbl> <dbl> <dbl> * <fct> <dbl> <dbl> ## geometry BIR74 SID74 NWBIR74 BIR79 SID79 NWBIR79 NAME ## ## # A tibble: 100 x 8 ## 487. 145. ((-76.00897 36.3196, -76~ 260. ((-80.45634 36.24256, -8~ 2. 830. 123. 1. 508. 4 Curr~ ## 6. 0. 208. 3616. 5. 3 Surry 3188. ## 12. ((-81.23989 36.36536, -8~ 3. 542. 10. 23
st_cast (nc, ”MULTILINESTRING”) %>% st_geometry () %>% plot () 24
Grouping Features ## bbox: ## MULTIPOLYGON (((-76.54427 34.58783, -76.55515 3... +proj=longlat +datum=NAD27 +no_defs ## proj4string: 4267 ## epsg (SRID): xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965 XY nc_state = st_union (nc) ## dimension: MULTIPOLYGON ## geometry type: ## Geometry set for 1 feature nc_state plot (nc_state) 25
Recommend
More recommend