2

I work with a massive 4D nifti file (x - y - z - subject; MRI data) and due to the size I can't convert to a csv file and open in R. I would like to get a series of overlaying density plots (classic example here) one for each subject with the idea to just visualise that there is not much variance in density distributions across the sample.

I could however, extract summary statistics for each subject (mean, median, SD, range etc. of the variable of interest) and use these to create the density plots (at least for the variables that are normally distributed). Something like this would be fantastic but I am not sure how to do it for density plots.

Your help will be much appreciated.

user1442363
  • 800
  • 1
  • 10
  • 18
  • 2
    Maybe these packages are useful: [neurobase](https://cran.r-project.org/web/packages/neurobase/vignettes/neurobase.html), [oro.nifti](https://cran.r-project.org/web/packages/oro.nifti/oro.nifti.pdf) – zx8754 May 10 '18 at 12:18
  • I agree with zx8754, the oro.nifti package has been specifically designed for reading/writing/manipulating NIfTI files . – B. Whitcher Sep 08 '18 at 16:30

1 Answers1

1

So these really aren't density plots per se - they are plots of densties of normal distributions with given means and standard deviations.

That can be done in ggplot2, but you need to expand your table of subjects and summaries into grids of points and normal densities at those points.

Here's an example. First, make up some data, consisting of subject IDs and some simulated sample averages and sample standard deviations.

library(tidyverse)
set.seed(1)
foo <- data_frame(Subject = LETTERS[1:10], avg=runif(10, 10,20), stdev=runif(10,1,2)) 

Now, for each subject we need to obtain a suitable grid of "x" values along with the normal density (for that subject's avg and stdev) evaluated at those "x" values. I've chosen plus/minus 4 standard deviations. This can be done using do. But that produces a funny data frame with a column consisting of data frames. I use unnest to explode out the data frame.

bar <- foo %>% 
  group_by(Subject) %>% 
  do(densities=data_frame(x=seq(.$avg-4*.$stdev, .$avg+4*.$stdev, length.out = 50),
                          density=dnorm(x, .$avg, .$stdev))) %>% 
  unnest()

Have a look at bar to see what happened. Now we can use ggplot2 to put all these normal densities on the same plot. I'm guessing with lots of subjects you wouldn't want a legend for the plot.

bar %>% 
  ggplot(aes(x=x, y=density, color=Subject)) + 
  geom_line(show.legend = FALSE)

normal densities

ngm
  • 2,539
  • 8
  • 18