2

My data is as follows, I call it shortie in the code:

   State  Min  Max   Average
1     AL  7.0 10.8  8.842857
2     AK  6.9 10.2  8.095238
3     AZ  5.4 10.0  6.623810
4     AR  9.5 15.4 12.157143
5     CA  5.8  7.9  6.309524
6     CO  6.5  9.8  7.557143
7     CT  5.0  7.9  5.700000
8     DE  5.2  8.4  6.090476
9     FL  7.0 10.9  8.423810
10    GA  6.0 10.3  7.094737
11    HI 15.3 22.6 18.652381
12    ID  7.8 13.9  9.957143
13    IL  5.4  8.8  6.300000
14    IN  6.3  9.6  7.423810
15    IA  6.1  9.0  6.971429
16    KS  5.9  9.2  6.880952
17    KY  6.9 13.5  8.609524
18    LA  5.6  9.6  7.500000
19    ME  7.1  9.7  8.004762
20    MD  5.6  9.7  6.757143
21    MA  5.5  7.9  5.961905
22    MI  5.4  8.2  6.171429
23    MN  5.3  7.7  6.066667
24    MS  4.8  9.4  6.376190
25    MO  6.2  9.6  7.114286
26    MT  7.1  8.6  7.576190
27    NE  6.3  8.0  6.942857
28    NV 28.4 99.0 52.614286
29    NH  6.5  9.5  7.576190
30    NJ  4.8  7.6  5.676190
31    NM  4.0  8.8  7.042857
32    NY  6.5  8.6  7.095238
33    NC  6.5  8.5  7.233333
34    ND  5.8  7.5  6.623810
35    OH  5.7  9.0  6.547619
36    OK  6.5 10.6  7.323529
37    OR  6.3  8.9  7.176190
38    PA  5.3  7.1  5.771429
39    RI  5.8  8.1  6.885714
40    SC  6.6 15.9  8.638095
41    SD  6.7 11.1  8.128571
42    TN  8.2 15.5 10.838095
43    TX  6.9 10.5  7.928571
44    UT  7.3 11.2  9.290476
45    VT  7.9 10.9  9.071429
46    VA  6.7 11.4  7.900000
47    WA  6.0  9.5  6.695238
48    WV  6.1  8.7  7.133333
49    WI  5.2  7.9  5.995238
50    WY  7.1 10.7  8.723810

I've tried a multitude of ways to plot this data over the US Map, I want individual plots, with just max, min, or average numbers.

plot_usmap(regions="states", values="minimum")
plot_usmap(shortie, regions="states", values="minimum")
plot_usmap(shortie, regions="states", values="Min")

I either get blank maps or just blank screens with this. I tried defining "minimum" as shortie[2:2] because sometimes I would get the error that Min was not found as a column in the data.

This code runs but as you can see it is very long, and for some reason it doesn't put a value on Florida, and doesn't run if I includes Alaska and Hawaii. The code below is without Alaska and Hawaii. Also I wanted to know what other alternative ways there were to representing the number's min/max/avg visually on the states, beyond just gradients, possibly through ggplot.

map.text("state", regions=c("alabama",
                            "arizona",
                            "arkansas",
                            "california",
                            "colorado",
                            "connecticut",
                            "delaware",
                            "district of columbia",
                            "florida",
                            "georgia",
                            "idaho",
                            "illinois",
                            "indiana",
                            "iowa",
                            "kansas",
                            "kentucky",
                            "louisiana",
                            "maine",
                            "maryland",
                            "massachusetts:main",
                            "michigan:north",
                            "minnesota",
                            "mississippi",
                            "missouri",
                            "montana",
                            "nebraska",
                            "nevada",
                            "new hampshire",
                            "new jersey",
                            "new mexico",
                            "new york:main",
                            "north carolina:main",
                            "north dakota",
                            "ohio",
                            "oklahoma",
                            "oregon",
                            "pennsylvania",
                            "rhode island",
                            "south carolina",
                            "south dakota",
                            "tennessee",
                            "texas",
                            "utah",
                            "vermont",
                            "virginia:main",
                            "washington:main",
                            "west virginia",
                            "wisconsin",
                            "wyoming"), labels=as.character(c(7,
5.4,
9.5,
5.8,
6.5,
5,
5.2,
4,
7,
6,
7.8,
5.4,
6.3,
6.1,
5.9,
6.9,
5.6,
7.1,
5.6,
5.5,
5.4,
5.3,
4.8,
6.2,
7.1,
6.3,
28.4,
6.5,
4.8,
4,
6.5,
6.5,
5.8,
5.7,
6.5,
6.3,
5.3,
5.8,
6.6,
6.7,
8.2,
6.9,
7.3,
7.9,
6.7,
6,
6.1,
5.2,
7.1)))
dc37
  • 15,840
  • 4
  • 15
  • 32
subrinarafiq
  • 59
  • 1
  • 8

2 Answers2

2

If you wish to use plot_usmap, you need to make sure you provide 2 columns of data to the function:

A data frame containing values to plot on the map. This parameter should be a data frame consisting of two columns, a fips code (2 characters for state, 5 characters for county) and the value that should be associated with that region. The columns of data must be fips or state and the value of the 'values' parameter.

So if you want to plot minimum you can pass on shortie[,2:3]:

   state  Min
1     AL  7.0
2     AK  6.9
3     AZ  5.4
4     AR  9.5
5     CA  5.8
...

Note that the state column name should be lower case to match what plot_usmap expects.

Then the following should work (set values = "Min"):

library(usmap)
library(ggplot2)

plot_usmap(data = shortie[,2:3], 
           regions="states", 
           values="Min") +
  theme(legend.position = "right")

map with plot_usmap

If you want to plot Max, use shortie[,c(2,4)] and values = "Max". If you want to plot Average, use shortie[,c(2,5)] and values = "Average".

Ben
  • 28,684
  • 5
  • 23
  • 45
  • Error in map_with_data(data, values = values, include = include, exclude = exclude) : `data` must be a data.frame containing either a `state` or `fips` column. I keep receiving this error message, and I replaced "State" to "state" in my CSV file and shortie is definitely a data frame. – subrinarafiq Feb 05 '20 at 03:19
  • Oh I realized my mistake shortie had to be defined as shortie[,1:2] and not 2:3! Thank you for the help. – subrinarafiq Feb 05 '20 at 03:24
  • If I wanted to just project the numbers above each state, I would have to use another plotting command and not plot_usmap()? – subrinarafiq Feb 05 '20 at 03:38
  • The only built-in label would have state abbreviation (if `labels = TRUE` set in `plot_usmap`) --- however, you could use other `ggplot` functions to annotate/add text - would also check out these [vignettes](https://cran.r-project.org/web/packages/usmap/vignettes/advanced-mapping.html) - if you use `usmap_transform` you can use other longitude/latitude data with your map. in the last vignette, you can see how `geom_label_repel` can be used to add text to label cities as an example – Ben Feb 05 '20 at 03:47
  • I should mention that there are other examples available using other packages include statistics/values in state plots with `ggplot` that could be adapted: https://stackoverflow.com/questions/46859779/how-to-map-all-the-states-of-us-using-r-with-the-number-of-crimes-occurred-in-ea or https://stackoverflow.com/questions/48832201/plot-a-numerical-values-in-united-states-map-based-on-abbreviated-state-names – Ben Feb 05 '20 at 03:53
  • Thank you for the last link! Also, I tried to add a title to the graph by "main="Title"" within the "plot_usmap()" and it is not working, any advice? – subrinarafiq Feb 05 '20 at 04:13
  • Add `+ labs(title = "my title goes here")` ... this goes after `plot_usmap` ... don’t forget plus sign (+)... – Ben Feb 06 '20 at 03:18
  • Should this comment work to change the color scale? I think it is hard to see the variation of rates with the current one + scale_colour_gradient(low = "red",high = "white") – subrinarafiq Feb 08 '20 at 19:44
  • How about this: `+ scale_fill_continuous(low = "red", high = "white")` – Ben Feb 08 '20 at 20:03
0

Based on your dataframe, you can do the following:

1) Using the package maps, you can have a map with al coordinates of US states as following. On this package, you can add a column for abreviation of states names.

library(maps)
states <- map_data("state")
DF <- states 
DF$Abrev. <- unlist(sapply(DF$region, function(x) match(toupper(x), toupper(state.name))))
DF$Abrev. <- state.abb[DF$Abrev.]

2) you can merge your values with this dataframe "DF" using abbreviated states names (here using left_join from dplyr package) and reshape your dataframe into a longer format to create a categorical values for each variable (Max, Min, Average) (here using pivot_longer from tidyr package):

library(dplyr)
library(tidyr)
DF <- left_join(DF, df, by = c("Abrev." = "State"))
DF <- DF %>% pivot_longer(., cols = c(Min, Max, Average), names_to = "variable", values_to = "value")

# A tibble: 6 x 9
   long   lat group order region  subregion Abrev. variable value
  <dbl> <dbl> <dbl> <int> <chr>   <chr>     <chr>  <chr>    <dbl>
1 -87.5  30.4     1     1 alabama NA        AL     Min       7   
2 -87.5  30.4     1     1 alabama NA        AL     Max      10.8 
3 -87.5  30.4     1     1 alabama NA        AL     Average   8.84
4 -87.5  30.4     1     2 alabama NA        AL     Min       7   
5 -87.5  30.4     1     2 alabama NA        AL     Max      10.8 
6 -87.5  30.4     1     2 alabama NA        AL     Average   8.84
...

3) Finally, you can plot it using geom_map from ggplot2 and used facet_wrap to create one US map for each variable Max, Min, Average by doing:

library(ggplot2)
ggplot(DF, aes(map_id = region))+
  geom_map(aes(fill = value), map = states)+
  expand_limits(x = states$long, y = states$lat)+
  facet_wrap(.~variable)+

enter image description here

Does it answer your question ?

dc37
  • 15,840
  • 4
  • 15
  • 32
  • I am getting the following error, when I run the second line of your code, Error in map_data("state") : could not find function "map_data" – subrinarafiq Feb 05 '20 at 03:45
  • Also your outputted graphs don't include Alaska or Hawaii, how do I get those states included? – subrinarafiq Feb 05 '20 at 03:54