Skip to content

Commit dedbfb8

Browse files
authored
Merge pull request #78 from uclahs-cds/nzeltser-fix-densityplot-bug
fix densityplot bug
2 parents 17db16f + 6f0970b commit dedbfb8

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
## Added
99
* Added new contributor
10+
* Added minimum sample size check for grouped density curves
1011

1112
# ApplyPolygenicScore 3.0.2
1213

R/plot-pgs.R

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,20 +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

200+
# handle case where all categories have fewer than 2 samples
201+
if (length(pgs.data.for.plotting) == 0) {
202+
# issue a warning
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;
206+
}
207+
194208
# color handling
195209
max.colors <- 12;
196210
max.lty <- 6;
197211
max.categories <- max.colors * max.lty;
198212
if (length(pgs.data.for.plotting) > max.categories) {
199-
# Issue a warning that plot is not bein color-coded
213+
# Issue a warning that plot is not being color-coded
200214
warning(paste0('Skipping colors for ', pgs.column, ' and ', phenotype, ' due to too many categories'));
201215
# plot all lines in black
202216
group.xaxis.formatting <- BoutrosLab.plotting.general::auto.axis(

tests/testthat/test-plotting.R

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ test_that(
195195
)
196196
);
197197

198-
# check handling of only continuous phenotype (not supposed to be plotted, how can I test for multipanel dimensions over a multipanel object?)
198+
# check handling of only continuous phenotype (not supposed to be plotted)
199199
expect_no_error(
200200
create.pgs.density.plot(
201201
pgs.data = pgs.test,
@@ -223,6 +223,19 @@ test_that(
223223
file.exists(file.path(temp.dir, test.filename.continuous.phenotype))
224224
);
225225

226+
227+
# check handling of categorical phenotype with fewer than 2 samples in all categories
228+
low.n.test.data <- pgs.test;
229+
low.n.test.data$categorical.phenotype <- paste0('cat', 1:nrow(low.n.test.data));
230+
expect_warning(
231+
create.pgs.density.plot(
232+
pgs.data = low.n.test.data,
233+
phenotype.columns = c('categorical.phenotype'),
234+
output.dir = temp.dir,
235+
filename.prefix = 'TEST-all-small-categories'
236+
)
237+
);
238+
226239
}
227240
);
228241

0 commit comments

Comments
 (0)