Cheyne-Stoke Respiration

Summary

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()

-biafra
bahanonu [at] alum.mit.edu

other entires to explore:

r functions: ggplot barplot with errorbars
06 june 2013 | programming

The best way to create a barplot with errorbars using ggplot in R is not obvious. I'll show a super simple solution.[...]

filugori reboot
15 may 2012 | filugori

Several months ago I took a hard look at Filugori: The Long Tale, the story I started in grade school that was meant to be a mash-[...]up of my favorite books and fictional universes. However, it lacked a certain vision. The story was fun, frantic and fanciful, but there was no heart. It lacked cohesion and the universe did not appear to justify its own existence. Why should someone care to read this tale? What would they gain from it? While fleshing out the background of the universe, providing details on the four major epochs that define the story, I came to realize that I wanted to tell a very different tale than originally planned.

Jabez Redesign
19 December 2011 | designs

We have an internal website for Phi Sig. This was one of my redesign m[...]ockups.

satellite-based videos: eastern europe during the russia-ukraine conflict
30 november 2022 | satellite

To visualize the nighttime lights of Eastern Europe, with a focus on times before and after the ongoing Russia-Ukraine conflict, I updated [...]my geoSatView R code originally built to view forest fires in the west coast of the United States to use satellite data from VNP46A1 and other datasets collected from the Suomi NPP VIIRS satellite.

I then created higher-quality movies in MATLAB by using the VNP46A2 Black Marble dataset collected by the same satellite, which has reduced cloud and other artifacts due to additional data processing. This allowed me to quantitate a permanent reduction in nighttime lights within Ukraine (in line with my initial hypothesis) and identify a multi-stage reduction of nighttime lights in Kiev's outer neighborhoods/metropolitan area that was greater than that seen in the city core/center. This highlights the utility of public satellite data to quickly test hypotheses and visualize large-scale changes.

I will go over how the Black Marble dataset is collected and processed along with how I created the movies and the advantages/disadvantages of each data source.

Using this platform and codebase, in follow-up posts I will look at 2021 Texas power crisis during the winter storms, vegetation changes in deforested areas or after conservation efforts, and other events.

©2006-2024 | Site created & coded by Biafra Ahanonu | Updated 17 April 2024
biafra ahanonu