0

I would like to create a map (or, better, a series of maps) representing how population has evolved through time.

For the sake of clarity I will be using tmap's built in metro sample data.

library(tmap)
data("World", "metro")

> str(metro@data)
'data.frame':   436 obs. of  12 variables:
 $ name     : chr  "Kabul" "Algiers" "Luanda" "Buenos Aires" ...
 $ name_long: chr  "Kabul" "El Djazair  (Algiers)" "Luanda" "Buenos Aires" ...
 $ iso_a3   : chr  "AFG" "DZA" "AGO" "ARG" ...
 $ pop1950  : num  170784 516450 138413 5097612 429249 ...
 $ pop1960  : num  285352 871636 219427 6597634 605309 ...
 $ pop1970  : num  471891 1281127 459225 8104621 809794 ...
 $ pop1980  : num  977824 1621442 771349 9422362 1009521 ...
 $ pop1990  : num  1549320 1797068 1390240 10513284 1200168 ...
 $ pop2000  : num  2401109 2140577 2591388 12406780 1347561 ...
 $ pop2010  : num  3722320 2432023 4508434 14245871 1459268 ...
 $ pop2020  : num  5721697 2835218 6836849 15894307 1562509 ...
 $ pop2030  : num  8279607 3404575 10428756 16956491 1718192 ...
> 

As can be seen the data is structured on a wide format in which there are several variables (one per year) which have their own set of values. I know I can use tmap's facetting and create a vector containing each column's name, like that:

tm_shape(World) +
  tm_polygons() +
tm_shape(metro) +
  tm_dots(c("pop2010", "pop2020"), col = c("pop2010", "pop2020"),
          size = c("pop2010", "pop2020"), border.col = "black")

And that's the output:

enter image description here

As can be seen data has been messed up, as I wanted that each facet displayed only each year's population (in size and color), but I do not know where do the shapes come from.

Could you please tell me what am I doing wrong?

ccamara
  • 1,141
  • 1
  • 12
  • 32

1 Answers1

1

I am unsure why you want to display population in both size and color when you have only one variable (population size).

You can try the following:

library(tmap)
data("World", "metro")
tm_shape(World) + tm_polygons() + 
tm_shape(metro) + tm_dots(size = c("pop2010", "pop2020"), col = "red", border.col = "black")

It will result in the following picture - map of metro areas, with size of the dots marking population size. The red color is my emphasis (default would be black).

tmap of metro population

Jindra Lacko
  • 7,814
  • 3
  • 22
  • 44
  • Thank you! I agree that this example didn't make much sense to have colors and sizes for the same value, but I'll do it with the real scenario. Thanks to your answer I realized that my problem was the first vector. Regards – ccamara Nov 10 '17 at 10:19
  • Glad to be of service! – Jindra Lacko Nov 10 '17 at 13:11