Object of class US
US.Rd
Creates an object with data and coordinate of class US
for United States of America to use with mapping
functions or available in other R "maps" packages.
Usage
US(data, colID = NULL,
unit = c("country","region", "division","state", "county",
"district", "district_county","urban_area"),
year = c("2018"),matchWith = c("name", "id", "number"),
scale = c("20", "50", "500"), show_us = TRUE,
subset = NULL, add = NULL, new_var_names = NULL,
aggregation_fun = sum, aggregation_unit = NULL, aggregation_var = NULL,
facets = NULL, check.unit.names = TRUE, dir = NULL, use_cache = TRUE,
print = FALSE, use_internet = TRUE, crs = NULL)
Arguments
- data
a data.frame object with variables to display
- colID
character value or column numbers indicating the column with unit names.
- unit
the type of US statistical unit
- year
year of the analysis
- matchWith
the type of id to check if unit is set to "states"
- scale
the scale of the map
- show_us
logical value set to TRUE indicating if the entire map is drawn or only the coordinates linked to the input data
- subset
a formula indicating the condition to subset the data. See the details.
- add
a formula to add new transformed variables starting from the variables in the
data
- new_var_names
a character value or vector indicating the names of the new variables created in
add
.- aggregation_fun
function to use when data are aggregated
- aggregation_unit
variable name by which the unit are aggregated
- aggregation_var
variable name with value to aggregate
- facets
variable(s) name to split the data
- check.unit.names
a logical value indicating if the
colID
names are checked withunit
names.- dir
local directory in which shape files are stored
- use_cache
a logical value indicating whether use the cache
a logical value indicating whether print the nomatched names
- use_internet
a logical value indicating wheter the coordinates are downloaded from https://github.com/mappinguniverse/geospatial. If
FALSE
the maps downloaded during package installation will be used- crs
coordinate reference system. Look at
st_crs
Details
The function links (automatically) the id in the data
and the coordinates for the given unit
.
Since the names (or codes) provided in the data
given in input must be checked with the unit
names (or codes) available in the package (as provided by USA Census of Bureau), the check.unit.names
provides a preliminary check.
subset
provide an expression to subset the data
, using a formula with the logical operators. For example, sub-samples of the data can be selected as follows: ~I("Variable 1" == "condition 1" & "Variable 2" != "condition 2")
or for example, ~I("Variable 1" > "condition 1" | "Variable 2" != "condition 2")
.
Examples
data("popUS")
us <- US(data = popUS,colID = "id", unit = "state")
str(us, 1)
#> Classes ‘sf’, ‘US’, ‘US’ and 'data.frame': 51 obs. of 7 variables:
#> $ state_id : chr "MD" "IA" "DE" "OH" ...
#> $ id : chr "maryland" "iowa" "delaware" "ohio" ...
#> $ state_number: chr "24" "19" "10" "39" ...
#> $ region : chr "South" "Midwest" "South" "Midwest" ...
#> $ division : chr "South Atlantic" "West North Central" "South Atlantic" "East North Central" ...
#> $ population : int 6042718 3156145 967171 11689442 12807060 1929268 7535591 4887871 3013825 2095428 ...
#> $ geometry :sfc_MULTIPOLYGON of length 51; first list element: List of 2
#> ..- attr(*, "class")= chr [1:3] "XY" "MULTIPOLYGON" "sfg"
#> - attr(*, "sf_column")= chr "geometry"
#> - attr(*, "agr")= Factor w/ 3 levels "constant","aggregate",..: NA NA NA NA NA NA
#> ..- attr(*, "names")= chr [1:6] "state_id" "id" "state_number" "region" ...
#> - attr(*, "unit")= chr "state"
#> - attr(*, "year")= chr "2018"
#> - attr(*, "colID")= chr "id"
us1 <- US(data = popUS, colID = "id", unit = "state", add = ~I(population/100))
str(us1, 1)
#> Classes ‘sf’, ‘US’, ‘US’ and 'data.frame': 51 obs. of 8 variables:
#> $ state_id : chr "MD" "IA" "DE" "OH" ...
#> $ id : chr "maryland" "iowa" "delaware" "ohio" ...
#> $ state_number : chr "24" "19" "10" "39" ...
#> $ region : chr "South" "Midwest" "South" "Midwest" ...
#> $ division : chr "South Atlantic" "West North Central" "South Atlantic" "East North Central" ...
#> $ population : int 6042718 3156145 967171 11689442 12807060 1929268 7535591 4887871 3013825 2095428 ...
#> $ I.population.100.: 'AsIs' num 60427.18 31561.45 9671.71 116894.42 128070.6 ...
#> $ geometry :sfc_MULTIPOLYGON of length 51; first list element: List of 2
#> ..- attr(*, "class")= chr [1:3] "XY" "MULTIPOLYGON" "sfg"
#> - attr(*, "sf_column")= chr "geometry"
#> - attr(*, "agr")= Factor w/ 3 levels "constant","aggregate",..: NA NA NA NA NA NA NA
#> ..- attr(*, "names")= chr [1:7] "state_id" "id" "state_number" "region" ...
#> - attr(*, "unit")= chr "state"
#> - attr(*, "year")= chr "2018"
#> - attr(*, "colID")= chr "id"
us2 <- US(data = popUS, colID = "id", unit = "state",
add = ~I(population/100), new_var_names = c("population/100"))
str(us2, 1)
#> Classes ‘sf’, ‘US’, ‘US’ and 'data.frame': 51 obs. of 8 variables:
#> $ state_id : chr "MD" "IA" "DE" "OH" ...
#> $ id : chr "maryland" "iowa" "delaware" "ohio" ...
#> $ state_number : chr "24" "19" "10" "39" ...
#> $ region : chr "South" "Midwest" "South" "Midwest" ...
#> $ division : chr "South Atlantic" "West North Central" "South Atlantic" "East North Central" ...
#> $ population : int 6042718 3156145 967171 11689442 12807060 1929268 7535591 4887871 3013825 2095428 ...
#> $ population.100: 'AsIs' num 60427.18 31561.45 9671.71 116894.42 128070.6 ...
#> $ geometry :sfc_MULTIPOLYGON of length 51; first list element: List of 2
#> ..- attr(*, "class")= chr [1:3] "XY" "MULTIPOLYGON" "sfg"
#> - attr(*, "sf_column")= chr "geometry"
#> - attr(*, "agr")= Factor w/ 3 levels "constant","aggregate",..: NA NA NA NA NA NA NA
#> ..- attr(*, "names")= chr [1:7] "state_id" "id" "state_number" "region" ...
#> - attr(*, "unit")= chr "state"
#> - attr(*, "year")= chr "2018"
#> - attr(*, "colID")= chr "id"