首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用usmap标记数字而不是名称?

如何使用usmap标记数字而不是名称?
EN

Stack Overflow用户
提问于 2020-08-19 13:28:12
回答 2查看 1.2K关注 0票数 2

我知道usmap在plot_usmap()中有一个选项plot_usmap()。我想给一些数字贴上标签,而不是州名。我想在usmap中应该有与状态质心坐标相关的数据,但我不知道如何找到它。如果我能得到坐标,那么我就可以用geom_text()标记数字。

这是我的数据。

代码语言:javascript
复制
 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 

我想给n_votes贴上标签,应该是

。我该怎么做?

谢谢,

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-08-19 14:37:43

这一目标可以这样实现:

usmapdata.包中作为数据包含的状态质心的坐标

  1. 使用坐标

将数据集连接到df

  1. 使用geom_text将地图标记为数据

由于需要一些时间来读取和整理您提供的数据(下一次:在控制台中键入dput(NAME_OF_DATASET)并复制和粘贴以structure(...开头的输出到您的帖子中),我只需使用usmap包提供的statepop数据作为示例数据:

代码语言:javascript
复制
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")

票数 3
EN

Stack Overflow用户

发布于 2020-08-19 15:32:29

下面是另一个完全可用的示例,它允许您通过将us转换为一个ggplot对象来使用sf。这使您可以自由选择使用ggplot获得的绘图参数。

代码语言:javascript
复制
library(usmap)
library(sf)
library(ggplot2)

d   <- us_map("states")

USS <- lapply(split(d, d$full), function(x) {
    if(length(table(x$piece)) == 1)
    {
      st_polygon(list(cbind(x$x, x$y)))
    }
    else
    {
      st_multipolygon(list(lapply(split(x, x$piece), function(y) cbind(y$x, y$y))))
    }
  })

USA  <- st_sfc(USS, crs = usmap_crs()@projargs)
USA  <- st_sf(data.frame(df, geometry = USA))
USA$centroids <- st_centroid(USA$geometry)

虽然这段代码看起来有点复杂,但它允许轻松地绘制:

代码语言:javascript
复制
ggplot(USA) + 
  geom_sf(aes(fill = Attitude)) + 
  geom_sf_text(aes(label = n_votes, geometry = centroids), colour = "white") +
  scale_fill_manual(values = c("#67b5e3",  "#ffada2","#1155b6",
                               "#ed4747", "#cccccc"), guide = guide_none()) +
  theme_void()

数据

代码语言:javascript
复制
df <-  df <- structure(list(State = structure(1:51, .Label = c("Alabama", 
"Alaska", "Arizona", "Arkansas", "California", "Colorado", "Connecticut", 
"Delaware", "District of Columbia", "Florida", "Georgia", "Hawaii", 
"Idaho", "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", 
"Louisiana", "Maine", "Maryland", "Massachusetts", "Michigan", 
"Minnesota", "Mississippi", "Missouri", "Montana", "Nebraska", 
"Nevada", "New Hampshire", "New Jersey", "New Mexico", "New York", 
"North Carolina", "North Dakota", "Ohio", "Oklahoma", "Oregon", 
"Pennsylvania", "Rhode Island", "South Carolina", "South Dakota", 
"Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington", 
"West Virginia", "Wisconsin", "Wyoming"), class = "factor"), 
    Abbrev = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 9L, 8L, 
    10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 22L, 
    21L, 23L, 24L, 25L, 26L, 27L, 34L, 35L, 30L, 31L, 32L, 33L, 
    28L, 29L, 36L, 37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L, 45L, 
    47L, 46L, 49L, 48L, 50L, 51L), .Label = c("Ala.", "Alaska", 
    "Ariz.", "Ark.", "Calif.", "Colo.", "Conn.", "D.C.", "Del.", 
    "Fla.", "Ga.", "Hawaii", "Idaho", "Ill.", "Ind.", "Iowa", 
    "Kans.", "Ky.", "La.", "Maine", "Mass.", "Md.", "Mich.", 
    "Minn.", "Miss.", "Mo.", "Mont.", "N.C.", "N.D.", "N.H.", 
    "N.J.", "N.M.", "N.Y.", "Nebr.", "Nev.", "Ohio", "Okla.", 
    "Ore.", "Pa.", "R.I.", "S.C.", "S.D.", "Tenn.", "Tex.", "Utah", 
    "Va.", "Vt.", "W.Va.", "Wash.", "Wis.", "Wyo."), class = "factor"), 
    Code = structure(c(2L, 1L, 4L, 3L, 5L, 6L, 7L, 9L, 8L, 10L, 
    11L, 12L, 14L, 15L, 16L, 13L, 17L, 18L, 19L, 22L, 21L, 20L, 
    23L, 24L, 26L, 25L, 27L, 30L, 34L, 31L, 32L, 33L, 35L, 28L, 
    29L, 36L, 37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L, 45L, 47L, 
    46L, 48L, 50L, 49L, 51L), .Label = c("AK", "AL", "AR", "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", "RI", "SC", "SD", "TN", "TX", 
    "UT", "VA", "VT", "WA", "WI", "WV", "WY"), class = "factor"), 
    n_votes = c(9, 3, 11, 6, 55, 9, 7, 3, 3, 29, 16, 4, 4, 20, 
    11, 6, 6, 8, 8, 2, 10, 11, 16, 10, 6, 10, 3, 2, 6, 4, 14, 
    5, 29, 15, 3, 18, 7, 7, 20, 4, 9, 3, 11, 38, 6, 3, 13, 12, 
    5, 10, 3), Attitude = structure(c(4L, 5L, 5L, 4L, 3L, 1L, 
    3L, 3L, 3L, 1L, 5L, 3L, 4L, 3L, 2L, 2L, 2L, 4L, 4L, 3L, 3L, 
    3L, 1L, 5L, 4L, 2L, 4L, 4L, 1L, 1L, 3L, 3L, 3L, 5L, 4L, 5L, 
    4L, 3L, 1L, 3L, 5L, 4L, 4L, 5L, 2L, 3L, 1L, 3L, 4L, 1L, 4L
    ), .Label = c("Leaning to Democrat", "Leaning to Republican", 
    "Solid Democrat", "Solid Republican", "Toss-up"), class = "factor"), 
    fips = structure(1:51, .Label = c("01", "02", "04", "05", 
    "06", "08", "09", "10", "11", "12", "13", "15", "16", "17", 
    "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", 
    "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", 
    "38", "39", "40", "41", "42", "44", "45", "46", "47", "48", 
    "49", "50", "51", "53", "54", "55", "56"), class = "factor")), 
    class = "data.frame", row.names = c(NA, -51L))
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63488086

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档