@@ -7,10 +7,8 @@ plot_choro = function(x, time_value = NULL, include = c(), range,
77 dir_col = c(" #6F9CC6" , " #E4E4E4" , " #C56B59" ),
88 title = NULL , params = list ()) {
99 # Check that we're looking at either counties or states
10- if (! (attributes(x )$ geo_type == " county" ||
11- attributes(x )$ geo_type == " state" ||
12- attributes(x )$ geo_type == " hrr" ||
13- attributes(x )$ geo_type == " msa" )) {
10+ if (! (attributes(x )$ metadata $ geo_type %in%
11+ c(" county" , " state" , " hrr" , " msa" ))) {
1412 stop(" Only 'county', 'state', 'hrr' and 'msa' are supported
1513 for choropleth maps." )
1614 }
@@ -108,9 +106,12 @@ plot_choro = function(x, time_value = NULL, include = c(), range,
108106 names(val ) = geo
109107
110108 # Make background layer for MSA and HRR maps which are incomplete
111- if ((attributes(x )$ geo_type == " msa" ) |
112- (attributes(x )$ geo_type == " hrr" )) {
113- map_df = sf :: st_read(' ../data/shapefiles/state/cb_2019_us_state_5m.shp' )
109+ if ((attributes(x )$ metadata $ geo_type == " msa" ) ||
110+ (attributes(x )$ metadata $ geo_type == " hrr" )) {
111+ map_df = sf :: st_read(system.file(
112+ " shapefiles/state" ,
113+ " cb_2019_us_state_5m.shp" ,
114+ package = " covidcast" ))
114115 background_crs = sf :: st_crs(map_df )
115116 map_df $ STATEFP <- as.character(map_df $ STATEFP )
116117 map_df $ is_alaska = map_df $ STATEFP == ' 02'
@@ -138,15 +139,12 @@ plot_choro = function(x, time_value = NULL, include = c(), range,
138139 hawaii_df = sf :: st_set_crs(hawaii_df , 102003 )
139140
140141 alaska_centroid = sf :: st_centroid(sf :: st_geometry(alaska_df ))
141- print(' hi' )
142142 alaska_scale = (sf :: st_geometry(alaska_df ) - alaska_centroid ) * 0.35 + alaska_centroid
143143 alaska_df = sf :: st_set_geometry(alaska_df , alaska_scale )
144144 alaska_shift = sf :: st_geometry(alaska_df ) + c(- 2e+6 , - 2.6e+6 )
145145 alaska_df = sf :: st_set_geometry(alaska_df , alaska_shift )
146146 alaska_df = sf :: st_set_crs(alaska_df , 102003 )
147147
148- print(' hi' )
149-
150148 pr_shift = sf :: st_geometry(pr_df ) + c(- 0.9e+6 , 1e+6 )
151149 pr_df = sf :: st_set_geometry(pr_df , pr_shift )
152150 r = - 16 * pi / 180
@@ -179,10 +177,10 @@ plot_choro = function(x, time_value = NULL, include = c(), range,
179177 geom_args $ fill = alaska_col
180178 geom_args $ data = alaska_df
181179 back_alaska_layer = do.call(ggplot2 :: geom_sf , geom_args )
182- }
180+ }
183181
184182 # Create the choropleth colors for counties
185- if (attributes(x )$ geo_type == " county" ) {
183+ if (attributes(x )$ metadata $ geo_type == " county" ) {
186184 map_df = usmap :: us_map(" county" , include = include )
187185 map_geo = map_df $ fips
188186 map_col = rep(missing_col , length(map_geo ))
@@ -205,7 +203,7 @@ plot_choro = function(x, time_value = NULL, include = c(), range,
205203 }
206204
207205 # Create the choropleth colors for states
208- else if (attributes(x )$ geo_type == " state" ) {
206+ else if (attributes(x )$ metadata $ geo_type == " state" ) {
209207 map_df = usmap :: us_map(" state" , include = include )
210208 map_geo = tolower(map_df $ abbr )
211209 map_col = rep(missing_col , length(map_geo ))
@@ -215,8 +213,11 @@ plot_choro = function(x, time_value = NULL, include = c(), range,
215213 map_col [map_geo %in% geo ] = col_fun(val [map_obs ])
216214 }
217215
218- else if (attributes(x )$ geo_type == " msa" ) {
219- map_df = sf :: st_read(' ../data/shapefiles/msa/cb_2019_us_cbsa_5m.shp' )
216+ else if (attributes(x )$ metadata $ geo_type == " msa" ) {
217+ map_df = sf :: st_read(system.file(
218+ " shapefiles/msa" ,
219+ " cb_2019_us_cbsa_5m.shp" ,
220+ package = " covidcast" ))
220221 map_df = map_df %> % filter(map_df $ LSAD == ' M1' ) # only get metro and not micropolitan areas
221222 if (length(include ) > 0 ) {
222223 map_df = map_df %> % filter(map_df $ GEOID %in% include )
@@ -267,8 +268,11 @@ plot_choro = function(x, time_value = NULL, include = c(), range,
267268 pr_geo = pr_df $ color
268269 }
269270
270- else if (attributes(x )$ geo_type == " hrr" ) {
271- map_df = sf :: st_read(' ../data/shapefiles/hrr/geo_export_ad86cff5-e5ed-432e-9ec2-2ce8732099ee.shp' )
271+ else if (attributes(x )$ metadata $ geo_type == " hrr" ) {
272+ map_df = sf :: st_read(system.file(
273+ " shapefiles/hrr" ,
274+ " geo_export_ad86cff5-e5ed-432e-9ec2-2ce8732099ee.shp" ,
275+ package = " covidcast" ))
272276 if (length(include ) > 0 ) {
273277 map_df = map_df %> % filter(map_df $ hrr_num %in% include )
274278 }
@@ -324,8 +328,8 @@ plot_choro = function(x, time_value = NULL, include = c(), range,
324328 }
325329
326330 # Create the polygon layer
327- if (attributes(x )$ geo_type == " county" ||
328- attributes(x )$ geo_type == " state" ) {
331+ if (attributes(x )$ metadata $ geo_type == " county" ||
332+ attributes(x )$ metadata $ geo_type == " state" ) {
329333 aes = ggplot2 :: aes
330334 geom_args = list ()
331335 geom_args $ color = border_col
@@ -336,8 +340,8 @@ plot_choro = function(x, time_value = NULL, include = c(), range,
336340 polygon_layer = do.call(ggplot2 :: geom_polygon , geom_args )
337341 coord_layer = ggplot2 :: coord_equal()
338342 }
339- else if (attributes(x )$ geo_type == " msa" ||
340- attributes(x )$ geo_type == " hrr" ) {
343+ else if (attributes(x )$ metadata $ geo_type == " msa" ||
344+ attributes(x )$ metadata $ geo_type == " hrr" ) {
341345 aes = ggplot2 :: aes
342346 geom_args = list ()
343347 geom_args $ color = border_col
@@ -430,8 +434,8 @@ plot_choro = function(x, time_value = NULL, include = c(), range,
430434 guide = guide )
431435 }
432436 # Put it all together and return
433- if ((attributes(x )$ geo_type == " msa" ) |
434- (attributes(x )$ geo_type == " hrr" )) {
437+ if ((attributes(x )$ metadata $ geo_type == " msa" ) |
438+ (attributes(x )$ metadata $ geo_type == " hrr" )) {
435439 return (ggplot2 :: ggplot() +
436440 back_main_layer + back_pr_layer + back_hawaii_layer + back_alaska_layer +
437441 main_layer + pr_layer + alaska_layer + hawaii_layer + coord_layer +
@@ -449,8 +453,8 @@ plot_bubble = function(x, time_value = NULL, include = c(), range = NULL,
449453 col = " purple" , alpha = 0.5 , num_bins = 8 ,
450454 title = NULL , params = list ()) {
451455 # Check that we're looking at either counties or states
452- if (! (attributes(x )$ geo_type == " county" ||
453- attributes(x )$ geo_type == " state" )) {
456+ if (! (attributes(x )$ metadata $ geo_type == " county" ||
457+ attributes(x )$ metadata $ geo_type == " state" )) {
454458 stop(" Only 'county' and 'state' are supported for bubble maps." )
455459 }
456460
@@ -498,9 +502,9 @@ plot_bubble = function(x, time_value = NULL, include = c(), range = NULL,
498502 # Max and min bubble sizes
499503 min_size = params $ min_size
500504 max_size = params $ max_size
501- if (is.null(min_size )) min_size = ifelse(attributes(x )$ geo_type == " county" ,
505+ if (is.null(min_size )) min_size = ifelse(attributes(x )$ metadata $ geo_type == " county" ,
502506 0.1 , 1 )
503- if (is.null(max_size )) max_size = ifelse(attributes(x )$ geo_type == " county" ,
507+ if (is.null(max_size )) max_size = ifelse(attributes(x )$ metadata $ geo_type == " county" ,
504508 4 , 12 )
505509
506510 # Bubble sizes. Important note the way we set sizes later, via
@@ -535,13 +539,13 @@ plot_bubble = function(x, time_value = NULL, include = c(), range = NULL,
535539 names(val ) = geo
536540
537541 # Grap the map data frame for counties
538- if (attributes(x )$ geo_type == " county" ) {
542+ if (attributes(x )$ metadata $ geo_type == " county" ) {
539543 map_df = usmap :: us_map(" county" , include = include )
540544 map_geo = map_df $ fips
541545 }
542546
543547 # Grap the map data frame for states
544- else if (attributes(x )$ geo_type == " state" ) {
548+ else if (attributes(x )$ metadata $ geo_type == " state" ) {
545549 map_df = usmap :: us_map(" state" , include = include )
546550 map_geo = tolower(map_df $ abbr )
547551 }
@@ -568,12 +572,12 @@ plot_bubble = function(x, time_value = NULL, include = c(), range = NULL,
568572
569573 # Retrieve coordinates for mapping
570574 # Reading from usmap files to ensure consistency with borders
571- if (attributes(x )$ geo_type == " county" ) {
575+ if (attributes(x )$ metadata $ geo_type == " county" ) {
572576 centroids = county_geo [county_geo $ fips %in% map_geo , ]
573577 cur_geo = centroids $ fips
574578 cur_val = rep(NA , length(cur_geo ))
575579 }
576- else if (attributes(x )$ geo_type == " state" ) {
580+ else if (attributes(x )$ metadata $ geo_type == " state" ) {
577581 centroids = state_geo
578582 centroids $ abbr = tolower(centroids $ abbr )
579583 centroids = centroids [centroids $ abbr %in% map_geo , ]
0 commit comments