I am new to R programming and wanted to know how to run in parallel plot
on 12 trellis objects made with lattice
package.
Basically, after a lot of pre-processing steps, I have the following commands:
plot(adhd_plot, split = c(1,1,4,3)) #plot adhd trellis object at 1,1 in a grid of 4 by 3 i.e 4 COLUMNS x 3 ROWS
plot(bpd_plot, split = c(2,1,4,3), newpage = F) #plot bpd trellis object in 2nd Column in a grid of 4colx3row
plot(bmi_plot, split = c(3,1,4,3), newpage = F)
plot(dbp_plot, split = c(4,1,4,3), newpage = F)
plot(height_plot, split = c(1,2,4,3), newpage = F)
plot(hdl_plot, split = c(2,2,4,3), newpage = F)
plot(ldl_plot, split = c(3,2,4,3), newpage = F)
plot(ra_plot, split = c(4,2,4,3), newpage = F)
plot(sbp_plot, split = c(1,3,4,3), newpage = F)
plot(scz_plot, split = c(2,3,4,3), newpage = F)
plot(tc_plot, split = c(3,3,4,3), newpage = F)
plot(tg_plot, split = c(4,3,4,3), newpage = F)
The issue is that while the above commands work, they take really long (>4hrs) on Mac OSX to produce a figure like the following :
Since my Mac has 8 cores, I thought I should try to split up the plot command across the different cores so as to speed up the plotting.
After searching across other parallelization questions, I found the doParallel
package and thought I could potentially implement the parLapply
function in it like as follows:
library(doParallel)
detectCores()
cl <- makeCluster(6) #6 out of 8 cores
registerdoParallel(cl)
parLapply(cl, list_of_all_trellis_objects, plot)
However, I am not sure how to use the split
parameter in the above parLapply
command to place the plots in different locations on the grid.
I necessarily need the 12 plots placed separately and not superimposed, so how to do that?
Thank you for going through my query and I look forward to your hints and solutions .
As suggested in comments, there is no way to write to plotting device in parallel.
Some workarounds to speed up drawing of individual plots:
Reduce the number of points in the QQ plot, see:
https://stats.stackexchange.com/questions/35220/removing-extraneous-points-near-the-centre-of-a-qq-plot
Load data faster by applying these tips:
http://cbio.ensmp.fr/~thocking/reading-large-text-files-into-R.html
You could try to draw/save multiple plots in parallel (where each plot uses methods from point 1 and 2), but writing to disk may cause significant bottleneck.
Edit:
Here is rough code to draw fast qq-plot:
https://github.com/vforget/fastqq
Code below: