1

I have the following data and code that I need to use to create 2 accurate choropleth maps of the US based on intervals set in the code. Currently, this is filling in the map of the states, but according to the hex codes each state should be assigned, it's showing up differently in the scale on the 10 class "RdYlBu" palette. For example, Arizona should be #FDAE61 (according to the dataset), but it is showing up as #fee090 on the map. I also don't know how to include Alaska and Hawaii in the map, but it is part of the data set I'm trying to map.

This is the code I have currently:

# Split the dataframe by year
yearly_data <- split(avg_inpatient_beds, f = avg_inpatient_beds$year)

# Create individual year dataframes
avg_2020 <- (yearly_data$"2020")
avg_2021 <- (yearly_data$"2021")
avg_2022 <- (yearly_data$"2022")

# Retain only percentage and state columns and rename column for year
avg_2021 <- as.data.frame(avg_2021[, c(1, 5)])
names(avg_2021)[2] <- "2021"
avg_2022 <- as.data.frame(avg_2022[, c(1, 5)])
names(avg_2022)[2] <- "2022"

# Merge two years to one data frame
avg_beds <- merge(avg_2021, avg_2022, by = "state")
# Remove states that are not in the typical state map
avg_beds <- subset(avg_beds, !(row.names(avg_beds) %in% c(4, 9, 41, 49)))
avg_beds

# Update state column to state names to match map data
full_state <- state.name[match(avg_beds$state, state.abb)]

# Replace state abb column with full name column
avg_beds <- cbind(full_state, avg_beds)
avg_beds <- avg_beds[, c(1, 3, 4)]
names(avg_beds)[1] <- "state"

# Get ranges for figuring intervals
range(avg_beds$"2021")
range(avg_beds$"2022")

# Create breaks to split data into intervals
breaks <- c(40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90)

# Define colors
beds_colors <- brewer.pal(10, "RdYlBu")
beds_colors <- beds_colors[10:1]

# Assign colors
twenty_one_cuts <- cut(avg_beds$"2021", breaks)
twenty_two_cuts <- cut(avg_beds$"2022", breaks)
twenty_one_colors <- beds_colors[twenty_one_cuts]
twenty_two_colors <- beds_colors[twenty_two_cuts]
twenty_one <- cbind(avg_beds[, c(1, 2)], twenty_one_colors)
twenty_two <- cbind(avg_beds[, c(1, 3)], twenty_two_colors)

# Order dataframes alphabetically to allow it to match map
twenty_one <- twenty_one[order(twenty_one$state), ]
twenty_one$state <- tolower(twenty_one$state)
twenty_two <- twenty_two[order(twenty_two$state), ]
twenty_two$state <- tolower(twenty_two$state)

colored_map <- map("state", fill = TRUE, col = twenty_one$twenty_one_colors)

I am getting this currently, but the colors are incorrect when compared with the dataset. enter image description here

This is the dataset:

> dput(avg_inpatient_beds)
structure(list(state = c("AK", "AK", "AK", "AK", "AL", "AL", 
"AL", "AL", "AR", "AR", "AR", "AR", "AS", "AS", "AS", "AZ", "AZ", 
"AZ", "AZ", "CA", "CA", "CA", "CA", "CO", "CO", "CO", "CO", "CT", 
"CT", "CT", "CT", "DC", "DC", "DC", "DC", "DE", "DE", "DE", "DE", 
"FL", "FL", "FL", "FL", "GA", "GA", "GA", "GA", "HI", "HI", "HI", 
"HI", "IA", "IA", "IA", "IA", "ID", "ID", "ID", "ID", "IL", "IL", 
"IL", "IL", "IN", "IN", "IN", "IN", "KS", "KS", "KS", "KS", "KY", 
"KY", "KY", "KY", "LA", "LA", "LA", "LA", "MA", "MA", "MA", "MA", 
"MD", "MD", "MD", "MD", "ME", "ME", "ME", "ME", "MI", "MI", "MI", 
"MI", "MN", "MN", "MN", "MN", "MO", "MO", "MO", "MO", "MS", "MS", 
"MS", "MS", "MT", "MT", "MT", "MT", "NC", "NC", "NC", "NC", "ND", 
"ND", "ND", "ND", "NE", "NE", "NE", "NE", "NH", "NH", "NH", "NH", 
"NJ", "NJ", "NJ", "NJ", "NM", "NM", "NM", "NM", "NV", "NV", "NV", 
"NV", "NY", "NY", "NY", "NY", "OH", "OH", "OH", "OH", "OK", "OK", 
"OK", "OK", "OR", "OR", "OR", "OR", "PA", "PA", "PA", "PA", "PR", 
"PR", "PR", "PR", "RI", "RI", "RI", "RI", "SC", "SC", "SC", "SC", 
"SD", "SD", "SD", "SD", "TN", "TN", "TN", "TN", "TX", "TX", "TX", 
"TX", "UT", "UT", "UT", "UT", "VA", "VA", "VA", "VA", "VI", "VI", 
"VI", "VI", "VT", "VT", "VT", "VT", "WA", "WA", "WA", "WA", "WI", 
"WI", "WI", "WI", "WV", "WV", "WV", "WV", "WY", "WY", "WY", "WY"
), year = c("2020", "2021", "2022", "2023", "2020", "2021", "2022", 
"2023", "2020", "2021", "2022", "2023", "2021", "2022", "2023", 
"2020", "2021", "2022", "2023", "2020", "2021", "2022", "2023", 
"2020", "2021", "2022", "2023", "2020", "2021", "2022", "2023", 
"2020", "2021", "2022", "2023", "2020", "2021", "2022", "2023", 
"2020", "2021", "2022", "2023", "2020", "2021", "2022", "2023", 
"2020", "2021", "2022", "2023", "2020", "2021", "2022", "2023", 
"2020", "2021", "2022", "2023", "2020", "2021", "2022", "2023", 
"2020", "2021", "2022", "2023", "2020", "2021", "2022", "2023", 
"2020", "2021", "2022", "2023", "2020", "2021", "2022", "2023", 
"2020", "2021", "2022", "2023", "2020", "2021", "2022", "2023", 
"2020", "2021", "2022", "2023", "2020", "2021", "2022", "2023", 
"2020", "2021", "2022", "2023", "2020", "2021", "2022", "2023", 
"2020", "2021", "2022", "2023", "2020", "2021", "2022", "2023", 
"2020", "2021", "2022", "2023", "2020", "2021", "2022", "2023", 
"2020", "2021", "2022", "2023", "2020", "2021", "2022", "2023", 
"2020", "2021", "2022", "2023", "2020", "2021", "2022", "2023", 
"2020", "2021", "2022", "2023", "2020", "2021", "2022", "2023", 
"2020", "2021", "2022", "2023", "2020", "2021", "2022", "2023", 
"2020", "2021", "2022", "2023", "2020", "2021", "2022", "2023", 
"2020", "2021", "2022", "2023", "2020", "2021", "2022", "2023", 
"2020", "2021", "2022", "2023", "2020", "2021", "2022", "2023", 
"2020", "2021", "2022", "2023", "2020", "2021", "2022", "2023", 
"2020", "2021", "2022", "2023", "2020", "2021", "2022", "2023", 
"2020", "2021", "2022", "2023", "2020", "2021", "2022", "2023", 
"2020", "2021", "2022", "2023", "2020", "2021", "2022", "2023", 
"2020", "2021", "2022", "2023", "2020", "2021", "2022", "2023"
), avg_beds = c(1482.3485915493, 1579.34794520548, 1722.99726027397, 
1611.08235294118, 10263.0081967213, 15258.8849315068, 14412.6712328767, 
12341.9176470588, 7620.39202657807, 8970.35890410959, 8286.20273972603, 
7564.30588235294, 150, 150, 150, 10922.8459016393, 16757.9150684932, 
15837.5452054795, 14664.8117647059, 54697.1607717042, 63854.0520547945, 
63370.4602739726, 62733.7411764706, 8349.11945392492, 11614.4575342466, 
11095.8246575342, 10311.3058823529, 7811.21799307958, 8253.53150684931, 
7801.68493150685, 7520.22352941176, 3078.63440860215, 3499.66301369863, 
3369.04931506849, 3248.97647058824, 2720.75, 3236.13424657534, 
2904.96712328767, 2442.03529411765, 53886.7796610169, 57841.8657534247, 
57473.0410958904, 56006.7647058823, 16365.931372549, 22393.0630136986, 
21705.4109589041, 21049.6, 1620.81147540984, 2591.40273972603, 
2555.15068493151, 2523.18823529412, 7090.79738562091, 7865.93150684932, 
7215.30410958904, 6983.58823529412, 3524.44897959184, 3591.14794520548, 
3039.05753424658, 2908.62352941176, 24285, 30953.3534246575, 
29198.5205479452, 28424.6352941176, 11896.4371584699, 18502.7671232877, 
16081.8712328767, 14564.6352941176, 6259.70447761194, 8649.8602739726, 
8453.32602739726, 7940.51764705882, 11450.9869281046, 13096.9260273973, 
11895.8219178082, 12042.8, NA, 13463.8164383562, 12650.2493150685, 
12495.9058823529, 16865.7857142857, 18369.9835616438, 17447.1808219178, 
15897.7058823529, 8396.2091503268, 11356.002739726, 11094.7753424658, 
10408.2705882353, 2748.74836601307, 3250.99726027397, 3042.84383561644, 
2847.78823529412, 19097.3888888889, 22814.0520547945, 21593.3068493151, 
20813.5882352941, 8464.00819672131, 9984.75342465753, 9349.83287671233, 
9347.81176470588, 16171.2268370607, 16679.5643835616, 15829.2794520548, 
14754.9647058824, 6234.09463722398, 8559.61369863014, 8566.53150684931, 
7959.34117647059, 2061.41256830601, 3053.95890410959, 2786.01095890411, 
2573.76470588235, 16280.7404371585, 23422.2054794521, 22315.9424657534, 
20619.4941176471, 2118.23202614379, 2268.7698630137, 2148.16164383562, 
1938.69411764706, 4507.30718954248, 4347.65205479452, 4500.17534246575, 
4699.50588235294, 2875.26785714286, 3029.40273972603, 2756.00547945205, 
2427.25882352941, 14989.2418300654, 23662.2493150685, 23451.0684931507, 
23140.9647058824, 2818.71672354949, 4313.10410958904, 4073.8, 
3784.44705882353, 5789.30459770115, 8680.15616438356, 8016.82739726027, 
7301.35294117647, 38705.7508532423, 50604.397260274, 46850.701369863, 
45106.5764705882, 23244.1176470588, 31288.3424657534, 27543.5561643836, 
26225.0235294118, 9241.88888888889, 10415.0575342466, 9769.58082191781, 
9260.01176470588, 4907.88782051282, 7027.76712328767, 6829.62739726027, 
6773.44705882353, 22642.7058823529, 31180.8657534247, 31712.3232876712, 
31315.4588235294, 6714.30546623794, 9353.34794520548, 8594.23561643836, 
6564.63529411765, 1875.22368421053, 2436.26575342466, 2194.10684931507, 
2214.61176470588, 9285.72786885246, 10837.9698630137, 10910.8821917808, 
9961.62352941177, 2100.54609929078, 2759.58904109589, 2305.18630136986, 
2077.10588235294, 17183.6313993174, 20020.0246575342, 19495.0794520548, 
18164.3764705882, 45096.1639344262, 67333.6602739726, 61701.1589041096, 
57172.7176470588, 5026.24738675958, 6042.06849315068, 5884.36438356164, 
5822.6, 13327.8196721311, 18029.2657534247, 17448.8520547945, 
16969.0941176471, 143.731343283582, 168.112328767123, 170.112328767123, 
208.847058823529, 1025.37931034483, 1271.89589041096, 1241.14246575342, 
1178.96470588235, 10205.9772727273, 13070.1095890411, 11642.2630136986, 
10954.0470588235, 10528.6699346405, 13154.5945205479, 11998.5397260274, 
11552.4823529412, 5347.03164556962, 6112.14794520548, 5390.77260273973, 
4918.96470588235, 1308.81699346405, 1683.56712328767, 1629.08767123288, 
1327.91764705882), avg_beds_used = c(859.514084507042, 1031.90410958904, 
1336.44109589041, 1271.81176470588, NA, 11420.0246575342, 10671.6493150685, 
9339.91764705882, 4833.80066445183, 6361.31232876712, 5899.79178082192, 
5409.70588235294, 69.4538461538462, 67.2301369863014, 65.1566265060241, 
8254.48196721311, 12088.1260273973, 11970.9095890411, 11742.8588235294, 
36386.2958199357, 49016.8739726027, 49290.0054794521, 50077.8588235294, 
5384.49146757679, 7960.87671232877, 7574.07397260274, 6974.57647058824, 
5247.73356401384, 6507.43835616438, 6055.90684931507, 5929.91764705882, 
2298.45161290323, 2698.0602739726, 2695.86575342466, 2686.05882352941, 
1906.80714285714, 2407.89315068493, 2346.27397260274, 2094.89411764706, 
34351.5288135593, 45450.8164383562, 44357.4739726027, 44287.9529411765, 
11806.7581699346, 17503.1287671233, 17315.3424657534, 16434.8, 
1460.71038251366, 1867.11506849315, 1972.61643835616, 1986.32941176471, 
3877.73529411765, 5074.71506849315, 4826.48493150685, 4695.37647058823, 
1715.47959183673, 2258.34520547945, 2141.17260273973, 2066.25882352941, 
NA, 21714.6904109589, 20805.8931506849, 19975.5529411765, 7218.73770491803, 
12180.8356164384, 11711.8246575342, 10382.8352941176, NA, 5154.47397260274, 
4924.76712328767, 4708.48235294118, 7118.85620915033, 8618.89863013699, 
8964.02191780822, 9267.81176470588, NA, 9377.41369863014, 8943.85479452055, 
8615.22352941177, 12337.7142857143, 15264.095890411, 14973.8, 
13675.1294117647, 6609.22222222222, 9216.59178082192, 9080.94520547945, 
8546.72941176471, 1721.41176470588, 2292.81369863014, 2263.33698630137, 
2195.10588235294, 13118.908496732, 17985.8136986301, 17276.2136986301, 
16595.0588235294, NA, 7725.37534246575, 7821.78356164384, 7765.78823529412, 
NA, 13279.1095890411, 12996.7452054795, 12252.8, 4077.80126182965, 
5538.44383561644, 5367.3698630137, 5270.2, 1271.74316939891, 
2035.58082191781, 1840.67397260274, 1673.21176470588, NA, 17873.7616438356, 
17379.5205479452, 16728.2705882353, 1337.61764705882, 1634.50684931507, 
1626.37260273973, 1392.67058823529, 2732.67320261438, 3004.75616438356, 
3266.20821917808, 3400.17647058824, 1882.575, 2233.58356164384, 
2222.84109589041, 2003.36470588235, NA, 16389.4136986301, 16091.5780821918, 
15967.8588235294, NA, 3082.44657534247, 3000.12876712329, 2742.45882352941, 
4106.17528735632, 6579.17808219178, 6228.57534246575, 5693.83529411765, 
27533.1467576792, 38432.4931506849, 36969.3150684932, 36087.2941176471, 
15721.1764705882, 22884.9315068493, 21579.4438356164, 20819.0235294118, 
5369.90522875817, 7306.44109589041, 6892.15068493151, 6729.51764705882, 
3297.5641025641, 5256.22191780822, 5386.10136986301, 5348.8, 
16351.3104575163, 25367.7205479452, 25152.4657534247, 24211.8705882353, 
3788.38585209003, 5606.55616438356, 5794.81369863014, 4504.89411764706, 
1598.75657894737, 2183.77534246575, 1941.18356164384, 1950.54117647059, 
6813.28196721311, 8441.33150684932, 8314.68219178082, 7780.95294117647, 
1195.37234042553, 1616.48219178082, 1475.36164383562, 1347.98823529412, 
NA, 13369.3698630137, 13911.4410958904, 13302.0117647059, 30255.9508196721, 
49958.2328767123, 47551.9698630137, 45613.7529411765, NA, 3654.07671232877, 
3883.23561643836, 3643.21176470588, NA, 13147.2712328767, 12598.5863013699, 
11930.6352941176, NA, 95.358904109589, 104.172602739726, 126.882352941176, 
NA, 868.32602739726, 913.249315068493, 866.705882352941, 7538.48051948052, 
9722.10410958904, 9771.67123287671, 9187.48235294118, NA, 8704.84109589041, 
8572.38082191781, 8377.32941176471, 3578.38291139241, 4651.43835616438, 
4363.65205479452, 3982.89411764706, 553.062091503268, 783.830136986301, 
727.032876712329, 594.988235294118), avg_pct_used = c(58.0341335678192, 
65.3918285451338, 77.486689789602, 78.7069859419761, NA, 74.8323725749902, 
74.0321411255795, 75.6673005341654, 60.7230210185479, 70.8459629988653, 
71.114302653934, 71.4884791485398, 46.3025641025641, 44.8200913242009, 
43.4377510040161, 76.4187396426551, 72.0622470157661, 75.5299532951362, 
80.064864649838, 63.6566790011334, 76.7010909079842, 77.7577244390066, 
79.8272641974943, 63.8469922075423, 68.5281388333889, 68.2810792760171, 
67.6394107258107, 67.145180309496, 78.8209944195422, 77.6208246032181, 
78.8539611914541, 73.6668959214497, 77.0737279466314, 80.0078235586996, 
82.6650165945598, 70.4450989480634, 74.3348186010574, 80.8703111122651, 
85.7984589728335, 61.5900367839076, 78.5692691611553, 77.1576751070881, 
79.0648222425613, 70.5613743599451, 78.148310972946, 79.7712065488659, 
78.0682633956077, NaN, 72.0543489077551, 77.2172137028582, 78.7181154144497, 
52.6737074029103, 64.6168898952453, 66.9346718717226, 67.2220044908927, 
47.2141877609525, 63.0377100873095, 70.5391653388271, 70.993450834333, 
NA, 70.1477645800593, 71.2467634826167, 70.2740078757236, 54.3301380159047, 
65.8514105025912, 72.8863091365527, 71.2854674936872, NA, 59.6045663483043, 
58.2535536447001, 59.2894209267201, 59.5841607101206, 66.5339868749684, 
75.3609285976828, 76.9394742584137, NA, 69.8108153059624, 70.6825562358614, 
68.9190642728273, 72.6039025535447, 83.0839704811626, 85.7944028777476, 
86.0226922376057, 77.3792722022137, 81.1486231839679, 81.8115259991818, 
82.1175691360236, 62.8762311540265, 70.5136920428069, 74.453161232248, 
77.0807731157178, 65.0460381738816, 78.8431039948139, 79.9936513152379, 
79.7317724832292, NA, 77.3170219033029, 83.6292752542574, 83.0602240942635, 
NA, 79.5422311805706, 82.088985443897, 83.015381442142, 61.7698860682214, 
64.6670682078301, 62.6186207779669, 66.1985866915137, 56.6876936563504, 
66.7195368457316, 65.9968194906312, 65.0161981058136, NA, 76.3154613571695, 
77.9093413466763, 81.1345886796999, 59.8677172344674, 72.1077397115384, 
75.6406873882103, 71.8103617830332, 56.9990151519118, 69.151870898337, 
72.5638645928381, 72.3531071802958, 64.6950144458858, 73.7638511183982, 
80.6893174931963, 82.4492586491483, NA, 69.2391241671435, 68.5926909559758, 
68.978171777324, NA, 71.4690590158144, 73.6567942166183, 72.4545832176507, 
61.5439639959734, 75.839423218283, 77.6720889964072, 77.9847437399422, 
71.2578092100634, 76.0387684922576, 78.9065607260507, 79.9989677871531, 
64.9955230470055, 73.1589740461094, 78.341179701438, 79.3747049853637, 
55.2038005519259, 70.1570005816478, 70.5013626193212, 72.5909485205156, 
68.2320333974356, 74.7679794346103, 78.8818625465407, 78.9732884146987, 
65.9361384618222, 81.388519390194, 79.3451781172181, 77.3487329364629, 
58.9938404930082, 59.945262227112, 67.7187242430109, 68.5841922329257, 
83.7378026402648, 89.634872807669, 88.4890957351854, 88.069196916687, 
72.4583212802935, 77.8491530227364, 76.2724879883023, 78.1044771908371, 
53.7613268371485, 58.5535864783643, 64.0525119957538, 64.8919251656738, 
NA, 66.8044646550824, 71.3742115780498, 73.2277578779517, 59.2918761516133, 
74.2471431901894, 77.0988716989269, 79.7628244596268, NA, 60.3836722252458, 
65.8969040316745, 62.5580000888279, NA, 72.9107734587356, 72.1817163421604, 
70.3024802885588, NA, 56.7388220028929, 61.2388159417545, 60.8105742296919, 
NA, 68.3657287871175, 73.575127499954, 73.5177797687125, 72.8386838356664, 
74.5968026257042, 83.9757756275687, 83.8693574396094, NA, 66.2691862765157, 
71.4280050528217, 72.5039877649086, 63.7259250722131, 76.1371272643305, 
80.9239305142574, 80.9557245485105, 43.6256840298479, 46.4657999422649, 
44.5542417729596, 44.7866269949582)), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -215L), groups = structure(list(
    state = c("AK", "AL", "AR", "AS", "AZ", "CA", "CO", "CT", 
    "DC", "DE", "FL", "GA", "HI", "IA", "ID", "IL", "IN", "KS", 
    "KY", "LA", "MA", "MD", "ME", "MI", "MN", "MO", "MS", "MT", 
    "NC", "ND", "NE", "NH", "NJ", "NM", "NV", "NY", "OH", "OK", 
    "OR", "PA", "PR", "RI", "SC", "SD", "TN", "TX", "UT", "VA", 
    "VI", "VT", "WA", "WI", "WV", "WY"), .rows = structure(list(
        1:4, 5:8, 9:12, 13:15, 16:19, 20:23, 24:27, 28:31, 32:35, 
        36:39, 40:43, 44:47, 48:51, 52:55, 56:59, 60:63, 64:67, 
        68:71, 72:75, 76:79, 80:83, 84:87, 88:91, 92:95, 96:99, 
        100:103, 104:107, 108:111, 112:115, 116:119, 120:123, 
        124:127, 128:131, 132:135, 136:139, 140:143, 144:147, 
        148:151, 152:155, 156:159, 160:163, 164:167, 168:171, 
        172:175, 176:179, 180:183, 184:187, 188:191, 192:195, 
        196:199, 200:203, 204:207, 208:211, 212:215), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), row.names = c(NA, -54L), class = c("tbl_df", 
"tbl", "data.frame"), .drop = TRUE))
data_life
  • 387
  • 1
  • 11

1 Answers1

1

You need to use the regions param to map colors to states, but this code still doesn't work as you're expecting because maps using R's built in graphics.

library(maps)
library(RColorBrewer)
<Removing your code because response won't fit, have to do this on every reprex> 
colored_map <- map("state", regions = twenty_one$state, fill = TRUE, col = twenty_one$twenty_one_colors)

Created on 2023-04-03 with reprex v2.0.2

I tested this theory by changing a few of the color hexes to base graphics colors.

twenty_one <- twenty_one |> 
  dplyr::mutate(twenty_one_colors = dplyr::if_else(twenty_one_colors == "#FDAE61", "orange1", twenty_one_colors)) |> 
  dplyr::mutate(twenty_one_colors = dplyr::if_else(twenty_one_colors == "#F46D43", "orange4", twenty_one_colors)) 

colored_map <- map("state", regions = twenty_one$state, fill = TRUE, col = twenty_one$twenty_one_colors)

Created on 2023-04-03 with reprex v2.0.2

I think you can work around this based on this post, but SDMTools isn't on CRAN anymore - you can get it on Github.

Alternatively, you can use tools that are a bit newer and more supported with sf & ggplot2.

library(sf)
library(tidyverse)
library(RColorBrewer)
library(maps)

usa <- st_as_sf(maps::map("state", fill=TRUE, plot =FALSE))
twenty_one <- dplyr::left_join(twenty_one, usa, by = c("state" = "ID"))

ggplot(twenty_one) +
  geom_sf(aes(fill = twenty_one_colors, geometry = geom)) +
  scale_fill_identity() +
  coord_sf(crs = st_crs("+proj=laea +lat_0=45 +lon_0=-100 +x_0=0 +y_0=0 +a=6370997 +b=6370997 +units=m +no_defs"), datum = NA) +
  ggthemes::theme_map()

Created on 2023-04-03 with reprex v2.0.2

Regarding your separate question of mapping Alaska & Hawaii,there are a number of ways to include them on the same map - refer to this post. I really like using something like fiftystater if I can because it's a lot simpler.

library(sf)
library(tidyverse)
library(RColorBrewer)
library(maps)
library(fiftystater)

usa <- st_as_sf(maps::map("state", fill=TRUE, plot =FALSE))
twenty_one <- dplyr::left_join(twenty_one, usa, by = c("state" = "ID"))

ggplot(twenty_one) +
  geom_map(aes(map_id = state, fill = twenty_one_colors), map = fifty_states) +
  geom_sf(aes(fill = twenty_one_colors, geometry = geom)) +
  scale_fill_identity() +
  coord_sf(crs = st_crs("+proj=laea +lat_0=45 +lon_0=-100 +x_0=0 +y_0=0 +a=6370997 +b=6370997 +units=m +no_defs"), datum = NA) +
  ggthemes::theme_map()

Created on 2023-04-03 with reprex v2.0.2

Mark Druffel
  • 629
  • 4
  • 10
  • That still isn't lining up based on the column of hex codes in the `twenty_one` data set. If it was lining up, Arizona would be hex code #FDAE61, but it is mapping as #FEE090. This is still missing AK and HI as well. Not sure how to fix that – data_life Apr 03 '23 at 20:44
  • 1
    Yea sorry I just realized that, I think it's because maps might not work with hexes. I'm tinkering and will edit my answer shortly :) – Mark Druffel Apr 03 '23 at 20:46
  • I think it has something to do with the colored_maps$name column including multiple entries for washington and some other states. I would imagine that it needs to have all of those combined to line up correctly. I have used maps to do something similar with counties, but they don't add extra polygons. – data_life Apr 03 '23 at 21:19
  • 1
    I'm not 100% sure because I haven't used maps in a really long time, but based on a few other posts I think you need to use other libs to get RColorBrewer to work with it. Hopefully my answer is helpful, but I'm sure someone else can solve your problem more directly. – Mark Druffel Apr 03 '23 at 21:35