Skip to content

Commit 76801df

Browse files
authored
Merge pull request #105 from ilia-kats/fix_factor_plot_colorscale
plot_factors: fix continuous color scale when plotting >2 factors
2 parents f95fb5a + 7360cf4 commit 76801df

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

R/plot_factors.R

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -420,14 +420,22 @@ plot_factors <- function(object, factors = c(1, 2), groups = "all",
420420
legend.text = element_text(size=rel(1.2)),
421421
legend.title = element_text(size=rel(1.2))
422422
)
423-
if (length(unique(df$color))>1 && isTRUE(legend)) { p <- p + labs(color=color_name) } else { p <- p + guides(color="none") + scale_color_manual(values="black") }
423+
424+
colorscale <- NULL
425+
if (length(unique(df$color))>1 && isTRUE(legend)) {
426+
p <- p + labs(color=color_name)
427+
} else {
428+
p <- p + guides(color="none")
429+
colorscale <- scale_color_manual(values="black")
430+
}
424431
if (length(unique(df$color))>1 && isFALSE(legend)) { p <- p + guides(color="none") }
425-
if (is.numeric(df$color)) p <- p + scale_color_gradientn(colors=colorRampPalette(rev(brewer.pal(n=5, name="RdYlBu")))(10))
432+
if (is.numeric(df$color)) colorscale <- scale_color_gradientn(colors=colorRampPalette(rev(brewer.pal(n=5, name="RdYlBu")))(10))
426433
if (length(unique(df$shape))>1) { p <- p + labs(shape=shape_name) } else { p <- p + guides(shape = "none") }
434+
if (!is.null(colorscale)) p <- p + colorscale
427435
if ((length(unique(df$color))>1 || length(unique(df$shape))>1) && isTRUE(legend)) { legend <- GGally::grab_legend(p) } else { legend <- NULL }
428436

429437
# Generate plot
430-
p <- GGally::ggpairs(df,
438+
p <- GGally::ggpairs(df,
431439
columns = factors,
432440
lower = list(continuous=GGally::wrap("points", size=dot_size)),
433441
diag = list(continuous='densityDiag'),
@@ -441,7 +449,8 @@ plot_factors <- function(object, factors = c(1, 2), groups = "all",
441449
axis.ticks = element_blank(),
442450
axis.text = element_blank()
443451
)
444-
452+
if (!is.null(colorscale)) p <- p + colorscale
453+
445454
return(p)
446455
}
447456

0 commit comments

Comments
 (0)