Example 1: World GPD
- read GPD (in blllions) data from internet
library(kableExtra)
df <- read.csv('https://raw.githubusercontent.com/plotly/datasets/master/2014_world_gdp_with_codes.csv')
kable(df)%>%kable_styling()%>%scroll_box(width="500px",height = "500px")
COUNTRY
|
GDP..BILLIONS.
|
CODE
|
Afghanistan
|
21.71
|
AFG
|
Albania
|
13.40
|
ALB
|
Algeria
|
227.80
|
DZA
|
American Samoa
|
0.75
|
ASM
|
Andorra
|
4.80
|
AND
|
Angola
|
131.40
|
AGO
|
Anguilla
|
0.18
|
AIA
|
Antigua and Barbuda
|
1.24
|
ATG
|
Argentina
|
536.20
|
ARG
|
Armenia
|
10.88
|
ARM
|
Aruba
|
2.52
|
ABW
|
Australia
|
1483.00
|
AUS
|
Austria
|
436.10
|
AUT
|
Azerbaijan
|
77.91
|
AZE
|
Bahamas, The
|
8.65
|
BHM
|
Bahrain
|
34.05
|
BHR
|
Bangladesh
|
186.60
|
BGD
|
Barbados
|
4.28
|
BRB
|
Belarus
|
75.25
|
BLR
|
Belgium
|
527.80
|
BEL
|
Belize
|
1.67
|
BLZ
|
Benin
|
9.24
|
BEN
|
Bermuda
|
5.20
|
BMU
|
Bhutan
|
2.09
|
BTN
|
Bolivia
|
34.08
|
BOL
|
Bosnia and Herzegovina
|
19.55
|
BIH
|
Botswana
|
16.30
|
BWA
|
Brazil
|
2244.00
|
BRA
|
British Virgin Islands
|
1.10
|
VGB
|
Brunei
|
17.43
|
BRN
|
Bulgaria
|
55.08
|
BGR
|
Burkina Faso
|
13.38
|
BFA
|
Burma
|
65.29
|
MMR
|
Burundi
|
3.04
|
BDI
|
Cabo Verde
|
1.98
|
CPV
|
Cambodia
|
16.90
|
KHM
|
Cameroon
|
32.16
|
CMR
|
Canada
|
1794.00
|
CAN
|
Cayman Islands
|
2.25
|
CYM
|
Central African Republic
|
1.73
|
CAF
|
Chad
|
15.84
|
TCD
|
Chile
|
264.10
|
CHL
|
China
|
10360.00
|
CHN
|
Colombia
|
400.10
|
COL
|
Comoros
|
0.72
|
COM
|
Congo, Democratic Republic of the
|
32.67
|
COD
|
Congo, Republic of the
|
14.11
|
COG
|
Cook Islands
|
0.18
|
COK
|
Costa Rica
|
50.46
|
CRI
|
Cote d’Ivoire
|
33.96
|
CIV
|
Croatia
|
57.18
|
HRV
|
Cuba
|
77.15
|
CUB
|
Curacao
|
5.60
|
CUW
|
Cyprus
|
21.34
|
CYP
|
Czech Republic
|
205.60
|
CZE
|
Denmark
|
347.20
|
DNK
|
Djibouti
|
1.58
|
DJI
|
Dominica
|
0.51
|
DMA
|
Dominican Republic
|
64.05
|
DOM
|
Ecuador
|
100.50
|
ECU
|
Egypt
|
284.90
|
EGY
|
El Salvador
|
25.14
|
SLV
|
Equatorial Guinea
|
15.40
|
GNQ
|
Eritrea
|
3.87
|
ERI
|
Estonia
|
26.36
|
EST
|
Ethiopia
|
49.86
|
ETH
|
Falkland Islands (Islas Malvinas)
|
0.16
|
FLK
|
Faroe Islands
|
2.32
|
FRO
|
Fiji
|
4.17
|
FJI
|
Finland
|
276.30
|
FIN
|
France
|
2902.00
|
FRA
|
French Polynesia
|
7.15
|
PYF
|
Gabon
|
20.68
|
GAB
|
Gambia, The
|
0.92
|
GMB
|
Georgia
|
16.13
|
GEO
|
Germany
|
3820.00
|
DEU
|
Ghana
|
35.48
|
GHA
|
Gibraltar
|
1.85
|
GIB
|
Greece
|
246.40
|
GRC
|
Greenland
|
2.16
|
GRL
|
Grenada
|
0.84
|
GRD
|
Guam
|
4.60
|
GUM
|
Guatemala
|
58.30
|
GTM
|
Guernsey
|
2.74
|
GGY
|
Guinea-Bissau
|
1.04
|
GNB
|
Guinea
|
6.77
|
GIN
|
Guyana
|
3.14
|
GUY
|
Haiti
|
8.92
|
HTI
|
Honduras
|
19.37
|
HND
|
Hong Kong
|
292.70
|
HKG
|
Hungary
|
129.70
|
HUN
|
Iceland
|
16.20
|
ISL
|
India
|
2048.00
|
IND
|
Indonesia
|
856.10
|
IDN
|
Iran
|
402.70
|
IRN
|
Iraq
|
232.20
|
IRQ
|
Ireland
|
245.80
|
IRL
|
Isle of Man
|
4.08
|
IMN
|
Israel
|
305.00
|
ISR
|
Italy
|
2129.00
|
ITA
|
Jamaica
|
13.92
|
JAM
|
Japan
|
4770.00
|
JPN
|
Jersey
|
5.77
|
JEY
|
Jordan
|
36.55
|
JOR
|
Kazakhstan
|
225.60
|
KAZ
|
Kenya
|
62.72
|
KEN
|
Kiribati
|
0.16
|
KIR
|
Korea, North
|
28.00
|
PRK
|
Korea, South
|
1410.00
|
KOR
|
Kosovo
|
5.99
|
KSV
|
Kuwait
|
179.30
|
KWT
|
Kyrgyzstan
|
7.65
|
KGZ
|
Laos
|
11.71
|
LAO
|
Latvia
|
32.82
|
LVA
|
Lebanon
|
47.50
|
LBN
|
Lesotho
|
2.46
|
LSO
|
Liberia
|
2.07
|
LBR
|
Libya
|
49.34
|
LBY
|
Liechtenstein
|
5.11
|
LIE
|
Lithuania
|
48.72
|
LTU
|
Luxembourg
|
63.93
|
LUX
|
Macau
|
51.68
|
MAC
|
Macedonia
|
10.92
|
MKD
|
Madagascar
|
11.19
|
MDG
|
Malawi
|
4.41
|
MWI
|
Malaysia
|
336.90
|
MYS
|
Maldives
|
2.41
|
MDV
|
Mali
|
12.04
|
MLI
|
Malta
|
10.57
|
MLT
|
Marshall Islands
|
0.18
|
MHL
|
Mauritania
|
4.29
|
MRT
|
Mauritius
|
12.72
|
MUS
|
Mexico
|
1296.00
|
MEX
|
Micronesia, Federated States of
|
0.34
|
FSM
|
Moldova
|
7.74
|
MDA
|
Monaco
|
6.06
|
MCO
|
Mongolia
|
11.73
|
MNG
|
Montenegro
|
4.66
|
MNE
|
Morocco
|
112.60
|
MAR
|
Mozambique
|
16.59
|
MOZ
|
Namibia
|
13.11
|
NAM
|
Nepal
|
19.64
|
NPL
|
Netherlands
|
880.40
|
NLD
|
New Caledonia
|
11.10
|
NCL
|
New Zealand
|
201.00
|
NZL
|
Nicaragua
|
11.85
|
NIC
|
Nigeria
|
594.30
|
NGA
|
Niger
|
8.29
|
NER
|
Niue
|
0.01
|
NIU
|
Northern Mariana Islands
|
1.23
|
MNP
|
Norway
|
511.60
|
NOR
|
Oman
|
80.54
|
OMN
|
Pakistan
|
237.50
|
PAK
|
Palau
|
0.65
|
PLW
|
Panama
|
44.69
|
PAN
|
Papua New Guinea
|
16.10
|
PNG
|
Paraguay
|
31.30
|
PRY
|
Peru
|
208.20
|
PER
|
Philippines
|
284.60
|
PHL
|
Poland
|
552.20
|
POL
|
Portugal
|
228.20
|
PRT
|
Puerto Rico
|
93.52
|
PRI
|
Qatar
|
212.00
|
QAT
|
Romania
|
199.00
|
ROU
|
Russia
|
2057.00
|
RUS
|
Rwanda
|
8.00
|
RWA
|
Saint Kitts and Nevis
|
0.81
|
KNA
|
Saint Lucia
|
1.35
|
LCA
|
Saint Martin
|
0.56
|
MAF
|
Saint Pierre and Miquelon
|
0.22
|
SPM
|
Saint Vincent and the Grenadines
|
0.75
|
VCT
|
Samoa
|
0.83
|
WSM
|
San Marino
|
1.86
|
SMR
|
Sao Tome and Principe
|
0.36
|
STP
|
Saudi Arabia
|
777.90
|
SAU
|
Senegal
|
15.88
|
SEN
|
Serbia
|
42.65
|
SRB
|
Seychelles
|
1.47
|
SYC
|
Sierra Leone
|
5.41
|
SLE
|
Singapore
|
307.90
|
SGP
|
Sint Maarten
|
304.10
|
SXM
|
Slovakia
|
99.75
|
SVK
|
Slovenia
|
49.93
|
SVN
|
Solomon Islands
|
1.16
|
SLB
|
Somalia
|
2.37
|
SOM
|
South Africa
|
341.20
|
ZAF
|
South Sudan
|
11.89
|
SSD
|
Spain
|
1400.00
|
ESP
|
Sri Lanka
|
71.57
|
LKA
|
Sudan
|
70.03
|
SDN
|
Suriname
|
5.27
|
SUR
|
Swaziland
|
3.84
|
SWZ
|
Sweden
|
559.10
|
SWE
|
Switzerland
|
679.00
|
CHE
|
Syria
|
64.70
|
SYR
|
Taiwan
|
529.50
|
TWN
|
Tajikistan
|
9.16
|
TJK
|
Tanzania
|
36.62
|
TZA
|
Thailand
|
373.80
|
THA
|
Timor-Leste
|
4.51
|
TLS
|
Togo
|
4.84
|
TGO
|
Tonga
|
0.49
|
TON
|
Trinidad and Tobago
|
29.63
|
TTO
|
Tunisia
|
49.12
|
TUN
|
Turkey
|
813.30
|
TUR
|
Turkmenistan
|
43.50
|
TKM
|
Tuvalu
|
0.04
|
TUV
|
Uganda
|
26.09
|
UGA
|
Ukraine
|
134.90
|
UKR
|
United Arab Emirates
|
416.40
|
ARE
|
United Kingdom
|
2848.00
|
GBR
|
United States
|
17420.00
|
USA
|
Uruguay
|
55.60
|
URY
|
Uzbekistan
|
63.08
|
UZB
|
Vanuatu
|
0.82
|
VUT
|
Venezuela
|
209.20
|
VEN
|
Vietnam
|
187.80
|
VNM
|
Virgin Islands
|
5.08
|
VGB
|
West Bank
|
6.64
|
WBG
|
Yemen
|
45.45
|
YEM
|
Zambia
|
25.61
|
ZMB
|
Zimbabwe
|
13.74
|
ZWE
|
- create map using Plotly package
library(plotly)
# light grey boundaries
l <- list(color = toRGB("grey"), width = 0.5)
# specify map projection/options
g <- list(
showframe = FALSE,
showcoastlines = FALSE,
projection = list(type = 'Mercator')
)
p <- plot_geo(df) %>%
add_trace(
z = ~GDP..BILLIONS., color = ~GDP..BILLIONS., colors = 'Blues',
text = ~COUNTRY, locations = ~CODE, marker = list(line = l)
) %>%
colorbar(title = 'GDP Billions US$', tickprefix = '$') %>%
layout(
title = '2014 Global GDP<br>Source:<a href="https://www.cia.gov/library/publications/the-world-factbook/fields/2195.html">CIA World Factbook</a>',
geo = g
)
p