sjewo / cartogram

r package for cartogram creation
https://sjewo.github.io/cartogram/
148 stars 15 forks source link

cartogram_ncont and cartogram_dorling shrink a map? #21

Open shadrackkibet opened 5 years ago

shadrackkibet commented 5 years ago

I am a newbie in using tmap package to produce maps!. So this might be a simple problem to solve. I am using cartogram_dorling and cartogram_ncont to Plot a map of Africa representing Life expectancy. I think the problem arises since this values are 'small' and maybe the range (26.962, 57.964) is small. So when representing this on the map, polygons are shrunk such that they are too small and not visible. For the case of cartogram_dorling they appear as circles and the shape of African is not maintained.

I import a shapefile and since I have life expectancy in a separate file I then append it as a column on the spatial polygon dataframe. I the proceed with mapping. Below is my code. I am sorry this code might not reproducible i am just attaching for display together with my outputs. Any quick solution to this?. Thanks.

world <- st_read("TM_WORLD_BORDERS-0.3/TM_WORLD_BORDERS-0.3.shp") africa <- world[world$REGION == 2, ]

Africa_life_Expectancy <- read_excel("Africa_Population.xlsx",sheet = "Sheet6")

africa <- africa[order(africa$NAME),]

africa <- as_Spatial(africa)

Africa_life_Expectancy <- Africa_life_Expectancy[order(Africa_life_Expectancy$Region),]

africa$Life_Expectancy_1950_1955 <- Africa_life_Expectancy$1950-1955

africa <- spTransform(africa, CRS("+init=epsg:3395"))

africa_cont <- cartogram_ncont(africa, 'Life_Expectancy_1950_1955 ', inplace = TRUE)

tm_shape(africa_cont) + tm_polygons('Life_Expectancy_1950_1955 ', style = "jenks", area = 'AREA') + tm_layout(frame = FALSE, legend.position = c("left","top"), legend.title.size = 2, legend.text.size = 0.6, legend.bg.color = "white", legend.outside=TRUE )

Here is the output:

image

africa_cont <- cartogram_dorling(africa, 'Life_Expectancy_1950_1955 ')

tm_shape(africa_cont) + tm_polygons('Life_Expectancy_1950_1955 ', style = "jenks") + tm_layout(frame = FALSE, legend.position = c("left","top"), legend.title.size = 1, legend.text.size = 0.6, legend.bg.color = "white", legend.outside=TRUE )

Ouput: image

Using cartogram_cont seems to be better. But the Mean size error for iteration is very high.

Mean size error for iteration 1: 47.7556381408357 Mean size error for iteration 2: 40.5957713606489 Mean size error for iteration 3: 34.4636034987029

I am aware increasing the number of iterations reduces the error.

africa_cont <- cartogram_cont(africa, 'Life_Expectancy_1950_1955 ', itermax = 3)

tm_shape(africa_cont) + tm_polygons('Life_Expectancy_1950_1955 ', style = "jenks") + tm_layout(frame = TRUE, legend.position = c("left","top"), legend.title.size = 1, legend.text.size = 0.6, legend.bg.color = "white", legend.outside=TRUE )+ tm_scale_bar()

image