100% found this document useful (1 vote)
118 views18 pages

WS3 Geographic

This code loads data on cell tower locations and US county boundaries. It filters the cell tower data to include coordinates, joins it to the county boundary data, and plots the towers on a map of Maryland counties with points colored by tower type.

Uploaded by

GPA FOUR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
118 views18 pages

WS3 Geographic

This code loads data on cell tower locations and US county boundaries. It filters the cell tower data to include coordinates, joins it to the county boundary data, and plots the towers on a map of Maryland counties with points colored by tower type.

Uploaded by

GPA FOUR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

SYED FARHAN KHAJA

May 21, 2023

##This code loads the ggplot2 and dplyr packages for data visualization and manipulation.
It then retrieves data for the boundaries of each state in the United States using the
map_data() function from the maps package and assigns the resulting data frame to the
variable us_map. Finally, it displays the first three rows of the us_map data frame using the
head() function.

install.packages(“usmap”)

install.packages(“ggplot2”)

library(ggplot2)
library(dplyr)

us_map <- map_data("state")


head(us_map, 3)

## long lat group order region subregion


## 1 -87.46201 30.38968 1 1 alabama <NA>
## 2 -87.48493 30.37249 1 2 alabama <NA>
## 3 -87.52503 30.37249 1 3 alabama <NA>

##filters the data to only include the states of North Carolina and South Carolina using the
filter() function. It then creates a scatterplot of the latitude and longitude coordinates of the
two states using the ggplot() and geom_point() functions from the ggplot2 package.
us_map %>%
filter(region %in% c("north carolina", "south carolina")) %>%
ggplot(aes(x = long, y = lat)) +
geom_point()
##It filters the data
to only include the states of North Carolina and South Carolina using the filter() function. It
then creates a plot of the latitude and longitude coordinates of the boundaries of the two
states using the ggplot() and geom_path() functions from the ggplot2 package.
us_map %>%
filter(region %in% c("north carolina", "south carolina")) %>%
ggplot(aes(x = long, y = lat)) +
geom_path()
##
us_map %>%
filter(region %in% c("north carolina", "south carolina")) %>%
ggplot(aes(x = long, y = lat, group = group)) +
geom_path()
##This code uses
the %>% pipe operator to pass the us_map data frame through a series of operations. It
filters the data to only include the states of North Carolina and South Carolina using the
filter() function. It then creates a filled polygon plot of the latitude and longitude
coordinates of the boundaries of the two states using the ggplot() and geom_polygon()
functions from the ggplot2 package.
us_map %>%
filter(region %in% c("north carolina", "south carolina")) %>%
ggplot(aes(x = long, y = lat, group = group)) +
geom_polygon(fill = "lightblue", color = "black")
##This code uses
the %>% pipe operator to pass the us_map data frame through a series of operations. It
filters the data to only include the states of North Carolina and South Carolina using the
filter() function. It then creates a filled polygon plot of the latitude and longitude
coordinates of the boundaries of the two states using the ggplot() and geom_polygon()
functions, and removes the background and gridlines using the theme_void() function from
the ggplot2 package.
us_map %>%
filter(region %in% c("north carolina", "south carolina")) %>%
ggplot(aes(x = long, y = lat, group = group)) +
geom_polygon(fill = "lightblue", color = "black") +
theme_void()
us_map %>%
ggplot(aes(x = long, y = lat, group = group)) +
geom_polygon(fill = "lightblue", color = "black") +
theme_void()
##This code loads the maps package, which provides a collection of map data for various
regions of the world. It then loads the votes.repub data set, which is included in the maps
package and contains the results of the 1988 United States presidential election by county
for Republican candidate George H.W. Bush. Finally, it displays the first few rows of the
votes.repub data frame using the head() function to check the data.
library(maps)

data(votes.repub)
head(votes.repub)

## 1856 1860 1864 1868 1872 1876 1880 1884 1888 1892
1896
## Alabama NA NA NA 51.44 53.19 40.02 36.98 38.44 32.28 3.95
28.13
## Alaska NA NA NA NA NA NA NA NA NA NA
NA
## Arizona NA NA NA NA NA NA NA NA NA NA
NA
## Arkansas NA NA NA 53.73 52.17 39.88 39.55 40.50 38.07 32.01
25.11
## California 18.77 32.96 58.63 50.24 56.38 50.88 48.92 52.08 49.95 43.76
49.13
## Colorado NA NA NA NA NA NA 51.28 54.39 55.31 41.13
13.84
## 1900 1904 1908 1912 1916 1920 1924 1928 1932 1936
1940
## Alabama 34.67 20.65 24.38 8.26 21.97 30.98 27.01 48.49 14.15 12.82
14.34
## Alaska NA NA NA NA NA NA NA NA NA NA
NA
## Arizona NA NA NA 12.74 35.37 55.41 41.26 57.57 30.53 26.93
36.01
## Arkansas 35.04 40.25 37.31 19.73 28.01 38.73 29.28 39.33 12.91 17.86
20.87
## California 54.48 61.90 55.46 0.58 46.26 66.24 57.21 64.70 37.40 31.70
41.35
## Colorado 42.04 55.27 46.88 21.88 34.75 59.32 57.02 64.72 41.43 37.09
50.92
## 1944 1948 1952 1956 1960 1964 1968 1972 1976
## Alabama 18.20 19.04 35.02 39.39 41.75 69.5 14.0 72.4 43.48
## Alaska NA NA NA NA 50.94 34.1 45.3 58.1 62.91
## Arizona 40.90 43.82 58.35 60.99 55.52 50.4 54.8 64.7 58.62
## Arkansas 29.84 21.02 43.76 45.82 43.06 43.9 30.8 68.9 34.97
## California 42.99 47.14 56.39 55.40 50.10 40.9 47.8 55.0 50.89
## Colorado 53.21 46.52 60.27 59.49 54.63 38.7 50.5 62.6 55.89

##This code loads the dplyr and viridis packages for data manipulation and visualization. It
transforms the votes.repub data set into a tibble, adds a new column state with the state
names, joins the us_map data frame based on the state names, and creates a filled polygon
plot of the boundaries of the United States colored by the percentage of votes received by
Republican candidate Gerald Ford in the 1976 presidential election using the ggplot() and
geom_polygon() functions. Finally, it removes the background and gridlines using the
theme_void() function and applies a color scale using the scale_fill_viridis() function from
the viridis package.
library(dplyr)
library(viridis)

## Loading required package: viridisLite

##
## Attaching package: 'viridis'

## The following object is masked from 'package:maps':


##
## unemp

votes.repub %>%
tbl_df() %>%
mutate(state = rownames(votes.repub),
state = tolower(state)) %>%
right_join(us_map, by = c("state" = "region")) %>%
ggplot(aes(x = long, y = lat, group = group, fill = `1976`)) +
geom_polygon(color = "black") +
theme_void() +
scale_fill_viridis(name = "Republican\nvotes (%)")

## Warning: `tbl_df()` was deprecated in dplyr 1.0.0.


## ℹ Please use `tibble::as_tibble()` instead.

## Warning in right_join(., us_map, by = c(state = "region")): Each row in


`x` is expected to match at most 1 row in `y`.
## ℹ Row 1 of `x` matches multiple rows.
## ℹ If multiple matches are expected, set `multiple = "all"` to silence this
## warning.

##This code loads the readr package for data reading and manipulation. It reads the CSV
file located at a specific URL using the read_csv() function and saves it as a data frame
called serial. The head() function is then used to display the first three rows of the serial
data frame.
library(readr)
serial <- read_csv(paste0("https://raw.githubusercontent.com/",
"dgrtwo/serial-ggvis/master/input_data/",
"serial_podcast_data/serial_map_data.csv"))

## Rows: 25 Columns: 5
## ── Column specification
────────────────────────────────────────────────────────
## Delimiter: ","
## chr (3): Type, Name, Description
## dbl (2): x, y
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this
message.

head(serial, 3)

## # A tibble: 3 × 5
## x y Type Name Description
## <dbl> <dbl> <chr> <chr> <chr>
## 1 356 437 cell-site L688 <NA>
## 2 740 360 cell-site L698 <NA>
## 3 910 340 cell-site L654 <NA>

serial <- serial %>%


mutate(long = -76.8854 + 0.00017022 * x,
lat = 39.23822 + 1.371014e-04 * y,
tower = Type == "cell-site")
serial %>%
slice(c(1:3, (n() - 3):(n())))

## # A tibble: 7 × 8
## x y Type Name Descr…¹ long lat
tower
## <dbl> <dbl> <chr> <chr> <chr> <dbl> <dbl>
<lgl>
## 1 356 437 cell-site L688 <NA> -76.8 39.3
TRUE
## 2 740 360 cell-site L698 <NA> -76.8 39.3
TRUE
## 3 910 340 cell-site L654 <NA> -76.7 39.3
TRUE
## 4 960 830 base-location Campfield Early Learning … Hae wa… -76.7 39.4
FALSE
## 5 580 1230 base-location Owings Mills Mall Hae wo… -76.8 39.4
FALSE
## 6 720 496 base-location Adnan's house <NA> -76.8 39.3
FALSE
## 7 954 410 base-location Jenn's house <NA> -76.7 39.3
FALSE
## # … with abbreviated variable name ¹Description

maryland <- map_data('county', region = 'maryland')


head(maryland)

## long lat group order region subregion


## 1 -78.64992 39.53982 1 1 maryland allegany
## 2 -78.70148 39.55128 1 2 maryland allegany
## 3 -78.74159 39.57420 1 3 maryland allegany
## 4 -78.75878 39.58566 1 4 maryland allegany
## 5 -78.74732 39.61430 1 5 maryland allegany
## 6 -78.74732 39.63149 1 6 maryland allegany

##The code creates a new data frame called ‘baltimore’ that contains only the rows from
the ‘maryland’ data frame where the ‘subregion’ column matches either “baltimore city” or
“baltimore”. T
baltimore <- maryland %>%
filter(subregion %in% c("baltimore city", "baltimore"))
head(baltimore, 3)

## long lat group order region subregion


## 1 -76.88521 39.35074 3 114 maryland baltimore
## 2 -76.89094 39.37939 3 115 maryland baltimore
## 3 -76.88521 39.40804 3 116 maryland baltimore

##This code generates a plot of Baltimore, Maryland using the ggplot() function and data
from the baltimore data frame. The geom_polygon() function is used to draw polygons
based on the longitude and latitude coordinates of Baltimore, which are defined in the aes()
function. The plot is then modified using the theme_void() function to remove any
unnecessary background elements.
ggplot(baltimore, aes(x = long, y = lat, group = group)) +
geom_polygon(fill = "lightblue", color = "black") +
theme_void()

##This code creates a plot of Baltimore, Maryland using data from the baltimore data
frame. The plot is generated using the ggplot() function and geom_polygon() function to
draw polygons based on the longitude and latitude coordinates of Baltimore. The plot is
then modified to include points showing the location of cell towers in Baltimore, color-
coded according to their respective categories, using the geom_point() function and the
scale_color_manual() function
ggplot(baltimore, aes(x = long, y = lat, group = group)) +
geom_polygon(fill = "lightblue", color = "black") +
geom_point(data = serial, aes(group = NULL, color = tower)) +
theme_void() +
scale_color_manual(name = "Cell tower", values = c("black", "red"))

library(choroplethr)

## Loading required package: acs

## Loading required package: stringr

## Loading required package: XML

##
## Attaching package: 'acs'

## The following object is masked from 'package:dplyr':


##
## combine
## The following object is masked from 'package:base':
##
## apply

library(latticeExtra)

## Loading required package: lattice

##
## Attaching package: 'latticeExtra'

## The following object is masked from 'package:ggplot2':


##
## layer

library(choroplethrMaps)
data(df_pop_county)
df_pop_county %>% slice(1:3)

## region value
## 1 1001 54590
## 2 1003 183226
## 3 1005 27469

county_choropleth(df_pop_county, state_zoom = c("colorado", "wyoming"))

## Warning in private$zoom == "alaska" || private$zoom == "hawaii":


'length(x) = 2
## > 1' in coercion to 'logical(1)'

## Warning in private$zoom == "alaska" || private$zoom == "hawaii":


'length(x) = 2
## > 1' in coercion to 'logical(1)'
##This code produces a choropleth map of US counties based on population data stored in
the df_pop_county data frame, focusing on the state of North Carolina using the state_zoom
argument. The county_choropleth() function is likely a custom function that takes in the
data frame as its first argument, and the reference_map argument may be a logical value
indicating whether to include a reference map of the entire United States. The resulting
map would likely display North Carolina’s counties colored according to their respective
population sizes.
county_choropleth(df_pop_county, state_zoom = c("north carolina"),
reference_map = FALSE)
library(tigris)

## To enable caching of data, set `options(tigris_use_cache = TRUE)`


## in your R script or .Rprofile.

library(sp)
denver_tracts <- tracts(state = "CO", county = 31, cb = TRUE,
class = "sp")

## Warning: Spatial* (sp) classes are no longer formally


## supported in tigris as of version 2.0. We strongly
## recommend updating your workflow to use sf objects
## (the default in tigris) instead.

plot(denver_tracts)
bbox(denver_tracts)

## min max
## x -105.10992 -104.59958
## y 39.61433 39.91418

is.projected(denver_tracts)

## [1] FALSE

proj4string(denver_tracts)

## [1] "+proj=longlat +datum=NAD83 +no_defs"

head(denver_tracts@data)

## STATEFP COUNTYFP TRACTCE AFFGEOID GEOID NAME


## 7 08 031 001102 1400000US08031001102 08031001102 11.02
## 12 08 031 003300 1400000US08031003300 08031003300 33
## 23 08 031 002404 1400000US08031002404 08031002404 24.04
## 24 08 031 005001 1400000US08031005001 08031005001 50.01
## 25 08 031 002804 1400000US08031002804 08031002804 28.04
## 32 08 031 000301 1400000US08031000301 08031000301 3.01
## NAMELSAD STUSPS NAMELSADCO STATE_NAME LSAD ALAND AWATER
## 7 Census Tract 11.02 CO Denver County Colorado CT 1328939 0
## 12 Census Tract 33 CO Denver County Colorado CT 1288718 0
## 23 Census Tract 24.04 CO Denver County Colorado CT 371688 0
## 24 Census Tract 50.01 CO Denver County Colorado CT 1542074 0
## 25 Census Tract 28.04 CO Denver County

plot(denver_tracts, col = "lightblue")


plot(roads, add = TRUE, col = "darkred")

## Warning in plot.sf(roads, add = TRUE, col = "darkred"): ignoring all but


the
## first attribute

##This code first


converts the denver_tracts spatial object into a data frame using the fortify() function from
the ggplot2 package. It then selects the first four columns of the resulting data frame using
the dplyr::select() function, and takes the first five rows using the dplyr::slice() function.
The resulting data frame displays the first five rows of the latitude and longitude
coordinates of the tracts in Denver.
denver_tracts_df <- fortify(denver_tracts)

## Regions defined for each Polygons

denver_tracts_df %>%
dplyr::select(1:4) %>% dplyr::slice(1:5)

## long lat order hole


## 1 -105.0127 39.75537 1 FALSE
## 2 -105.0090 39.75809 2 FALSE
## 3 -105.0110 39.75964 3 FALSE
## 4 -105.0112 39.76204 4 FALSE
## 5 -105.0112 39.76564 5 FALSE

##This code creates a plot of the Denver metropolitan area using data from the
denver_tracts_df data frame. The plot is generated using the ggplot() function and
geom_polygon() function to draw polygons based on latitude and longitude coordinates of
the tracts in Denver. The plot is then modified to remove the background and gridlines
using the theme_void() function.
denver_tracts_df %>%
ggplot(aes(x = long, y = lat, group = group)) +
geom_polygon(fill = "lightblue", color = "black") +
theme_void()

You might also like