@@ -104,8 +104,6 @@ enrichment <- function(
104104 stop(" Feature names must be unique." )
105105 }
106106
107- # # This follows the original source:
108- # # features <- intersect(rownames(x), custom.set[[1]])
109107 features <- intersect(rownames(x_work ), custom.set [[1 ]])
110108
111109 if (length(features ) == 0L ) {
@@ -119,7 +117,7 @@ enrichment <- function(
119117 stop(" No valid groups were found in 'custom.set'." )
120118 }
121119
122- # # Restrict x to matched features, preserving the original intersect() order.
120+ # # Restrict x to matched features, preserving intersect() order.
123121 x_work <- x_work [features , , drop = FALSE ]
124122
125123 # # Build group-wise feature index list for the C++ core.
@@ -136,19 +134,13 @@ enrichment <- function(
136134 names(set_indices ) <- groups
137135
138136 # # --------------------------------------------------------------------------
139- # # RcppArmadillo replacement for the original heavy for-loop
137+ # # RcppArmadillo replacement for original heavy for-loop
140138 # #
141- # # This replaces the original:
142- # #
143- # # for (i in seq_len(ncol(x))) {
144- # # ...
145- # # }
146- # #
147- # # The C++ function returns matrices with:
139+ # # C++ returns matrices with:
148140 # # rows = columns/profiles of x_work
149141 # # columns = custom-set groups
150142 # #
151- # # This is the same orientation as the original S and pvalue before t().
143+ # # This matches the original S and pvalue orientation before t().
152144 # # --------------------------------------------------------------------------
153145
154146 core <- enrichment_core_original(
@@ -169,7 +161,7 @@ enrichment <- function(
169161 colnames(pvalue ) <- groups
170162
171163 # # --------------------------------------------------------------------------
172- # # Original post-processing and original plot format
164+ # # Original post-processing
173165 # # --------------------------------------------------------------------------
174166
175167 pvalue [is.na(S )] <- NA
@@ -181,7 +173,7 @@ enrichment <- function(
181173
182174 S <- t(S )
183175
184- # # define a dataframe
176+ # # Define plotting dataframe
185177 dat <- data.frame (
186178 x = factor (rep(colnames(S ), each = nrow(S ))),
187179 y = rep(rownames(S ), ncol(S )),
@@ -199,7 +191,7 @@ enrichment <- function(
199191 dat $ ks [dat $ ks < 0 & ! is.na(dat $ ks )] <- ks.min - 0.001
200192 dat $ ks <- dat $ ks - (ks.min - 0.001 )
201193
202- if (any (! is.na(dat $ ks ))) {
194+ if (sum (! is.na(dat $ ks )) > = 2L ) {
203195 dat $ ks [which.max(dat $ ks )] <- round(sort(dat $ ks , decreasing = TRUE )[2 ] + 0.5 )
204196 }
205197
@@ -214,6 +206,14 @@ enrichment <- function(
214206 ES.name <- " Enrichment\n Score"
215207 }
216208
209+ # # --------------------------------------------------------------------------
210+ # # Original plot format, with legend order:
211+ # #
212+ # # 1. significance legend, e.g. p<0.05
213+ # # 2. p-value colorbar
214+ # # 3. enrichment score size legend
215+ # # --------------------------------------------------------------------------
216+
217217 if (any(dat $ pvalue < pvalue.cutoff , na.rm = TRUE )) {
218218 g <- ggplot(data = dat ) +
219219 geom_point(
@@ -225,7 +225,11 @@ enrichment <- function(
225225 na.value = " black" ,
226226 colours = c(" blue" , " white" ),
227227 limits = c(0 , 1 ),
228- guide = guide_colorbar(barheight = 3 , barwidth = 1 )
228+ guide = guide_colorbar(
229+ barheight = 3 ,
230+ barwidth = 1 ,
231+ order = 2
232+ )
229233 ) +
230234 geom_point(
231235 aes(x = x , y = y , size = ks ),
@@ -234,14 +238,18 @@ enrichment <- function(
234238 ) +
235239 guides(
236240 colour = guide_legend(
237- override.aes = list (size = 5 )
241+ override.aes = list (size = 5 ),
242+ order = 1
238243 )
239244 ) +
240245 scale_size(
241246 name = ES.name ,
242247 range = c(1 , 5 ),
243248 breaks = c(0 , 1 , 2 , 3 ),
244- guide = guide_legend(keyheight = .8 )
249+ guide = guide_legend(
250+ keyheight = .8 ,
251+ order = 3
252+ )
245253 ) +
246254 theme(
247255 axis.text.x = element_text(
@@ -250,7 +258,8 @@ enrichment <- function(
250258 vjust = 1 ,
251259 hjust = 1
252260 ),
253- legend.margin = margin(- 0.1 , 0 , 0 , 0 , unit = " cm" )
261+ legend.margin = margin(- 0.1 , 0 , 0 , 0 , unit = " cm" ),
262+ legend.box = " vertical"
254263 ) +
255264 xlab(" " ) +
256265 ylab(" " )
@@ -260,22 +269,25 @@ enrichment <- function(
260269 scale_color_manual(
261270 name = NULL ,
262271 values = c(`red` = " red" ),
263- labels = " p<0.05"
272+ labels = " p<0.05" ,
273+ guide = guide_legend(order = 1 )
264274 )
265275 } else {
266276 if (pvalue.cutoff == 0.1 ) {
267277 g <- g +
268278 scale_color_manual(
269279 name = NULL ,
270280 values = c(`red` = " red" ),
271- labels = " p<0.1"
281+ labels = " p<0.1" ,
282+ guide = guide_legend(order = 1 )
272283 )
273284 } else {
274285 g <- g +
275286 scale_color_manual(
276287 name = NULL ,
277288 values = c(" gray" , " red" ),
278- labels = paste0(" p" , c(" >=" , " <" ), pvalue.cutoff )
289+ labels = paste0(" p" , c(" >=" , " <" ), pvalue.cutoff ),
290+ guide = guide_legend(order = 1 )
279291 )
280292 }
281293 }
@@ -290,21 +302,29 @@ enrichment <- function(
290302 na.value = " black" ,
291303 colours = c(" blue" , " white" ),
292304 limits = c(round(min(dat $ pvalue , na.rm = TRUE ), 2 ), 1 ),
293- guide = guide_colorbar(barheight = 3 , barwidth = 1 )
305+ guide = guide_colorbar(
306+ barheight = 3 ,
307+ barwidth = 1 ,
308+ order = 1
309+ )
294310 ) +
295311 scale_size(
296312 name = ES.name ,
297313 range = c(1 , 5 ),
298314 breaks = c(0 , 1 , 2 , 3 ),
299- guide = guide_legend(keyheight = .8 )
315+ guide = guide_legend(
316+ keyheight = .8 ,
317+ order = 2
318+ )
300319 ) +
301320 theme(
302321 axis.text.x = element_text(
303322 size = 8 ,
304323 angle = 45 ,
305324 vjust = 1 ,
306325 hjust = 1
307- )
326+ ),
327+ legend.box = " vertical"
308328 ) +
309329 xlab(" " ) +
310330 ylab(" " )
@@ -319,4 +339,4 @@ enrichment <- function(
319339 g = g
320340 )
321341 )
322- }
342+ }
0 commit comments