@@ -183,29 +183,34 @@ create.pgs.density.plot <- function(
183183 pgs.by.phenotype <- lapply(
184184 X = pgs.by.phenotype ,
185185 FUN = function (x ) {
186- x <- x [sapply(x , length ) > 1 ];
186+ large.categories <- sapply(
187+ X = x ,
188+ function (y ) {
189+ length(y [! is.na(y )]) > 1
190+ }
191+ );
192+ x <- x [large.categories ];
187193 }
188194 );
189195
190196 # iterate over phenotype variables
191197 for (phenotype in names(pgs.by.phenotype )) {
192198 pgs.data.for.plotting <- pgs.by.phenotype [[phenotype ]];
193199
194- # count non-NA values per phenotype category
195- n.samples.per.category <- sapply(pgs.data.for.plotting , function (x ) {length(x [! is.na(x )])});
196- if (any(n.samples.per.category < 5 )) {
197- # remove phenotype categories containing fewer than 5 samples
198- pgs.data.for.plotting <- pgs.data.for.plotting [n.samples.per.category > = 5 ];
200+ # handle case where all categories have fewer than 2 samples
201+ if (length(pgs.data.for.plotting ) == 0 ) {
199202 # issue a warning
200- warning(paste0(names(n.samples.per.category )[n.samples.per.category < 5 ], ' category has fewer than 5 samples, density curves will not be plotted' ));
203+ warning(paste0(' No ' , phenotype , ' categories with more than 2 samples, plotting aggregated density instead' ));
204+ pgs.density.by.phenotype.plots [[paste0(pgs.column ,' _' ,phenotype )]] <- pgs.density.plots [[pgs.column ]];
205+ next ;
201206 }
202207
203208 # color handling
204209 max.colors <- 12 ;
205210 max.lty <- 6 ;
206211 max.categories <- max.colors * max.lty ;
207212 if (length(pgs.data.for.plotting ) > max.categories ) {
208- # Issue a warning that plot is not bein color-coded
213+ # Issue a warning that plot is not being color-coded
209214 warning(paste0(' Skipping colors for ' , pgs.column , ' and ' , phenotype , ' due to too many categories' ));
210215 # plot all lines in black
211216 group.xaxis.formatting <- BoutrosLab.plotting.general :: auto.axis(
0 commit comments