Created
February 28, 2023 02:05
-
-
Save diamonaj/cf0a4f5265d68d33bca3c63518052906 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ## ----------------------------------------------------------------------------------------------------------- | |
| library(maps) | |
| data(us.cities) | |
| head(us.cities) | |
| map(database = "usa") | |
| capitals <- subset(us.cities, capital == 2) # subset state capitals | |
| ## add points proportional to population using latitude and longitude | |
| points(x = capitals$long, y = capitals$lat, | |
| cex = capitals$pop / 500000, pch = 19) | |
| title("US state capitals") # add a title | |
| map(database = "state", regions = "California") | |
| cal.cities <- subset(us.cities, subset = (country.etc == "CA")) | |
| sind <- order(cal.cities$pop, decreasing = TRUE) # order by population | |
| top7 <- sind[1:7] # seven cities with largest population | |
| map(database = "state", regions = "California") | |
| points(x = cal.cities$long[top7], y = cal.cities$lat[top7], pch = 19) | |
| ## add a constant to latitude to avoid overlapping with circles | |
| text(x = cal.cities$long[top7] + 2.25, y = cal.cities$lat[top7], | |
| label = cal.cities$name[top7]) | |
| title("Largest cities of California") | |
| usa <- map(database = "usa", plot = FALSE) # save map | |
| names(usa) # list elements | |
| length(usa$x) | |
| head(cbind(usa$x, usa$y)) # first five coordinates of a polygon | |
| ## ----------------------------------------------------------------------------------------------------------- | |
| allcolors <- colors() | |
| head(allcolors) # some colors | |
| length(allcolors) # number of color names | |
| red <- rgb(red = 1, green = 0, blue = 0) # red | |
| green <- rgb(red = 0, green = 1, blue = 0) # green | |
| blue <- rgb(red = 0, green = 0, blue = 1) # blue | |
| c(red, green, blue) # results | |
| black <- rgb(red = 0, green = 0, blue = 0) # black | |
| white <- rgb(red = 1, green = 1, blue = 1) # white | |
| c(black, white) # results | |
| rgb(red = c(0.5, 1), green = c(0, 1), blue = c(0.5, 0)) | |
| ## semi-transparent blue | |
| blue.trans <- rgb(red = 0, green = 0, blue = 1, alpha = 0.5) | |
| ## semi-transparent black | |
| black.trans <- rgb(red = 0, green = 0, blue = 0, alpha = 0.5) | |
| ## completely colored dots; difficult to distinguish | |
| plot(x = c(1, 1), y = c(1, 1.2), xlim = c(0.5, 4.5), ylim = c(0.5, 4.5), | |
| pch = 16, cex = 5, ann = FALSE, col = black) | |
| points(x = c(3, 3), y = c(3, 3.2), pch = 16, cex = 5, col = blue) | |
| ## semi-transparent; easy to distinguish | |
| points(x = c(2, 2), y = c(2, 2.2), pch = 16, cex = 5, col = black.trans) | |
| points(x = c(4, 4), y = c(4, 4.2), pch = 16, cex = 5, col = blue.trans) | |
| ## ----------------------------------------------------------------------------------------------------------- | |
| pres08 <- read.csv("pres08.csv") | |
| ## two-party vote share | |
| pres08$Dem <- pres08$Obama / (pres08$Obama + pres08$McCain) | |
| pres08$Rep <- pres08$McCain / (pres08$Obama + pres08$McCain) | |
| ## color for California | |
| cal.color <- rgb(red = pres08$Rep[pres08$state == "CA"], | |
| blue = pres08$Dem[pres08$state == "CA"], | |
| green = 0) | |
| ## California as a blue state | |
| map(database = "state", regions = "California", col = "blue", | |
| fill = TRUE) | |
| ## California as a purple state | |
| map(database = "state", regions = "California", col = cal.color, | |
| fill = TRUE) | |
| ## America as red and blue states | |
| map(database = "state") # create a map | |
| map(database = "state") # for some reason this needs to be repeated twice to get the map correct if you run markdown | |
| for (i in 1:nrow(pres08)) { | |
| if ((pres08$state[i] != "HI") & (pres08$state[i] != "AK") & | |
| (pres08$state[i] != "DC")) { | |
| maps::map(database = "state", regions = pres08$state.name[i], | |
| col = ifelse(pres08$Rep[i] > pres08$Dem[i], "red", "blue"), | |
| fill = TRUE, add = TRUE) | |
| } | |
| } | |
| ## America as purple states | |
| map(database = "state") # create a map | |
| for (i in 1:nrow(pres08)) { | |
| if ((pres08$state[i] != "HI") & (pres08$state[i] != "AK") & | |
| (pres08$state[i] != "DC")) { | |
| map(database = "state", regions = pres08$state.name[i], | |
| col = rgb(red = pres08$Rep[i], blue = pres08$Dem[i], | |
| green = 0), fill = TRUE, add = TRUE) | |
| } | |
| } | |
| ## ----------------------------------------------------------------------------------------------------------- | |
| walmart <- read.csv("walmart.csv") | |
| ## red = WalMartStore, green = SuperCenter, blue = DistributionCenter | |
| walmart$storecolors <- NA # create an empty vector | |
| walmart$storecolors[walmart$type == "Wal-MartStore"] <- | |
| rgb(red = 1, green = 0, blue = 0, alpha = 1/3) | |
| walmart$storecolors[walmart$type == "SuperCenter"] <- | |
| rgb(red = 0, green = 1, blue = 0, alpha = 1/3) | |
| walmart$storecolors[walmart$type == "DistributionCenter"] <- | |
| rgb(red = 0, green = 0, blue = 1, alpha = 1/3) | |
| ## larger circles for DistributionCenter | |
| walmart$storesize <- ifelse(walmart$type == "DistributionCenter", 1, 0.5) | |
| ## map with legend | |
| map(database = "state") | |
| points(walmart$long, walmart$lat, col = walmart$storecolors, | |
| pch = 19, cex = walmart$storesize) | |
| legend(x = -120, y = 32, bty = "n", | |
| legend = c("Wal-Mart", "Supercenter", "Distrib. Center"), | |
| col = c("red", "green", "blue"), pch = 19, # solid circles | |
| pt.cex = c(0.5, 0.5, 1)) # size of circles | |
| ### Section 5.3.6: Animation in R | |
| walmart.map <- function(data, date) { | |
| walmart <- subset(data, subset = (opendate <= date)) | |
| map(database = "state") | |
| points(walmart$long, walmart$lat, col = walmart$storecolors, | |
| pch = 19, cex = walmart$storesize) | |
| } | |
| walmart$opendate <- as.Date(walmart$opendate) | |
| walmart.map(walmart, as.Date("1974-12-31")) | |
| title("1975") | |
| walmart.map(walmart, as.Date("1984-12-31")) | |
| title("1985") | |
| walmart.map(walmart, as.Date("1994-12-31")) | |
| title("1995") | |
| walmart.map(walmart, as.Date("2004-12-31")) | |
| title("2005") | |
| n <- 25 # number of maps to animate | |
| dates <- seq(from = min(walmart$opendate), | |
| to = max(walmart$opendate), length.out = n) | |
| ## library("animation") | |
| ## saveHTML({ | |
| ## for (i in 1:length(dates)) { | |
| ## walmart.map(walmart, dates[i]) | |
| ## title(dates[i]) | |
| ## } | |
| ## }, title = "Expansion of Walmart", htmlfile = "walmart.html", | |
| ## outdir = getwd(), autobrowse = FALSE) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment