Using facet_wraps in ggplot


#1

i am trying to plot individual longitudinal growth trajectories for height data. i have 876 individuals and would like the trajectory for each individual. if i use the code below the graphs are so tiny as to be of no use. is there way of increasing the size of the graphs so they become visible.

a snippet of the data is given below.

id_comb ht black female age
203 148.1 0 1 9.804444992
203 154.2 0 1 10.68057523
203 162.8 0 1 11.76752432
203 169.7 0 1 12.777812
203 169.6 0 1 13.76619643
203 170.6 0 1 14.691609
203 171.9 0 1 15.77582017
203 172.1 0 1 16.6848053
203 171.1 0 1 17.69783089
203 172.5 0 1 18.82858649
203 172.1 0 1 22.14966769
207 137.1 0 1 9.790755457
207 146.0 0 1 10.93520059
207 157.8 0 1 12.90923154
207 162.7 0 1 13.96058783
207 164.2 0 1 14.92159319
207 165.5 0 1 16.04139715
207 166.0 0 1 17.31999973
207 165.7 0 1 18.10304113
208 132.2 0 0 9.911223365
349 148.5 1 1 12.80792898
349 152.1 1 1 14.15224132
349 152.8 1 1 14.71351226
349 152.4 1 1 15.54309808
349 153.9 1 1 16.58350274
349 153.8 1 1 17.72247206
349 153.3 1 1 18.73002183

the code i have used is below.
library(ggplot2)
library(nlme)
library(haven)
females_black <- read_sas("C:/Users/rchiruka/Desktop/phd r code/females_black.sas7bdat",
NULL)

ggplot(data=females_black,aes(x=age,y=ht))+geom_line() +facet_wrap(~id_comb,nrow=176,ncol=5, scales="free")


#2

you can set the plot size in chunk option section


#3

Facets are meant to be used for small multiples, so you will need to make adjustments for a grid of 176x5.

If you save the output via ggsave(), you can experiment with the width, height and dpi parameters. You will probably also need to edit the font size.


#4

I'll start by saying your goal is not going to work well. Showing 876 groups of data simultaneously, with each meant to be examined individually, isn't feasible. You might need to focus more on what you're looking for to choose an appropriate visualization.

  1. If you're looking for "weird" trajectories, then write a test for weirdness. Plot only trajectories flagged as weird.

  2. If you have no idea what you're looking for, plot each trajectory individually, but use the same scales for all of them. Maybe save the plots with ggsave, and then browse the output files with your image viewer of choice. Hopefully, you'll find out what's "important" and go to option #1.

  3. If this is for a report which requires you to include trajectories for all individuals, then you could stick them all in the back as an appendix. If the report will be HTML and you're comfortable with JavaScript, you could include a <select> control which lets you see only a handful at once.


#5

I have decided to go the long way of breaking down the data into subsets and then drawing the tragectories for each subset


#6

As one option that doesn't require any knowledge of Javascript, I can recommend this package - trelliscopejs. It's quite good for exactly this purpose - visualizing small multiples in an interactive manner.


#7

For quick interactive viewing, another option would be to send your plot into plotly so you can scan over for outliers:

library(plotly)
plot <- ggplot(data=females_black,aes(x = age, y = ht, label = id_comb)+
  geom_line() 
ggplotly(plot)

For this many individuals, it makes sense to explore subsets. One approach you might consider would be to classify your individuals on some criteria (e.g. max ht, best/worst fit to a linear model, fastest growth, age range in data set, k-means groups, etc.). Then you could plot those trajectories of those groups against a background of the full data set. That can be a helpful approach for finding underlying structure across many individuals.