将其他变量添加到plot_usmap

时间:2019-07-11 16:38:31

标签: r ggplot2 plot geo

我正在尝试绘制选举数据。我很想花时间在以下图表上绘制一些数值。

我想用变量winning_perc_2012winning_perc_2016显示每个州的获胜利润。

我目前拥有的地块: enter image description here

绘图代码:

E2012<-plot_usmap(data=election,
                  values='Winner2012',
                  lines='black')+
  labs(title='2012 Election Results')+
  scale_fill_manual(name = 'Party',values = party_colors,labels=c('Democrat','Republican'))+
theme(plot.title = element_text(color="Black", size=16, face="bold.italic",hjust = 0.5),
      plot.subtitle = element_text(color="Black", size=14, face="bold",
                                   hjust = 0.253,vjust=-205),

      panel.background = element_rect(fill = "white",
                                      size = 2, linetype = "solid"),
      plot.background = element_rect(fill = "white"),
      panel.grid = element_blank(),
      legend.position = 'bottom',
      legend.key = element_rect(fill = "white"),
      legend.background = element_rect(fill = "white"),
      legend.text = element_text(color='Black',size=10),
      legend.title = element_text(color="Black", size=14, face="bold")
)


E2016<-plot_usmap(data=election,
                  values='Winner2016',
                  lines='black')+ 
  labs(title='2016 Election Results')+
  scale_fill_manual(name = 'Party',values = party_colors,labels=c('Democrat','Republican'))+
  theme(plot.title = element_text(color="Black", size=16, face="bold.italic",hjust = 0.5),
        panel.background = element_rect(fill = "white",
                                        size = 2,),
        plot.background = element_rect(fill = "white"),
        legend.position = 'bottom',
        legend.background = element_rect(fill = "white"),
        legend.text = element_text(color='Black',size=10),
        legend.title = element_text(color="Black", size=14, face="bold")
  )


plot_grid(E2012,E2016)

dput输出:

structure(list(state = structure(1:51, .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"), dem_votes_2016 = c(2697087, 
718084, 378729, 936250, 7230699, 1212209, 884432, 260223, 235581, 
4485745, 1837300, 266827, 650790, 189677, 2977498, 1031953, 414788, 
628834, 779535, 1964768, 1497951, 354873, 2268193, 1366676, 1054889, 
462001, 174521, 2162074, 93526, 273858, 348521, 2021756, 380724, 
537753, 4143874, 2317001, 419788, 934631, 2844705, 249902, 849469, 
117442, 867110, 3867816, 274188, 1916845, 178179, 1610524, 1382210, 
187457, 55949), gop_votes_2016 = c(3781977, 1306925, 677904, 
1021154, 3841134, 1137455, 668266, 11553, 185103, 4605515, 2068623, 
128815, 798923, 407199, 2118179, 1556220, 656009, 1202942, 1178004, 
1083069, 873646, 334838, 2279805, 1322891, 1585753, 678457, 274120, 
2339603, 216133, 485819, 345789, 1535513, 315875, 511319, 2640570, 
2771984, 947934, 742506, 2912941, 179421, 1143611, 227701, 1517402, 
4681590, 452086, 1731156, 95053, 1129120, 1409467, 486198, 174248
), dem_votes_2012 = c(NA, 793620, 391953, 900081, 6241648, 1238490, 
912531, 222332, 242547, 4235270, 1761761, 303090, 816429, 212560, 
2934826, 1140425, 427918, 679340, 808496, 1901276, 1527686, 397754, 
2561911, 1547668, 1215031, 528260, 200489, 2178388, 124490, 289154, 
368529, 1960744, 408312, 528801, 3875826, 2697260, 442647, 932459, 
2907448, 274342, 845756, 144988, 953043, 3294440, 229463, 1904303, 
199259, 1588309, 1613950, 234925, 68780), gop_votes_2012 = c(NA, 
1252453, 643717, 1107130, 4046524, 1125391, 631432, 17337, 165476, 
4162081, 2070221, 119494, 727928, 420750, 2094526, 1412620, 678719, 
1087127, 1152460, 1178245, 904970, 290437, 2112673, 1321575, 
1478961, 674302, 264974, 2275853, 187586, 462972, 327870, 1383233, 
331915, 462422, 2226637, 2593779, 889372, 732288, 2619583, 155355, 
1049507, 210541, 1453097, 4555799, 671747, 1788091, 92700, 1183614, 
1408746, 412406, 170265), votes2016 = c(6479064, 2025009, 1056633, 
1957404, 11071833, 2349664, 1552698, 271776, 420684, 9091260, 
3905923, 395642, 1449713, 596876, 5095677, 2588173, 1070797, 
1831776, 1957539, 3047837, 2371597, 689711, 4547998, 2689567, 
2640642, 1140458, 448641, 4501677, 309659, 759677, 694310, 3557269, 
696599, 1049072, 6784444, 5088985, 1367722, 1677137, 5757646, 
429323, 1993080, 345143, 2384512, 8549406, 726274, 3648001, 273232, 
2739644, 2791677, 673655, 230197), votes2012 = c(0, 2046073, 
1035670, 2007211, 10288172, 2363881, 1543963, 239669, 408023, 
8397351, 3831982, 422584, 1544357, 633310, 5029352, 2553045, 
1106637, 1766467, 1960956, 3079521, 2432656, 688191, 4674584, 
2869243, 2693992, 1202562, 465463, 4454241, 312076, 752126, 696399, 
3343977, 740227, 991223, 6102463, 5291039, 1332019, 1664747, 
5527031, 429697, 1895263, 355529, 2406140, 7850239, 901210, 3692394, 
291959, 2771923, 3022696, 647331, 239045), winning_perc_2016 = c(0.167, 
0.291, 0.283, 0.043, 0.306, 0.032, 0.139, 0.915, 0.12, 0.013, 
0.059, 0.349, 0.102, 0.364, 0.169, 0.203, 0.225, 0.313, 0.204, 
0.289, 0.263, 0.029, 0.003, 0.016, 0.201, 0.19, 0.222, 0.039, 
0.396, 0.279, 0.004, 0.137, 0.093, 0.025, 0.222, 0.089, 0.386, 
0.115, 0.012, 0.164, 0.148, 0.319, 0.273, 0.095, 0.245, 0.051, 
0.304, 0.176, 0.01, 0.443, 0.514), winning_perc_2012 = c(NA, 
0.224, 0.243, 0.103, 0.213, 0.048, 0.182, 0.855, 0.189, 0.009, 
0.08, 0.434, 0.057, 0.329, 0.167, 0.107, 0.227, 0.231, 0.175, 
0.235, 0.256, 0.156, 0.096, 0.079, 0.098, 0.121, 0.139, 0.022, 
0.202, 0.231, 0.058, 0.173, 0.103, 0.067, 0.27, 0.02, 0.335, 
0.12, 0.052, 0.277, 0.108, 0.184, 0.208, 0.161, 0.491, 0.031, 
0.365, 0.146, 0.068, 0.274, 0.425), Winner2016 = c("Republican", 
"Republican", "Republican", "Republican", "Democrat", "Democrat", 
"Democrat", "Democrat", "Democrat", "Republican", "Republican", 
"Democrat", "Republican", "Republican", "Democrat", "Republican", 
"Republican", "Republican", "Republican", "Democrat", "Democrat", 
"Democrat", "Republican", "Democrat", "Republican", "Republican", 
"Republican", "Republican", "Republican", "Republican", "Democrat", 
"Democrat", "Democrat", "Democrat", "Democrat", "Republican", 
"Republican", "Democrat", "Republican", "Democrat", "Republican", 
"Republican", "Republican", "Republican", "Republican", "Democrat", 
"Democrat", "Democrat", "Republican", "Republican", "Republican"
), Winner2012 = c("Republican", "Republican", "Republican", "Republican", 
"Democrat", "Democrat", "Democrat", "Democrat", "Democrat", "Democrat", 
"Republican", "Democrat", "Democrat", "Republican", "Democrat", 
"Republican", "Republican", "Republican", "Republican", "Democrat", 
"Democrat", "Democrat", "Democrat", "Democrat", "Republican", 
"Republican", "Republican", "Republican", "Republican", "Republican", 
"Democrat", "Democrat", "Democrat", "Democrat", "Democrat", "Democrat", 
"Republican", "Democrat", "Democrat", "Democrat", "Republican", 
"Republican", "Republican", "Republican", "Republican", "Democrat", 
"Democrat", "Democrat", "Democrat", "Republican", "Republican"
)), row.names = c(NA, -51L), class = c("tbl_df", "tbl", "data.frame"
))

0 个答案:

没有答案