require(maps)
## Loading required package: maps
library(sp)

data(world.cities)
head(world.cities,4)
##                 name country.etc   pop   lat  long capital
## 1 'Abasan al-Jadidah   Palestine  5629 31.31 34.34       0
## 2 'Abasan al-Kabirah   Palestine 18999 31.32 34.35       0
## 3       'Abdul Hakim    Pakistan 47788 30.55 72.11       0
## 4 'Abdullah-as-Salam      Kuwait 21817 29.36 47.98       0
table(world.cities$country.etc)
## 
##                      Afghanistan                          Albania 
##                              117                               67 
##                          Algeria                   American Samoa 
##                              316                               35 
##                          Andorra                           Angola 
##                                7                               37 
##                         Anguilla              Antigua and Barbuda 
##                                8                               13 
##                        Argentina                          Armenia 
##                               82                              402 
##                            Aruba                        Australia 
##                                2                              213 
##                          Austria                       Azerbaijan 
##                              235                              187 
##                           Azores                          Bahamas 
##                               11                               26 
##                          Bahrain                       Bangladesh 
##                               11                              143 
##                         Barbados                          Belarus 
##                               11                              102 
##                          Belgium                           Belize 
##                              552                                9 
##                            Benin                          Bermuda 
##                               33                                2 
##                           Bhutan                          Bolivia 
##                               38                              122 
##           Bosnia and Herzegovina                         Botswana 
##                              226                              154 
##                           Brazil           British Virgin Islands 
##                              975                                1 
##                           Brunei                         Bulgaria 
##                                5                              241 
##                     Burkina Faso                          Burundi 
##                               49                               17 
##                         Cambodia                         Cameroon 
##                               25                              135 
##                           Canada                   Canary Islands 
##                              917                               53 
##                       Cape Verde                   Cayman Islands 
##                               15                                5 
##         Central African Republic                             Chad 
##                               37                               42 
##                            Chile                            China 
##                              107                              892 
##                         Colombia                          Comoros 
##                              987                              154 
##                            Congo        Congo Democratic Republic 
##                               19                               74 
##                     Cook Islands                       Costa Rica 
##                               11                              141 
##                          Croatia                             Cuba 
##                              545                              129 
##                           Cyprus                   Czech Republic 
##                              315                              950 
##                          Denmark                         Djibouti 
##                              310                               11 
##                         Dominica               Dominican Republic 
##                               19                              202 
##                       East Timor                    Easter Island 
##                               19                                1 
##                          Ecuador                            Egypt 
##                              113                              153 
##                      El Salvador                Equatorial Guinea 
##                              106                               24 
##                          Eritrea                          Estonia 
##                               14                              955 
##                         Ethiopia                 Falkland Islands 
##                              133                                4 
##                    Faroe Islands                             Fiji 
##                              116                               22 
##                          Finland                           France 
##                              430                             1000 
##                    French Guiana                 French Polynesia 
##                               15                              116 
##                            Gabon                           Gambia 
##                               33                              302 
##                          Georgia                          Germany 
##                              113                              998 
##                            Ghana                        Gibraltar 
##                               75                                1 
##                           Greece                        Greenland 
##                             1000                              102 
##                          Grenada                       Guadeloupe 
##                                7                               27 
##                             Guam                        Guatemala 
##                               27                              331 
##            Guernsey and Alderney                           Guinea 
##                                3                               37 
##                    Guinea-Bissau                           Guyana 
##                               15                               17 
##                            Haiti                         Honduras 
##                              116                              541 
##                          Hungary                          Iceland 
##                              844                              100 
##                            India                        Indonesia 
##                              963                              476 
##                             Iran                             Iraq 
##                              266                               80 
##                          Ireland                      Isle of Man 
##                              165                                8 
##                           Israel                            Italy 
##                              193                              985 
##                      Ivory Coast                          Jamaica 
##                               66                              103 
##                            Japan                           Jersey 
##                              999                               12 
##                           Jordan                       Kazakhstan 
##                              151                              320 
##                            Kenya                         Kiribati 
##                              138                               24 
##                      Korea North                      Korea South 
##                              130                              132 
##                           Kuwait                       Kyrgyzstan 
##                               70                               33 
##                             Laos                           Latvia 
##                               18                               77 
##                          Lebanon                          Lesotho 
##                               19                               11 
##                          Liberia                            Libya 
##                               10                               44 
##                    Liechtenstein                        Lithuania 
##                               11                              113 
##                       Luxembourg                        Macedonia 
##                              473                              117 
##                       Madagascar                          Madeira 
##                               71                                9 
##                          Madiera                           Malawi 
##                                1                               33 
##                         Malaysia                         Maldives 
##                              163                              201 
##                             Mali                            Malta 
##                               37                               66 
##                 Marshall Islands                       Martinique 
##                               99                               21 
##                       Mauritania                        Mauritius 
##                               29                              115 
##                          Mayotte                           Mexico 
##                               17                              939 
##                       Micronesia                          Moldova 
##                               62                               44 
##                           Monaco                         Mongolia 
##                                4                               25 
##                       Montserrat                          Morocco 
##                                6                               74 
##                       Mozambique                          Myanmar 
##                               28                               66 
##                          Namibia                            Nauru 
##                               39                                1 
##                            Nepal                      Netherlands 
##                               52                              318 
##             Netherlands Antilles                    New Caledonia 
##                               44                               32 
##                      New Zealand                        Nicaragua 
##                              276                              147 
##                            Niger                          Nigeria 
##                               42                              521 
##                             Niue                   Norfolk Island 
##                               13                                1 
##         Northern Mariana Islands                           Norway 
##                               15                              714 
##                             Oman                         Pakistan 
##                               42                              418 
##                            Palau                        Palestine 
##                               17                              244 
##                           Panama                 Papua New Guinea 
##                              254                               41 
##                         Paraguay                             Peru 
##                              228                              272 
##                      Philippines                         Pitcairn 
##                              980                                1 
##                           Poland                         Portugal 
##                              439                              438 
##                      Puerto Rico                            Qatar 
##                               80                               27 
##                          Reunion                          Romania 
##                               23                              999 
##                           Russia                           Rwanda 
##                              985                               12 
##                 Saint-Barthelemy                     Saint-Martin 
##                                1                                1 
##                     Saint Helena            Saint Kitts and Nevis 
##                                9                               16 
##                      Saint Lucia        Saint Pierre and Miquelon 
##                               12                                2 
## Saint Vincent and The Grenadines                            Samoa 
##                                8                              198 
##                       San Marino            Sao Tome and Principe 
##                                9                                6 
##                     Saudi Arabia                          Senegal 
##                              186                               59 
##            Serbia and Montenegro                       Seychelles 
##                              254                                1 
##                           Sicily                     Sierra Leone 
##                                2                               97 
##                        Singapore                         Slovakia 
##                                1                              129 
##                         Slovenia                  Solomon Islands 
##                              203                                9 
##                          Somalia                     South Africa 
##                               63                              226 
##                            Spain                        Sri Lanka 
##                              864                               71 
##                            Sudan                         Suriname 
##                              118                               14 
##           Svalbard and Jan Mayen                        Swaziland 
##                                7                               27 
##                           Sweden                      Switzerland 
##                              104                              826 
##                            Syria                           Taiwan 
##                              105                               98 
##                       Tajikistan                         Tanzania 
##                               80                              308 
##                         Thailand                             Togo 
##                              293                               21 
##                          Tokelau                            Tonga 
##                                3                                9 
##              Trinidad and Tobago                          Tunisia 
##                               22                              200 
##                           Turkey                     Turkmenistan 
##                              791                               32 
##                 Turks and Caicos                           Tuvalu 
##                                2                                8 
##                           Uganda                               UK 
##                               90                              925 
##                          Ukraine             United Arab Emirates 
##                              959                                9 
##                          Uruguay                US Virgin Islands 
##                              135                                9 
##                              USA                       Uzbekistan 
##                              997                              153 
##                          Vanuatu                     Vatican City 
##                                8                                1 
##                        Venezuela                          Vietnam 
##                              104                               68 
##                Wallis and Futuna                   Western Sahara 
##                               23                                4 
##                            Yemen                           Zambia 
##                               30                               73 
##                         Zimbabwe 
##                               76
citiesIND <- world.cities[world.cities$country.etc == "India",]
class(citiesIND)
## [1] "data.frame"
coordinates(citiesIND) <- ~long + lat
class(citiesIND)
## [1] "SpatialPointsDataFrame"
## attr(,"package")
## [1] "sp"
str(citiesIND)
## Formal class 'SpatialPointsDataFrame' [package "sp"] with 5 slots
##   ..@ data       :'data.frame':  963 obs. of  4 variables:
##   .. ..$ name       : chr [1:963] "Abohar" "Achalpur" "Adilabad" "Adityapur" ...
##   .. ..$ country.etc: chr [1:963] "India" "India" "India" "India" ...
##   .. ..$ pop        : int [1:963] 132117 112173 121168 145045 165441 206666 1475091 59232 3769846 383784 ...
##   .. ..$ capital    : int [1:963] 0 0 0 0 0 0 0 0 0 0 ...
##   ..@ coords.nrs : int [1:2] 5 4
##   ..@ coords     : num [1:963, 1:2] 74.2 77.5 78.5 86 77.3 ...
##   .. ..- attr(*, "dimnames")=List of 2
##   .. .. ..$ : chr [1:963] "235" "315" "396" "399" ...
##   .. .. ..$ : chr [1:2] "long" "lat"
##   ..@ bbox       : num [1:2, 1:2] 69.6 8.18 95.36 34.3
##   .. ..- attr(*, "dimnames")=List of 2
##   .. .. ..$ : chr [1:2] "long" "lat"
##   .. .. ..$ : chr [1:2] "min" "max"
##   ..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slot
##   .. .. ..@ projargs: chr NA
nrow(citiesIND@data)
## [1] 963
#creating random data for dengue deaths
citiesIND@data[["samp"]] <- sample(40:1000,963,T)
head(citiesIND@data)
##          name country.etc    pop capital samp
## 235    Abohar       India 132117       0  282
## 315  Achalpur       India 112173       0  686
## 396  Adilabad       India 121168       0  827
## 399 Adityapur       India 145045       0  812
## 411     Adoni       India 165441       0  217
## 467  Agartala       India 206666       0  671
summary(citiesIND@data$pop)
##     Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
##    51210    66420    99660   242800   180700 12880000
par(mfrow=c(1,1))
plot(citiesIND, axes=T, asp=1, pch=16,main="Spatial Plot of Cities in India")
## Highlight big cities
plot(citiesIND[citiesIND@data$pop > 1000000, ], pch=1, col="red", cex=3, add=TRUE)
## Highlight  cities with bigger  dengue deaths
plot(citiesIND[citiesIND@data$samp > 960, ], pch=1, col="blue", cex=3, add=TRUE)