3

I have a data set, df, in format as:

    Country  Population
     US      1000
     Germany 3000
     Brazil  5000
     France  6000
     ......  

I would like to treat Population as density and plot the density in gradient colors in the world map.
My code as follows:

    df <- joinCountryData2Map(df, joinCode="NAME", nameJoinColumn="country")
      tm_shape(World)+
        tm_shape(df)+
        tm_polygons(df$Population, palette = "-Blues", 
                    title = "Income class", contrast = 0.7, 
                    border.col = "gray30", id = "name") +
        tm_fill(df$Population)+
        tm_text("iso_a3", size = "AREA", col = "gray30", root=3) +
        tm_style_classic()  

Error: Specify at least one layer after each tm_shape

I had two questions:
1) How to plot the country data as descirbed?
2) How to convert the original data set df into Spatial, Raster or sf data? Besides the package I used "countrycode", how to convert the data in a clearer way like sp/sf/rgal or raster, so that I could see and understand the procedure of spatial data transforming?

Any help would be appreciated.

Updated with dput, I found this public data sets package "wpp2015", and generated a simple data set well representing my question:

    dput(df) <- structure(list(name = structure(c(1L, 3L, 4L, 5L, 6L, 14L, 7L, 11L, 13L, 15L, 16L, 17L, 8L, 18L, 20L, 23L, 24L, 25L, 26L, 27L, 21L, 190L, 28L, 29L, 146L, 31L, 19L, 33L, 34L, 35L, 32L, 37L, 201L, 40L, 42L, 43L, 46L, 47L, 48L, 134L, 49L, 58L, 50L, 52L, 53L, 55L, 56L, 22L, 59L, 61L, 65L, 67L, 68L, 71L, 69L, 70L, 73L, 74L, 75L, 76L, 77L, 60L, 78L, 80L, 79L, 203L, 81L, 82L, 108L, 83L, 84L, 85L, 86L, 87L, 88L, 90L, 91L, 93L, 44L, 94L, 95L, 96L, 97L, 98L, 99L, 100L, 101L, 102L, 51L, 103L, 104L, 106L, 105L, 107L, 57L, 173L, 109L, 110L, 111L, 115L, 116L, 113L, 119L, 120L, 121L, 124L, 45L, 125L, 126L, 127L, 128L, 129L, 130L, 131L, 132L, 133L, 136L, 141L, 174L, 142L, 144L, 145L, 160L, 147L, 148L, 149L, 54L, 9L, 150L, 231L, 151L, 152L, 153L, 154L, 158L, 138L, 162L, 163L, 164L, 165L, 166L, 167L, 168L, 170L, 89L, 214L, 171L, 172L, 175L, 176L, 177L, 178L, 179L, 202L, 181L, 182L, 183L, 184L, 185L, 186L, 187L, 188L, 233L, 189L, 191L, 194L, 241L, 200L, 196L, 205L, 237L, 206L, 207L, 208L, 209L, 210L, 211L, 213L, 215L, 216L, 217L, 223L, 218L, 219L, 220L, 221L, 222L, 212L, 66L, 224L, 41L, 225L, 226L, 227L, 30L, 229L, 230L, 232L, 180L, 239L, 240L, 238L, 143L, 117L, 2L, 112L, 156L, 63L, 72L, 159L, 62L, 140L, 155L, 197L, 234L, 36L, 38L, 193L, 192L, 235L, 64L, 157L, 199L, 236L, 12L, 135L, 195L, 161L, 10L, 114L, 204L, 118L, 137L, 169L, 122L, 123L, 228L, 92L, 139L, 39L, 198L), .Label = c("Afghanistan", "Africa", "Albania", "Algeria", "Angola", "Antigua and Barbuda", "Argentina", "Armenia", "Aruba", "Asia", "Australia", "Australia/New Zealand", "Austria", "Azerbaijan", "Bahamas", "Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium", "Belize", "Benin", "Bhutan", "Bolivia (Plurinational State of)", "Bosnia and Herzegovina", "Botswana", "Brazil", "Brunei Darussalam", "Bulgaria", "Burkina Faso", "Burundi", "Cabo Verde", "Cambodia", "Cameroon", "Canada", "Caribbean", "Central African Republic", "Central America", "Central Asia", "Chad", "Channel Islands", "Chile", "China", "China, Hong Kong SAR", "China, Macao SAR", "China, Taiwan Province of China", "Colombia", "Comoros", "Congo", "Costa Rica", "Cote d'Ivoire", "Croatia", "Cuba", "Curacao", "Cyprus", "Czech Republic", "Dem. People's Rep. of Korea", "Dem. Republic of the Congo", "Denmark", "Djibouti", "Dominican Republic", "Eastern Africa", "Eastern Asia", "Eastern Europe", "Ecuador", "Egypt", "El Salvador", "Equatorial Guinea", "Eritrea", "Estonia", "Ethiopia", "Europe", "Fiji", "Finland", "France", "French Guiana", "French Polynesia", "Gabon", "Gambia", "Georgia", "Germany", "Ghana", "Greece", "Grenada", "Guadeloupe", "Guam", "Guatemala", "Guinea", "Guinea-Bissau", "Guyana", "Haiti", "High-income countries", "Honduras", "Hungary", "Iceland", "India", "Indonesia", "Iran (Islamic Republic of)", "Iraq", "Ireland", "Israel", "Italy", "Jamaica", "Japan", "Jordan", "Kazakhstan", "Kenya", "Kiribati", "Kuwait", "Kyrgyzstan", "Lao People's Dem. Republic", "Latin America and the Caribbean", "Latvia", "Least developed countries", "Lebanon", "Lesotho", "Less developed regions", "Less developed regions, excluding China", "Liberia", "Libya", "Lithuania", "Low-income countries", "Lower-middle-income countries", "Luxembourg", "Madagascar", "Malawi", "Malaysia", "Maldives", "Mali", "Malta", "Martinique", "Mauritania", "Mauritius", "Mayotte", "Melanesia", "Mexico", "Micronesia", "Micronesia (Fed. States of)", "Middle-income countries", "Middle Africa", "Mongolia", "Montenegro", "More developed regions", "Morocco", "Mozambique", "Myanmar", "Namibia", "Nepal", "Netherlands", "New Caledonia", "New Zealand", "Nicaragua", "Niger", "Nigeria", "Northern Africa", "Northern America", "Northern Europe", "Norway", "Oceania", "Oman", "Other less developed countries", "Pakistan", "Panama", "Papua New Guinea", "Paraguay", "Peru", "Philippines", "Poland", "Polynesia", "Portugal", "Puerto Rico", "Qatar", "Republic of Korea", "Republic of Moldova", "Reunion", "Romania", "Russian Federation", "Rwanda", "Saint Lucia", "Samoa", "Sao Tome and Principe", "Saudi Arabia", "Senegal", "Serbia", "Seychelles", "Sierra Leone", "Singapore", "Slovakia", "Slovenia", "Solomon Islands", "Somalia", "South-Central Asia", "South-Eastern Asia", "South Africa", "South America", "South Sudan", "Southern Africa", "Southern Asia", "Southern Europe", "Spain", "Sri Lanka", "St. Vincent and the Grenadines", "State of Palestine", "Sub-Saharan Africa", "Sudan", "Suriname", "Swaziland", "Sweden", "Switzerland", "Syrian Arab Republic", "Tajikistan", "TFYR Macedonia", "Thailand", "Timor-Leste", "Togo", "Tonga", "Trinidad and Tobago", "Tunisia", "Turkey", "Turkmenistan", "Uganda", "Ukraine", "United Arab Emirates", "United Kingdom", "United Republic of Tanzania", "United States of America", "United States Virgin Islands", "Upper-middle-income countries", "Uruguay", "Uzbekistan", "Vanuatu", "Venezuela (Bolivarian Republic of)", "Viet Nam", "Western Africa", "Western Asia", "Western Europe", "Western Sahara", "World", "Yemen", "Zambia", "Zimbabwe"), class = "factor"), `1950` = c(7752.118, 1263.171, 8872.247, 4354.882, 46.301, 2895.997, 17150.335, 8177.344, 6936.445, 79.088, 115.614, 37894.68, 1353.506, 210.995, 8628.489, 176.795, 3089.649, 2661.293, 412.533, 53974.726, 68.918, 89.793, 48.001, 7250.999, 17527.243, 2308.923, 7745.003, 4432.716, 4466.498, 13736.997, 178.066, 1326.653, 8075.81, 2502.314, 6142.899, 544112.923, 7561.863, 12340.899, 156.334, 15.141, 807.726, 12183.661, 959.489, 3850.295, 5919.997, 494.014, 8902.619, 2255.221, 4268.27, 2364.65, 3470.162, 2199.897, 225.536, 18128.034, 1142.15, 1100.998, 288.993, 4008.299, 41879.607, 25.479, 60.268, 62.001, 473.3, 3527.004, 271.372, 931.926, 69786.246, 4980.878, 33.05, 7566.002, 76.676, 209.999, 59.65, 3146.073, 3093.651, 406.562, 3221.277, 1487.235, 1973.998, 9337.723, 142.656, 376325.205, 69543.319, 17119.263, 5719.191, 2913.093, 1257.971, 46598.602, 2630.131, 1402.896, 82199.47, 6702.996, 448.861, 6076.757, 10549.469, 19211.386, 152.25, 1740, 1682.916, 1334.618, 733.942, 1949, 930.026, 1113.382, 2567.402, 296.001, 196.482, 4083.554, 2953.871, 6109.907, 73.715, 4708.425, 311.997, 222.001, 660.491, 493.254, 28012.558, 780.2, 2341.003, 394.738, 8985.99, 6313.29, 456.418, 485.274, 8483.321, 10027.047, 100.184, 38.066, 64.824, 47.695, 1908.001, 1294.993, 2559.703, 37859.745, 3265.278, 32, 37542.38, 859.66, 1708.192, 1473.245, 7727.735, 18580.487, 24824.013, 8416.969, 535.429, 433.398, 2218, 24.999, 248.111, 16236.292, 102798.657, 2186.187, 82.783, 67, 60, 3121.336, 2476.638, 6732.256, 36.322, 1944.001, 1022.098, 3436.574, 24809.903, 1473.094, 2264.081, 13683.162, 2746.854, 28069.737, 2582.929, 5733.944, 13.766, 214.999, 273, 7009.913, 4668.088, 3413.329, 1531.502, 20710.356, 1395.458, 47.22, 645.628, 69.59, 3605.31, 21238.496, 1211, 5158.193, 37297.652, 1254.444, 20897.237, 50616.012, 102.235, 7649.766, 157813.04, 26.795, 4284.457, 2238.506, 6945.397, 5481.977, 82.102, 4402.32, 2316.95, 2525149.312, 812988.79, 1712160.522, 228901.723, 168843.911, 171614.868, 666585.791, 549089.107, 12681.946, 66922.702, 26400.57, 49221.876, 15587.911, 70768.664, 17075.654, 38028.823, 164900.344, 511574.182, 50957.44, 220170.535, 78029.913, 108632.979, 142255.68, 10085.345, 2199.497, 113739.434, 1516435.967, 1394017.757, 195724.555, 179679.847, 1158315.256, 155.093, 242.011, 130103.438, 768893.01, 824937.314, 800383.367, 1593830.324, 18130.895, 493443.287)), .Names = c("name", "1950"), class = "data.frame", row.names = c(NA, -241L)) 
HW-Scientist
  • 394
  • 2
  • 7
  • 20

2 Answers2

3

You need to merge the data from your df to World which is a SpatialPolygonsDataFrame. After that you can plot 1950 column as part of the World polygon.

Edit: as pointed out by RobertH in the comment, it's better to use World <- merge(World, df, by = "name")

library(tmap)

data(World)
str(World, max.level = 2)

#> Formal class 'SpatialPolygonsDataFrame' [package "sp"] with 5 slots
#>   ..@ data       :'data.frame':  177 obs. of  15 variables:
#>   ..@ polygons   :List of 177
#>   ..@ plotOrder  : int [1:177] 136 7 28 31 169 23 9 74 5 84 ...
#>   ..@ bbox       : num [1:2, 1:2] -16656124 -8451673 16656124 8375779
#>   .. ..- attr(*, "dimnames")=List of 2
#>   ..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slot

# Merge df data into World
World <- sp::merge(World, df, by = "name")

tm_shape(World) +
  tm_polygons("1950", palette = "Blues", 
              title = "Income class", contrast = 0.7, 
              border.col = "gray30", id = "name") +
  tm_text("iso_a3", size = "AREA", col = "gray30", root = 3) +
  tm_style_classic()  

enter image description here

Created on 2018-03-21 by the reprex package (v0.2.0).

Tung
  • 26,371
  • 7
  • 91
  • 115
  • Hi Tung, I checked carefully, I did not plot my data successfully. I kept getting wrong map with some countries' values failed being plotted in the map. Do you have any idea what issue this could be? – HW-Scientist Mar 23 '18 at 06:41
  • That's strange. I'm not sure what's the problem as I don't see anything wrong with the code. Maybe you should file an issue on [tmap GitHub](https://github.com/mtennekes/tmap/issues) to see what's going on – Tung Mar 23 '18 at 08:05
  • 2
    It would be better to do `World <- merge(World, df, by = "name"` instead of merge with `World@data` – Robert Hijmans Mar 26 '18 at 16:48
  • @RobertH, this worked, I got the expected map, thanks a lot! What is the difference here 'World@data <- merge' and 'World <- merge' that leads different outputs? – HW-Scientist Mar 27 '18 at 01:48
  • 1
    @HW-Scientist: from what I read, merging 2 df of a spatial object that way could mess up the order of the rows in the result – Tung Mar 27 '18 at 18:38
  • @RobertH, got it. Thanks a lot! – HW-Scientist Mar 28 '18 at 01:22
3

Use the append_data function from tmaptools...

library(tmap)
library(tmaptools)

data(World)

df <- read.csv(header = TRUE, text = "
Country,Population
United States,1000
Germany,3000
Brazil,5000
France,6000
")

World <- append_data(World, df, key.shp = "name", key.data = "Country", 
                     ignore.na = TRUE)

tm_shape(World) +
  tm_polygons("Population", title = "Pop Class", palette = "Blues", 
              contrast = 0.7, border.col = "gray30", id = "name") +
  tm_text("iso_a3", size = "AREA", col = "gray30", root = 3)
CJ Yetman
  • 8,373
  • 2
  • 24
  • 56