I know usmap has an option label
in plot_usmap()
. Instead of state names, I want to label some numbers. I guess there should be data related to the coordinate of the state centroids in usmap but I am not sure how to find it. If I can get the
coordinates then I can label numbers with geom_text()
.
Here is my data.
State Abbrev Code n_votes Attitude fips
1 Alabama Ala. AL 9 Solid Republican 01
2 Alaska Alaska AK 3 Toss-up 02
3 Arizona Ariz. AZ 11 Toss-up 04
4 Arkansas Ark. AR 6 Solid Republican 05
5 California Calif. CA 55 Solid Democrat 06
6 Colorado Colo. CO 9 Leaning to Democrat 08
7 Connecticut Conn. CT 7 Solid Democrat 09
8 Delaware Del. DE 3 Solid Democrat 10
9 District of Columbia D.C. DC 3 Solid Democrat 11
10 Florida Fla. FL 29 Leaning to Democrat 12
11 Georgia Ga. GA 16 Toss-up 13
12 Hawaii Hawaii HI 4 Solid Democrat 15
13 Idaho Idaho ID 4 Solid Republican 16
14 Illinois Ill. IL 20 Solid Democrat 17
15 Indiana Ind. IN 11 Leaning to Republican 18
16 Iowa Iowa IA 6 Leaning to Republican 19
17 Kansas Kans. KS 6 Leaning to Republican 20
18 Kentucky Ky. KY 8 Solid Republican 21
19 Louisiana La. LA 8 Solid Republican 22
20 Maine Maine ME 2 Solid Democrat 23
21 Maryland Md. MD 10 Solid Democrat 24
22 Massachusetts Mass. MA 11 Solid Democrat 25
23 Michigan Mich. MI 16 Leaning to Democrat 26
24 Minnesota Minn. MN 10 Toss-up 27
25 Mississippi Miss. MS 6 Solid Republican 28
26 Missouri Mo. MO 10 Leaning to Republican 29
27 Montana Mont. MT 3 Solid Republican 30
28 Nebraska Nebr. NE 2 Solid Republican 31
29 Nevada Nev. NV 6 Leaning to Democrat 32
30 New Hampshire N.H. NH 4 Leaning to Democrat 33
31 New Jersey N.J. NJ 14 Solid Democrat 34
32 New Mexico N.M. NM 5 Solid Democrat 35
33 New York N.Y. NY 29 Solid Democrat 36
34 North Carolina N.C. NC 15 Toss-up 37
35 North Dakota N.D. ND 3 Solid Republican 38
36 Ohio Ohio OH 18 Toss-up 39
37 Oklahoma Okla. OK 7 Solid Republican 40
38 Oregon Ore. OR 7 Solid Democrat 41
39 Pennsylvania Pa. PA 20 Leaning to Democrat 42
40 Rhode Island R.I. RI 4 Solid Democrat 44
41 South Carolina S.C. SC 9 Toss-up 45
42 South Dakota S.D. SD 3 Solid Republican 46
43 Tennessee Tenn. TN 11 Solid Republican 47
44 Texas Tex. TX 38 Toss-up 48
45 Utah Utah UT 6 Leaning to Republican 49
46 Vermont Vt. VT 3 Solid Democrat 50
47 Virginia Va. VA 13 Leaning to Democrat 51
48 Washington Wash. WA 12 Solid Democrat 53
49 West Virginia W.Va. WV 5 Solid Republican 54
50 Wisconsin Wis. WI 10 Leaning to Democrat 55
51 Wyoming Wyo. WY 3 Solid Republican 56
I would like to label n_votes
, it should be something like . How am I supposed to do that?
Thanks,
Dan
Update The usmap
has been modernized in version 0.7.0
and now returns map data as simple features, i.e. geom_text
will no longer work. Instead we have to switch to geom_sf_text
:
library(usmap)
library(ggplot2)
# Get centroids
centroid_labels <- usmapdata::centroid_labels("states")
# Join data to centroids
data_labels <- merge(centroid_labels, statepop, by = "fips")
plot_usmap(data = statepop, values = "pop_2015", color = "white", labels = FALSE) +
guides(fill = "none") +
geom_sf_text(data = data_labels, ggplot2::aes(
label = scales::number(pop_2015, scale = 1e-3, accuracy = 1)
), color = "white")
Original answer
This could be achieved like so:
Get the coordinates of the state centroids which are included as data in package usmapdata
.
Join your dataset to the df with the coordinates
Use geom_text to label the map with your data
As it would take some time to read and tidy the data you provided (Next time: Type dput(NAME_OF_DATASET)
in the console and copy & paste the output starting with structure(...
into your post) I simply use the statepop
data provided by package usmap
as example data:
library(usmap)
library(ggplot2)
# Get centroids
centroid_labels <- usmapdata::centroid_labels("states")
# Join data to centroids
data_labels <- merge(centroid_labels, statepop, by = "fips")
plot_usmap(data = statepop, values = "pop_2015", color = "white", labels = FALSE) +
guides(fill = "none") +
geom_text(data = data_labels, ggplot2::aes(
x = x, y = y,
label = scales::number(pop_2015, scale = 1e-3, accuracy = 1)
), color = "white")