Search code examples
rggplot2plotgraphusmap

Plotting Numerical Values on US Maps


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)))

Solution

  • 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".