Skip to content

Commit

Permalink
Updating county files with FIPS openjournals/joss-reviews#4015 (comment)
Browse files Browse the repository at this point in the history
  • Loading branch information
Zarrar Khan committed Jan 22, 2022
1 parent 95fad52 commit 2a23779
Show file tree
Hide file tree
Showing 12 changed files with 113 additions and 35 deletions.
Binary file added cheatsheet.pdf
Binary file not shown.
Binary file modified data/mapIntersectGCAMBasinUS52County.rda
Binary file not shown.
Binary file modified data/mapIntersectGCAMBasinUS52Countydf.rda
Binary file not shown.
Binary file modified data/mapUS49County.rda
Binary file not shown.
Binary file modified data/mapUS49Countydf.rda
Binary file not shown.
Binary file modified data/mapUS52County.rda
Binary file not shown.
Binary file modified data/mapUS52CountyCompact.rda
Binary file not shown.
Binary file modified data/mapUS52CountyCompactdf.rda
Binary file not shown.
Binary file modified data/mapUS52Countydf.rda
Binary file not shown.
16 changes: 11 additions & 5 deletions inst/extras/devTests.R
Original file line number Diff line number Diff line change
Expand Up @@ -339,13 +339,19 @@ mydata <- ncovr %>%

# Will give you the relevant plot but multiple counties
# See how you can define your own columns as arguments
rmap::map(mydata)
rmap::map(mydata,
subRegCol = "NAME",
valueCol = "HR60")

# Here you can see all the multiple counties. rmap appends the State for you as it recognizes this.
# Here you can see all the multiple counties labelled.
# rmap appends the State for you as it recognizes this.
# You can also see some of the other features of rmap here
# such as underLayer, zoom, labels etc.
rmap::map(mydata,
subRegCol="NAME",
valueCol="HR60",
labels = T,
labelSize = 3,
labelRepel = T,
subRegCol="NAME",
valueCol="HR60",
underLayer = rmap::mapUS49)
underLayer = rmap::mapUS49,
zoom=-2)
105 changes: 101 additions & 4 deletions inst/extras/saveDataFiles.R
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,20 @@ if(redoMaps){
mapStates@data <- mapStates@data %>%
dplyr::mutate(name="mapStates")
use_data(mapStates, version=3, overwrite=T)

mapx <- rmap::mapStates
mapx <- mapx[!is.na(mapx@data$subRegion),]
mapx@data <- mapx@data%>%droplevels()
mapx@data %>% filter(is.na(subRegion))
nrow(mapx)
mapx@data <- mapx@data %>%
dplyr::mutate(subRegion = if_else(is.na(subRegion),subRegionAlt,subRegion)) %>%
dplyr::filter(!is.na(subRegion));
mapx@data %>% filter(is.na(subRegion))
nrow(mapx)
mapStates <- mapx
use_data(mapStates, version=3, overwrite=T)

}

#-----------------
Expand Down Expand Up @@ -772,6 +786,27 @@ if(redoMaps){
mapUS52County@data <- mapUS52County@data %>%
dplyr::mutate(name="mapUS52County")
use_data(mapUS52County, version=3, overwrite=T)

# Create FIPS
mapx <- rmap::mapUS52County
mapx@data <- mapx@data %>%
dplyr::mutate(subRegion_cc = paste0(subRegion,COUNTYCODE))
subRegions_duplicated <- (mapx@data$subRegion)[(mapx@data$subRegion)%>%duplicated()]; subRegions_duplicated
countycodes_remove <- mapx@data %>%
dplyr::select(subRegion,subRegionAlt,COUNTYCODE, area_sqkm, subRegion_cc) %>%
dplyr::filter(subRegion %in% subRegions_duplicated)%>%
dplyr::group_by(subRegionAlt) %>%
dplyr::mutate(max_area = max(area_sqkm)) %>%
dplyr::filter(area_sqkm != max_area); countycodes_remove

mapx@data <- mapx@data %>%
dplyr::mutate(
subRegion = if_else(subRegion_cc %in% countycodes_remove$subRegion_cc,paste0(subRegion,"_city"),subRegion),
FIPS = paste0(STATEFP,COUNTYCODE))
mapUS52County <- mapx
subRegions_duplicated <- (mapx@data$subRegion)[(mapx@data$subRegion)%>%duplicated()]; subRegions_duplicated
use_data(mapUS52County, version=3, overwrite=T)

}

# US 52 Counties with Alaska (AK), Hawaii (HI) and Puerto Rico (PR) shrunken and shifted
Expand All @@ -794,19 +829,19 @@ if(redoMaps){
alaska <- elide(alaska, scale=max(apply(bbox(alaska), 1, diff)) / 2.3)
alaska <- elide(alaska, shift=c(-2100000, -2500000))
proj4string(alaska) <- proj4string(us_aea)
rmap::rmap::map(alaska)
rmap::map(alaska)
# extract, then rotate & shift hawaii
hawaii <- us_aea[us_aea$STATENAME=="Hawaii",]
hawaii <- elide(hawaii, rotate=-35)
hawaii <- elide(hawaii, shift=c(5400000, -1400000))
proj4string(hawaii) <- proj4string(us_aea)
rmap::rmap::map(hawaii)
rmap::map(hawaii)
# extract, then rotate & shift Puerto Rico
pr <- us_aea[us_aea$STATENAME=="Puerto Rico",]
#pr <- elide(pr, rotate=-35)
pr <- elide(pr, shift=c(-2500000,0))
proj4string(pr) <- proj4string(us_aea);
rmap::rmap::map(pr)
rmap::map(pr)
# remove old states and put new ones back in; note the different order
# we're also removing puerto rico in this example but you can move it
# between texas and florida via similar methods to the ones we just used
Expand All @@ -821,6 +856,27 @@ if(redoMaps){
mapUS52CountyCompact@data <- mapUS52CountyCompact@data %>%
dplyr::mutate(name="mapUS52CountyCompact")
use_data(mapUS52CountyCompact, version=3, overwrite=T)

# Create FIPS
mapx <- rmap::mapUS52CountyCompact
mapx@data <- mapx@data %>%
dplyr::mutate(subRegion_cc = paste0(subRegion,COUNTYCODE))
subRegions_duplicated <- (mapx@data$subRegion)[(mapx@data$subRegion)%>%duplicated()]; subRegions_duplicated
countycodes_remove <- mapx@data %>%
dplyr::select(subRegion,subRegionAlt,COUNTYCODE, area_sqkm, subRegion_cc) %>%
dplyr::filter(subRegion %in% subRegions_duplicated)%>%
dplyr::group_by(subRegionAlt) %>%
dplyr::mutate(max_area = max(area_sqkm)) %>%
dplyr::filter(area_sqkm != max_area); countycodes_remove

mapx@data <- mapx@data %>%
dplyr::mutate(
subRegion = if_else(subRegion_cc %in% countycodes_remove$subRegion_cc,paste0(subRegion,"_city"),subRegion),
FIPS = paste0(STATEFP,COUNTYCODE))
mapUS52CountyCompact <- mapx
subRegions_duplicated <- (mapx@data$subRegion)[(mapx@data$subRegion)%>%duplicated()]; subRegions_duplicated
use_data(mapUS52CountyCompact, version=3, overwrite=T)

}

# US 49 Counties
Expand All @@ -842,7 +898,28 @@ if(redoMaps){
mapUS49County@data <- mapUS49County@data %>%
dplyr::mutate(name="mapUS49County")
use_data(mapUS49County, version=3, overwrite=T)
}

# Create FIPS
mapx <- rmap::mapUS49County
mapx@data <- mapx@data %>%
dplyr::mutate(subRegion_cc = paste0(subRegion,COUNTYCODE))
subRegions_duplicated <- (mapx@data$subRegion)[(mapx@data$subRegion)%>%duplicated()]; subRegions_duplicated
countycodes_remove <- mapx@data %>%
dplyr::select(subRegion,subRegionAlt,COUNTYCODE, area_sqkm, subRegion_cc) %>%
dplyr::filter(subRegion %in% subRegions_duplicated)%>%
dplyr::group_by(subRegionAlt) %>%
dplyr::mutate(max_area = max(area_sqkm)) %>%
dplyr::filter(area_sqkm != max_area); countycodes_remove

mapx@data <- mapx@data %>%
dplyr::mutate(
subRegion = if_else(subRegion_cc %in% countycodes_remove$subRegion_cc,paste0(subRegion,"_city"),subRegion),
FIPS = paste0(STATEFP,COUNTYCODE))
mapUS49County <- mapx
subRegions_duplicated <- (mapx@data$subRegion)[(mapx@data$subRegion)%>%duplicated()]; subRegions_duplicated
use_data(mapUS49County, version=3, overwrite=T)

}


# Merge
Expand Down Expand Up @@ -1381,3 +1458,23 @@ if(F){

}

#-----------
# Test for duplicate subRegions
#----------

if(F){
library(rmap);

# Plotting
#-------------
# World
for(i in c("mapCountries","mapStates","mapGCAMReg32","mapGCAMBasins","mapUS52","mapUS52County",
"mapUS52CountyCompact","mapUS49","mapUS49County","mapGCAMReg32US52",
"mapIntersectGCAMBasinCountry")){
mapx = get(i)
subRegions_duplicated <- (mapx@data$subRegion)[(mapx@data$subRegion)%>%duplicated()]; subRegions_duplicated
print(i)
print(subRegions_duplicated)
}

}
27 changes: 1 addition & 26 deletions inst/extras/saveDataFilesDF.R
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ mapUS52Countydf <- rmap::tidy_shape(shape=mapx, shapeColum="subRegion") %>%
mapUS52Countydf %>% head()
use_data(mapUS52Countydf, version=3, overwrite=T)


# US 52 Counties with Alaska (AK), Hawaii (HI) and Puerto Rico (PR) shrunken and shifted
#-------------------
mapx <- rmap::mapUS52CountyCompact
Expand Down Expand Up @@ -294,14 +295,6 @@ mapIntersectGCAMBasinUS52Countydf <- rmap::tidy_shape(shape=mapx, shapeColum="su
mapIntersectGCAMBasinUS52Countydf %>% head()
use_data(mapIntersectGCAMBasinUS52Countydf, version=3, overwrite=T)

# Intersection of GCAM Basins and US 52 County
mapx <- rmap::mapIntersectGCAMBasinUS52County
mapIntersectGCAMBasinUS52Countydf <- rmap::tidy_shape(shape=mapx, shapeColum="subRegion") %>%
dplyr::rename(subRegion=id)%>%
dplyr::inner_join(mapx@data, by="subRegion") %>% dplyr::rename(lon=long) %>% dplyr::mutate(name = paste0(name,"df"));
mapIntersectGCAMBasinUS52Countydf %>% head()
use_data(mapIntersectGCAMBasinUS52Countydf, version=3, overwrite=T)

# Intersection of GCAM Basins and US 52 States
mapx <- rmap::mapIntersectGCAMBasin32RegUruguay
mapIntersectGCAMBasin32RegUruguaydf <- rmap::tidy_shape(shape=mapx, shapeColum="subRegion") %>%
Expand Down Expand Up @@ -403,15 +396,6 @@ if(T){
"subRegGCAMBasinsUS52" =
tolower(rmap::mapGCAMBasinsUS52@data$subRegion %>% unique() %>% as.character %>%
sort()),
"subRegGCAMLand" =
tolower(rmap::mapGCAMLand@data$subRegion %>% unique() %>% as.character %>%
sort()),
"subRegGCAMLandUS49" =
tolower(rmap::mapGCAMLandUS49@data$subRegion %>% unique() %>% as.character %>%
sort()),
"subRegGCAMLandUS52" =
tolower(rmap::mapGCAMLandUS52@data$subRegion %>% unique() %>% as.character %>%
sort()),
"subRegUS49HUC2" =
tolower(rmap::mapUS49HUC2@data$subRegion %>% unique() %>% as.character %>%
sort()),
Expand Down Expand Up @@ -475,15 +459,6 @@ if(T){
"subRegGCAMBasinsUS52Alt" =
tolower(rmap::mapGCAMBasinsUS52@data$subRegionAlt %>% unique() %>% as.character %>%
sort()),
"subRegGCAMLandAlt" =
tolower(rmap::mapGCAMLand@data$subRegionAlt %>% unique() %>% as.character %>%
sort()),
"subRegGCAMLandUS49Alt" =
tolower(rmap::mapGCAMLandUS49@data$subRegionAlt %>% unique() %>% as.character %>%
sort()),
"subRegGCAMLandUS52Alt" =
tolower(rmap::mapGCAMLandUS52@data$subRegionAlt %>% unique() %>% as.character %>%
sort()),
"subRegUS49HUC2Alt" =
tolower(rmap::mapUS49HUC2@data$subRegionAlt %>% unique() %>% as.character %>%
sort()),
Expand Down

0 comments on commit 2a23779

Please sign in to comment.