November 21, 2018

R US map plot

if (!require(zipcode)) install.packages('zipcode')
library(zipcode)

if (!require(plyr)) install.packages('plyr')
library(plyr)

if (!require(maps)) install.packages('maps')
library(maps)

if (!require(ggplot2)) install.packages('ggplot2')
library(ggplot2)

# file containing zip codes and population in each
locs <- read.csv("~/R_work/loc.csv", header=TRUE, sep=",")
data(zipcode)
loc$zip <- clean.zipcodes(locs$zip) # clean zipcodes
locs <- merge(locs, zipcode, by.x = 'zip', by.y ='zip') # merge 2 data frames

# mainland locs
locs_us <- locs[locs$state %in% c('AL','AZ','AR','CA','CO','CT','DE','FL','GA', 'ID','IL','IN','IA','KS','KY','LA','ME','MD','MA','MI','MN','MS','MO','MT','NE','NV','NH','NJ','NM','NY','NC','ND','OH','OK','OR','PA','RI','SC','SD','TN','TX','UT','VT','VA','WA','WV','WI','WY'), ]

install.packages("maps")
install.packages("ggplot2")
library(maps)
library(ggplot2)

all_states <- map_data("state")
p <- ggplot()
p <- p + geom_polygon(data = all_states,
                         aes(x=long, y=lat, group=group),
                         color="white")
p <- p +
  geom_point(data = locs_us,
             aes(x=longitude, y=latitude, size=population_count),
             color="red") +
  scale_size(name = "Title")

p


No comments:

Post a Comment