Composite plots: grid.arrange

I really like composite plots, where there’s a top part that describes a phenomenon and a bottom part with a synthetic time view of the overall process.
I’ve recently discovered this beautiful representation of educational differentials by gender, by Sara Lopus and Margaret Frye, and the beauty of this dataviz is that it tells a story on its own. (Click on the link for the publication)

I have used a random generated data to reproduce the graph in ggplot and used grid.arrange from gridExtra package to bind grobs, the top and bottom components.

grid.arrange(top, bottom, heights=c(10,5), widths=c(20), padding=0)

I have saved the map as a .png file png package and used rasterGrob from package grid to create a raster image graphical object.

Screen Shot 2018-08-30 at 11.26.50

Abandon the rainbows?

Are rainbow palettes really that bad?
If it depended on me, my ideal color palettes would be the color of my favorite toy as a kid: fluo squishy slime, the kind that lights up in the dark. However, not everyone appreciates it, especially scientific journals.
In the end, the presence of colors should be motivated by what’s being represented: a cluster of lines depicting the TFR trends in every country in the world during the past century would not benefit from a 250 colors scale. However, it’s different for maps and heat-maps as the right palette can improve the message, condensing and directing information (see this Lancet paper ).
Rainbow palettes work however rather well (in my opinion) in some instances although they can easily be substituted with less catchy and more printer friendly colors.

I’ll always be a fan of bright colors but I see the point for plotting minimalism. I’ve really enjoyed this article: End of the Rainbow? New Map Scale is More Readable by People Who Are Color Blind

I have downloaded the json file from here and transformed it into a dataframe using the rjson package.
I have used a bunch of color palettes to compare results. Ever since, discovering the viridis palettes, I am a huge fan of the ‘magma’ and ‘inferno’ as their darkest color is a deep black and it’s easier to highlight everything else.

 

Red-yellow-green palette:

Screen Shot 2018-08-14 at 09.48.02

Red-purple palette:

Screen Shot 2018-08-14 at 09.48.13

Rainbow palette:

Screen Shot 2018-08-14 at 09.48.21.png

Magma palette:

Screen Shot 2018-08-14 at 09.50.51.png
Inferno palette:

Screen Shot 2018-08-14 at 09.51.14
Plasma palette:

Screen Shot 2018-08-14 at 09.51.45
Viridis palette:

Screen Shot 2018-08-14 at 09.52.04.png

Cividis palette (from cividis library here):
Screen Shot 2018-08-14 at 09.52.45.png

Greys palette:

Screen Shot 2018-08-14 at 09.53.06

Inverted grey palette:
Screen Shot 2018-08-14 at 09.53.14

 

Here’s the code for the cividis palette plot:

ggplot(dt, aes(order, year))+ geom_tile(aes(fill = temp)) + scale_fill_cividis(na.value = "transparent")+ scale_y_reverse(name='', breaks = c(1876, 1900, 1950, 2000, 2018), labels=c('1876', '1900', '1950', '2000', '2018'))+ scale_x_continuous(name='', breaks = c(30-15, 61-15, 92-15, 122-15), labels=c('June', 'July', 'August', 'September'))+ theme(axis.ticks.x = element_blank())+ geom_vline(xintercept=c(30, 61, 92), linetype = "longdash" ) 

The Spatial side of Demography at EPC…

Come meet me at EPC to discuss Spatial Demography!

Thursday, 7 June 2018, 9:00-10:30 Session 1 Motherhood, Labor Market and Wages: Contextual Determinants of Childbearing in Spain: A Spatial Panel Study

Friday, 8 June 2018, 11:00-12:30 Session 65 Fertility Trends and ProspectsThe Geography of Fertility Rates in Low and Middle-Income Countries: Analysis of Cross-Sectional Surveys from 74 Countries

Lazy labeling: regular expressions

AKA how start getting around using grep, grepl, sub, gsub, regexp, gregexp.

Main difference between them is that while grep and grepl matches to pattern within the character vector, regexpr and gregexpr return more detail. sub and gsub perform replacement on the basis of a regular expression.

This is my version of lazy labeling: run the same code changing just the input dataset

file <- "BEN_Benin_2013" #myfile
clabel <- substring(file, 1, 3) #country label is the iso3 code
cname <-sub( ".*_(.*)_.*", "\\1", file) #country name
year <- sub( ".*_(.*)_*", "\\1", file) #census year

More to follow on grep and grepl…

See you at PAA 2018 in Denver!

As PAA 2018 is approaching, I’m looking forward to talk more about spatial prediction and subnational fertility disaggregation using DHS household survey data and our research on fine grid scale mapping of demographic indicators in low income countries at WorldPop (University of Southampton) and Flowminder.

Come meet me at the session Adolescent Transitions into Parenthood”, on Saturday 28th of April from 11:15am-12:45pm in Plaza Ballroom E (Sheraton Denver Downtown), I will present our paper The geography of fertility rates in low and middle-income countries: analysis of cross-sectional surveys from 69 countries”.

 

 

Kuwait population pyramids: changes between 2000-2020

Plot made in ggplot2 using geofacet, personal elaboration of Census data.

KWT_pyr2000-2020

Here is the the geofacet gris, although maybe not one of the most exciting ones…

mygrid <- data.frame(
row = c(1, 1, 1, 2, 2, 3),
col = c(1, 2, 3, 2, 3, 3),
code = c("KWT1", "KWT2", "KWT3", "KWT4", "KWT5", "KWT6"),
name = c("Al-Jahra Governorate", "Capital (Al Asimah) Governorate", "Hawalli Governorate", "Al-Farwaniya Governorate", "Mubarak Al-Kabeer Governorate", "al Ahmadi Governorate"),
stringsAsFactors = FALSE
)
geofacet::grid_preview(mygrid)

Bangladesh geofacet plot: Female population projections by upazilas

BGD_plot