Cheyne-Stoke Respiration


One can create art with data, many have proven that with beautiful infographics or dazzling company reports. I thought using R to create an artistic rendering of a normally mundane process would be neat. I developed a graphic based on Cheyne-Stokes respiration using ggplot. Code is included at the end.

Cheyne-Stokes respiration graphic

One can create art with data, many have proven that with beautiful infographics or dazzling company reports. I thought using R to create an artistic rendering of a normally mundane process would be neat. I developed a graphic based on Cheyne-Stokes respiration using ggplot.

I think the resulting work (right) has a sci-fi feel to it (think Galactic Empire from Star Wars), almost like it could be a symbol for a corporation should the sinusoidal lobes be made solid. Below I have included the full code needed to replicate the graph, most of it is automatic after specifying a couple of parameters. This was the main advantage of coding the design over manually creating it, I could iterate quickly until I found a look that I agreed with sans wasting a lot of time doing fine adjustments.

I won't go over the code in detail as it is largely self-explanatory (though pardon an inefficient techniques, this was a five minute job completed during a meeting). Basically I use a mirror set of times centered around zero to create a smooth, enveloped sine wave (hyperpnoea) and include bouts of no breathing (aponea). The bars to signify hyperpnoea/aponea are just modified error bars and the entire image is made more salient by shifting the coordinates to polar.

Note: The code isn't fancy (made during a meeting), so pardon the lack of efficiency or beauty at parts. The black line is the breathing rate while the red and blue interlocking lines signify aponea and hyperpnoea, respectively.

R / S+
  1. # biafra ahanonu
  2. # updated: 2013.04.12
  3. # short script to make an artistic rendering of cheyne-stokes respiration
  4. #________________________________________________________
  5. # Load libraries and dependencies
  6. # For plotting
  7. library(ggplot2)
  8. # Add time-stamped footnote to graphs
  9. source("view.footnote.R")
  10. #________________________________________________________
  11. # amplitude of breathing, script auto-adjust parameters to this
  12. breathing.amplitude = 42;
  13. # set the frequency of the resulting curve
  14. heartFreq = 1+100/(1+.001*exp(breathing.amplitude))
  15. # make a forward and back hyperpnoea, to make an envelope sinusoid
  16. hyperpnoea.seq=seq(-breathing.amplitude,breathing.amplitude,.1);
  17. aponea.seq=rep(breathing.amplitude, .5*length(hyperpnoea.seq));
  18. # state number of cycles to repeat hyperpnoea and aponea
  19. cycles = 4;
  20. time=rep(c(aponea.seq,hyperpnoea.seq),cycles);
  21. # length of hyperpnoea/aponea cycle
  22. len.cycle = length(c(aponea.seq,hyperpnoea.seq));
  23. # add noise to the signal of desired
  24. noise = 0; #runif(length(time),1,breathing.amplitude^2);
  25. # make a dataframe of the value and times
  26. heart = data.frame(value=sin(heartFreq*time)*(-time^2+time+breathing.amplitude^2)+breathing.amplitude+noise,time=c(0:(length(time)-1)));
  27. # create data.frames containing location of hyperpnoea/aponea lines
  28. minC = seq(0,cycles-1,1)*len.cycle;
  29. maxC = seq(0,cycles-1,1)*len.cycle+length(aponea.seq);
  30. aponea = data.frame(min=minC,max=maxC,y=(breathing.amplitude^2)/2,x=1,id="aponea");
  31. hyperpnoea = data.frame(min=minC+length(aponea.seq),max=maxC+length(hyperpnoea.seq),y=(breathing.amplitude^2)/1.5,x=1,id="hyperpnoea");
  32. bardata = rbind(aponea,hyperpnoea);
  33. # set bar height
  34. bH = (breathing.amplitude^2)/4;
  35. # ggplot of heart respiration
  36. # I have not included by graphing wrapper function for simplicity'hyperpnoea.seq sake
  37. stokesPlot = ggplot(heart,aes(time,value))+
  38. geom_line()+
  39. # add hyperpnoea/aponea bars
  40. geom_errorbarh(mapping=aes(xmin=min,xmax=max,y=y,x=x,colour=id,height=bH),data=bardata, inherit.aes=FALSE)+
  41. ylab('depth of respiration')+
  42. xlab('time (hyperpnoea.seq)')+
  43. ggtitle('Cheyne–Stokes respiration')+
  44. # theme_set(theme_grey(base_size = 25))+
  45. theme(axis.text.x = element_text(angle=90,hjust=1,vjust=.5,color="white"),panel.background=element_rect(fill="white",colour="white"))+
  46. theme(panel.grid = element_line(linetype="2925"))+
  47. theme(axis.text = element_text(color="white"),axis.ticks = element_line(color="white"))+
  48. coord_polar()
  49. print(stokesPlot)
  50. # save a copy of the chart to the current directory
  51. png(paste("cheyne_stoke_art.png",sep=""),width=2400,height=1350,res=400,pointsize=1,antialias = "cleartype")
  52.         print(stokesPlot)
  53.         makeFootnote()

bahanonu [at]

additional articles to journey through:

facebook crawler and youtube api: part 1
07 october 2012 | programming

Created a small app, implemented in python and PHP, that crawls authenticated Facebook page for Youtube videos and adds them to a specified[...] user's Youtube playlist. Useful for groups that post a lot of youtube videos and want a centralized playlist to share with others. I'll go over some of the implementation details in this post.

passing command-line parameters to imagej
11 october 2013 | programming

The processes of passing command-line parameters to ImageJ plugins isn't the most direct. Illustrated is a simple solution to this problem,[...] full code included.

guild wars 2 chef excel guide
08 september 2012 | guild wars 2

The chef's ingredients are scattered throughout the Guild Wars 2 map, but the profession is a cheap way to get 10 levels quickly. Because I[...] am OCD and would rather not read text when I can browse a spreadsheet, compiled information from a couple of sources that is more easily searchable.

stanford bing concert hall: first impressions
15 december 2012 | stanford

Designed by architect Richard Olcott (Ennead Architects) and sound designer Dr. Yasuhisa T[...]oyota (Nagata Acoustics), the Bing Concert Hall is stunning. Robert Campbell (Fisher Dachs Associates) was on hand during the second sound check (along with Richard and Dr. Toyota) to discuss the philosophy behind the building, a bit of history, and where they hope it will be in the future. This post is my impressions of the place along with notes from their interview.

©2006-2018 | biafra ahanonu | updated 31 january 2018
biafra ahanonu