R Logo@NBIS logo


In this lab, we will go step-by-step through points that are necessary to create some nice-looking plots.

Generating data

First, we will produce some random data that we will later plot. Make a data frame with

First, look at the defaults:

Generating plot step-by-step

As you see, the points are displayed in a simple way, axes are set automatically, the radius is not reflected on the plot in any way (3rd dimension).

Visualizing baby growth data on a WHO centile grid

A female child was measured at the following dates:

Your task is to plot these data on the WHO centile grids. Choose weight/length/circumference depending on the month you was born:

Good luck!

  1. use function dmy from the lubridate package to create a vector of timepoints.
    :key: Click to see how
         timepoints <- dmy(c('30-09-2015', '12-10-2015',
         '19-10-2015', '26-10-2015', '07-11-2015', '16-11-2015',
         '30-11-2015', '11-01-2016', '08-02-2016', '14-03-2016',
         '05-04-2016', '14-04-2016', '31-05-2016', '14-07-2016'))

  2. enter the measurement of choice as a vector
    :key: Click to see how
         weight <- c(3300, 3540, 3895, 4070, 4230, 4385, 4855, 5865, NA, 6736, 7065, 7080, 7530, 7640)
         length <- c(43,NA,53,54,55,56,58,62.5,65,67,67.5,67.5,70.5,71.5)
         head <- c(34,35.5,36.1,36.8,36.8,37.3,38,40.2,41.4,42.1,NA,43,44,45)

  3. WHO months is 30.4375 days long. Transform timepoints into OX coordinates so that the distance between them corresponds to the days between the two measurements. HINT: check as.duration and ddays functions. Do not feel bad if you have to click on the key. Working with dates is not an easy piece. The point is to know the lubridate exists…
    :key: Click to see how
         who.month <- 30.4375 #days
         xpoints <- as.duration(timepoints[1] %--% timepoints) / ddays(1) / who.month

  4. go to WHO website (http://www.who.int/childgrowth/standards/en/) and find out the link to the dataset of your concern, e.g. Weight for age, percentiles for girls have the following address: http://www.who.int/entity/childgrowth/standards/tab_wfa_girls_p_0_5.txt

  5. load the data using URL from the previous point and the read.table function.
    :key: Click to see how
         uri <- "http://www.who.int/entity/childgrowth/standards/tab_wfa_girls_p_0_5.txt"
         #uri <- "http://www.who.int/entity/childgrowth/standards/second_set/tab_hcfa_girls_p_0_5.txt"
         #uri <- "http://www.who.int/entity/childgrowth/standards/tab_lhfa_girls_p_0_2.txt"
         myData <-read.table(uri, header=T, sep='\t')

  6. create an empty plot to show your and WHO data,
    :key: Click to see how
         plot(1, xlim=c(0, max(myData$Month)), type='n', bty='n',
         ylim=c(0, max(myData[,c(5:19)])), las=1, xlab='Month', ylab='kg',

  7. plot WHO mean and percentiles: P25, P75, P0.1 and P99.9, use different colors and line types to make the plot pretty.
    :key: Click to see how
         lines(myData$M, col='grey', lty=1)
         lines(myData$P25, col='blue', lty=2)
         lines(myData$P75, col='blue', lty=2)
         lines(myData$P01, col='tomato', lty=2)
         lines(myData$P999, col='tomato', lty=2)

  8. plot your data on top of the percentiles, mind the units so that they match with the WHO ones,
    :key: Click to see how
         points(xpoints, weight/1000, pch=3, type='l', cex=.5)
         points(xpoints, weight/1000, pch=3, type='p', cex=.5)

  9. add some descriptions on the margins
    :key: Click to see how
         mtext(text = c('P0.1','P25','P75','P99.9'), side = 4,
         at=myData[dim(myData)[1], c('P01','P25','P75','P999')],
         las=1, cex=.5)

Visualizing Gapminder data

You task here is to use the already acquired R knowledge to plot an interesting relationship between two freely selected variables available at Hans Rosling’s Gapminder Foundation page.