<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/longitudinal-heat-plots/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://r-posts.com/longitudinal-heat-plots/"> R-posts.com</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>During our research on the effect of prednisone consumption during pregency on health outcomes of the baby (Palmsten K, Rolland M, Hebert MF, et al., Patterns of prednisone use during pregnancy in women with rheumatoid arthritis: Daily and cumulative dose. Pharmacoepidemiol Drug Saf. 2018 Apr;27(4):430-438. https://www.ncbi.nlm.nih.gov/pubmed/29488292) we developed a custom plot to visualize for each patient their daily and cumulative consumption of prednisone during pregenancy. Since the publication these plots have raised some interest so here is the code used to produce them.</p>
<p>Data needs to be in the following format:<br>
1 line per patient<br>
1 column for the patient ID (named id) and then 1 column per unit of time (here days) reporting the measure for that day<br>
To illustrate the type of data we dealt with I first generate a random dataset containing 25 patients followed for n days (n different for each patient, randomly chosen between 50 and 200) with a daily consumption value randomly selected between 0 and a maximum dose (randomly determined for each patient between 10 and 50 ).</p>
<p>Then we compute the cumulated consumption ie sum of all previous days.</p>
<p><pre>
# initial parameters for simulating data
n_indiv &lt;- 25
min_days &lt;- 50
max_days &lt;- 200
min_dose &lt;- 10
max_dose &lt;- 50

# list of ids
id_list &lt;- str_c(&quot;i&quot;, 1:n_indiv)

# intializing empty table
my_data &lt;- as.data.frame(matrix(NA, n_indiv, (max_days+1)))
colnames(my_data) &lt;- c(&quot;id&quot;, str_c(&quot;d&quot;, 1:max_days))
my_data$id &lt;- id_list

# daily simulated data
set.seed(113)
for(i in 1:nrow(my_data)){
# n days follow up
n_days &lt;- round(runif(1, min_days, max_days))
# maximum dose
dose &lt;- round(runif(1, min_dose, max_dose))
# random daily value
my_data[i,2:ncol(my_data)] &lt;- c(runif(n_days, 0, max_dose), rep(NA,(max_days-n_days)))
}

# cumulative simulated data
my_cum_data &lt;- my_data
for(i in 3:ncol(my_cum_data)){
my_cum_data[[i]] &lt;- my_cum_data[[i]] + my_cum_data[[i-1]] 
}
</pre>

<p>Our plots use the legend.col function found here: https://aurelienmadouasse.wordpress.com/2012/01/13/legend-for-a-continuous-color-scale-in-r/</p>
<p>Here is the longitudinal heat plot function:</p>
<p><pre>
 # Color legend on the top
long_heat_plot &lt;- function(my_data, cutoff, xmax) {
# my_data: longitudinal data with one line per individual, 1st column with id, and then one column per unit of time
# cutoff: cutoff value for color plot, all values above cutoff are same color
# xmax: x axis max value
n_lines &lt;- nrow(my_data)
line_count &lt;- 1
# color scale
COLS &lt;- rev(heat.colors(cutoff))
# plotting area
par(oma=c(1,1,4,1), mar=c(2, 2, 2, 4), xpd=TRUE)
# plot init
plot(1,1,xlim=c(0,xmax), ylim=c(1, n_lines), pch='.', ylab='Individual',xlab='Time unit',yaxt='n', cex.axis = 0.8)
# plot line for each woman one at a time
for (i in 1 : n_lines) { 
# get id
id1 &lt;- my_data$id[i]
# get trajectory data maxed at max_val
id_traj &lt;- my_data[my_data$id == id1, 2:ncol(my_data)]
# get last day
END &lt;- max(which(!is.na(id_traj)))
# plot dotted line
x1 &lt;- 1:xmax
y1 &lt;- rep(i,xmax)
lines(x1, y1, lty=3)
for (j in 1 : (ncol(my_data) - 1)) {
# trim traj to max val
val &lt;- min(id_traj[j], cutoff)
# plot traj
points(j, i, col=COLS[val], pch=20, cex=1)
}
# add limit line
points((END+1), i, pch=&quot;|&quot;, cex=0.9)
}
# add legend
legend.col(col = COLS, lev = 1:cutoff)
mtext(side=3, line = 3, 'unit of measurement')
opar &lt;- par()
text_size &lt;- opar$cex.main
}
</pre>

<p>Then we generate the corresponding plots:</p>
<p><pre> long_heat_plot(my_data, 50, 200) </pre>

<p><img data-orig-file="https://i2.wp.com/r-posts.com/wp-content/uploads/2018/07/longitudinal_plot.png?fit=551%2C470&amp;ssl=1" data-orig-size="551,470" data-medium-file="https://i2.wp.com/r-posts.com/wp-content/uploads/2018/07/longitudinal_plot.png?fit=352%2C300&amp;ssl=1" data-attachment-id="563" data-image-meta="{" data-image-description="" src="https://i2.wp.com/r-posts.com/wp-content/uploads/2018/07/longitudinal_plot.png?resize=352%2C300" data-permalink="https://r-posts.com/longitudinal-heat-plots/longitudinal_plot/" data-large-file="https://i2.wp.com/r-posts.com/wp-content/uploads/2018/07/longitudinal_plot.png?fit=450%2C384&amp;ssl=1" alt="" data-image-title="longitudinal_plot" sizes="(max-width: 352px) 100vw, 352px" width="352" data-comments-opened="1" class="wp-image-563" data-recalc-dims="1" height="300"> </p>
<p><pre> long_heat_plot(my_cum_data, 5000, 200) </pre>

<p><img data-orig-file="https://i0.wp.com/r-posts.com/wp-content/uploads/2018/07/longitudinal_cumulative_plot.png?fit=551%2C470&amp;ssl=1" data-orig-size="551,470" data-medium-file="https://i0.wp.com/r-posts.com/wp-content/uploads/2018/07/longitudinal_cumulative_plot.png?fit=352%2C300&amp;ssl=1" data-attachment-id="564" data-image-meta="{" data-image-description="" src="https://i0.wp.com/r-posts.com/wp-content/uploads/2018/07/longitudinal_cumulative_plot.png?resize=352%2C300" data-permalink="https://r-posts.com/longitudinal-heat-plots/longitudinal_cumulative_plot/" data-large-file="https://i0.wp.com/r-posts.com/wp-content/uploads/2018/07/longitudinal_cumulative_plot.png?fit=450%2C384&amp;ssl=1" alt="" data-image-title="longitudinal_cumulative_plot" sizes="(max-width: 352px) 100vw, 352px" width="352" data-comments-opened="1" class="wp-image-564" data-recalc-dims="1" height="300"></p>
<p>And here is what we did in our study:</p>
<p><img data-orig-file="https://i0.wp.com/r-posts.com/wp-content/uploads/2018/07/Capture.png?fit=521%2C667&amp;ssl=1" data-orig-size="521,667" data-medium-file="https://i0.wp.com/r-posts.com/wp-content/uploads/2018/07/Capture.png?fit=234%2C300&amp;ssl=1" data-attachment-id="565" data-image-meta="{" data-image-description="" src="https://i0.wp.com/r-posts.com/wp-content/uploads/2018/07/Capture.png?resize=234%2C300" data-permalink="https://r-posts.com/longitudinal-heat-plots/capture/" data-large-file="https://i0.wp.com/r-posts.com/wp-content/uploads/2018/07/Capture.png?fit=450%2C576&amp;ssl=1" alt="" data-image-title="Capture" sizes="(max-width: 234px) 100vw, 234px" width="234" data-comments-opened="1" class="wp-image-565" data-recalc-dims="1" height="300"></p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://r-posts.com/longitudinal-heat-plots/"> R-posts.com</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/Vc_mLgU-_RE" height="1"></a></div>