WS3 Geographic
WS3 Geographic
##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)
##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)
##
## Attaching package: 'viridis'
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 (%)")
##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>
## # 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
##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)
##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)
##
## Attaching package: 'acs'
library(latticeExtra)
##
## Attaching package: 'latticeExtra'
library(choroplethrMaps)
data(df_pop_county)
df_pop_county %>% slice(1:3)
## region value
## 1 1001 54590
## 2 1003 183226
## 3 1005 27469
library(sp)
denver_tracts <- tracts(state = "CO", county = 31, cb = TRUE,
class = "sp")
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)
head(denver_tracts@data)
denver_tracts_df %>%
dplyr::select(1:4) %>% dplyr::slice(1:5)
##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()