Browse code

fix pca_biplot

Gavin Rhys Lloyd authored on 19/02/2025 11:07:40
Showing 1 changed files
... ...
@@ -277,29 +277,10 @@ setMethod(f="chart_plot",
277 277
         P=output_value(dobj,'loadings')
278 278
         Ev=output_value(dobj,'eigenvalues')
279 279
         
280
-        # eigenvalues were square rooted when training PCA
281
-        Ev=Ev[,1]
282
-        Ev=Ev^2
283
-        
284
-        ## unscale the scores
285
-        #ev are the norms of scores
286
-        Ts=as.matrix(Ts) %*% diag(1/Ev) # these are normalised scores
287
-        
288
-        # scale scores and loadings by alpha
289
-        Ts=Ts %*% diag(Ev^(1-opt$scale_factor))
290
-        P=as.matrix(P) %*% diag(Ev^(opt$scale_factor))
291
-        
292
-        # additionally scale the loadings
293
-        sf=min(max(abs(Ts[,opt$components[1]]))/max(abs(P[,opt$components[1]])),
294
-            max(abs(Ts[,opt$components[2]]))/max(abs(P[,opt$components[2]])))
295
-        Ts=as.data.frame(Ts)
296
-        
297
-        rownames(Ts)=rownames(dobj$scores) # fix dimnames for SE object
298
-        colnames(Ts)=colnames(dobj$scores)
299
-        dobj$scores$data=as.data.frame(Ts) # nb object not returned, so only temporary scaling
280
+        sf = max(abs(Ts)) / max(abs(P)) * opt$scale_factor
300 281
         
301 282
         # plot
302
-        A=data.frame("x"=P[,opt$components[1]]*sf*0.8,"y"=P[,opt$components[2]]*sf*0.8)
283
+        A=data.frame("x"=P[,opt$components[1]]*sf,"y"=P[,opt$components[2]]*sf)
303 284
         C=pca_scores_plot(points_to_label=obj$points_to_label,xcol=obj$components[1],ycol=obj$components[2],factor_name=obj$factor_name)
304 285
         out=chart_plot(C,dobj)
305 286
         
Browse code

add max_pca param

allows reducing size of scree plot when PCA model has lots of components

Gavin Rhys Lloyd authored on 19/07/2023 10:39:16
Showing 1 changed files
... ...
@@ -472,8 +472,10 @@ setMethod(f="chart_plot",
472 472
 #' @include PCA_class.R
473 473
 #' @examples
474 474
 #' C = pca_scree_plot()
475
-pca_scree_plot = function(...) {
476
-    out=struct::new_struct('pca_scree_plot',...)
475
+pca_scree_plot = function(max_pc=15,...) {
476
+    out=struct::new_struct('pca_scree_plot',
477
+                           max_pc=max_pc,
478
+                           ...)
477 479
     return(out)
478 480
 }
479 481
 
... ...
@@ -481,11 +483,20 @@ pca_scree_plot = function(...) {
481 483
 .pca_scree_plot<-setClass(
482 484
     "pca_scree_plot",
483 485
     contains=c('chart'),
486
+    slots=c(max_pc='entity'),
484 487
     prototype = list(name='Scree plot',
485 488
         description=paste0('A plot of the percent variance and cumulative ',
486 489
             'percent variance for the components of a PCA model. '),
487 490
         type="line",
488
-        ontology="STATO:0000386"
491
+        ontology="STATO:0000386",
492
+        .params=c('max_pc'),
493
+        max_pc=entity(
494
+            name='Maximum number of components',
495
+            description='The maximum number of components to include in the plot.',
496
+            value=15,
497
+            type=c('numeric','integer'),
498
+            max_length=1
499
+        )
489 500
     )
490 501
 )
491 502
 
... ...
@@ -498,6 +509,10 @@ setMethod(f="chart_plot",
498 509
         ## percent variance
499 510
         scores=output_value(dobj,'scores')$data
500 511
         pvar=(colSums(scores*scores)/output_value(dobj,'ssx'))*100
512
+        
513
+        # only include up to max number of components
514
+        pvar=pvar[1:min(c(obj$max_pc,length(pvar)))]
515
+        
501 516
         A=data.frame("x"=1:length(pvar),"y"=c(pvar,cumsum(pvar)),"Variance"=as.factor(c(rep('Single component',length(pvar)),rep('Cumulative',length(pvar)))))
502 517
         labels=round(A$y,digits = 1)
503 518
         labels=format(labels,1)
Browse code

fix plot styles for PCA loadings

Gavin Rhys Lloyd authored on 23/01/2023 10:57:21
Showing 1 changed files
... ...
@@ -422,8 +422,17 @@ setMethod(f="chart_plot",
422 422
         # 2D plot
423 423
         if (length(opt$components)==2) {
424 424
             A=data.frame("x"=P[,opt$components[1]],"y"=P[,opt$components[2]])
425
-            out=ggplot(data=A,aes_(x=~x,y=~y)) +
426
-                geom_point() +
425
+            out=ggplot(data=A,aes_(x=~x,y=~y))
426
+            if (opt$style=='points'){
427
+                out=out+
428
+                    geom_point(data=A,inherit.aes = FALSE,color='black',mapping = aes_(x=~x,y=~y))
429
+            }
430
+            if (opt$style=='arrows'){
431
+                out=out+
432
+                    geom_segment(data=A,inherit.aes = FALSE,color='black',mapping = aes_(x=~0,y=~0,xend=~x,yend=~y),arrow=arrow(length=unit(8,'points')))
433
+                
434
+            }
435
+            out= out + 
427 436
                 ggtitle('PCA Loadings', subtitle=NULL) +
428 437
                 xlab(paste0('Component ',opt$components[1])) +
429 438
                 ylab(paste0('Component ',opt$components[2])) +
Browse code

fix loadings row.names

Gavin Rhys Lloyd authored on 23/01/2023 10:56:21
Showing 1 changed files
... ...
@@ -432,7 +432,7 @@ setMethod(f="chart_plot",
432 432
             
433 433
             if (!is.null(obj$label_features)) {
434 434
                 
435
-                if (obj$label_features=='rownames') {
435
+                if (obj$label_features=='row.names') {
436 436
                     vlabels=rownames(dobj$loadings)
437 437
                 } else {
438 438
                     vlabels=obj$label_features
Browse code

PLS updates (#64)

* add selectivity ratio

* replace vip_summary with feature_importance
renamed and now allows vip, sr and sr_pvalues to be plotted

* add equal_split model
random subsets so generate training sets with equal group numbers

* plot 1 - p-value
to conform with the "best" feature being a maximum value

* add resample iterator
subsample at random over a number of iterations. Option to use
different kinds of splitting methods. Corresponding chart.

* allow use of list() for factor_name

* force apply not to simplify output to guarantee returning a list

* update example

* add correct parameter
collect will collect the requested model output over all iterations in a list WORK IN PROGRESS

* add collection of multiple outputs of model sequence

* plot reg coeff on rhs

* match outputs of xval for use with grid search etc

* specify levels when converting predictions to factor

* change PLSDA to inherit from PLSR
rename some charts to be compatible with both PLSR and PLSDA

* allow y-block column selection

* re-assign y output after PLSR with factor

* update vignettes wrt PLS changes

* update documentation

* update R version to 4.1

* update documentation

* update documentation

* update scatter plot

- new scatter chart object
- used by PCA scores, PLSR/PLSDA scores
- other charts updated to reflect changes in scores plots where necessary
- added ycol param to plots for when y-block is a matrix

* add url to github

* add plsda scores alias

- plsda_scores_plot and pls_scores_plot do that same thing
Included for backwards compatability
- added components back as parameter for scores plots for backwards compatibility

* fix broken example

* fix broken tests

- scores is now returned as a DatasetExperiment object not a data.frame

* Update data_analysis_omics_using_the_structtoolbox.Rmd

- wrt changes in scores plots

* update documentation

* fix colnames for Y matrix

Gavin Rhys Lloyd authored on 28/02/2022 12:38:08 • GitHub committed on 28/02/2022 12:38:08
Showing 1 changed files
... ...
@@ -1,7 +1,7 @@
1 1
 #' @eval get_description('pca_correlation_plot')
2 2
 #' @import struct
3 3
 #' @export pca_correlation_plot
4
-#' @include PCA_class.R
4
+#' @include scatter_chart_class.R PCA_class.R
5 5
 #' @examples
6 6
 #' C = pca_correlation_plot()
7 7
 pca_correlation_plot = function(components=c(1,2),...) {
... ...
@@ -72,9 +72,9 @@ setMethod(f="chart_plot",
72 72
 #' M = model_apply(M,D)
73 73
 #' C = pca_scores_plot(factor_name = 'Species')
74 74
 #' chart_plot(C,M[2])
75
-#'
76 75
 pca_scores_plot = function(
77
-    components=c(1,2),
76
+    xcol='PC1',
77
+    ycol='PC2',
78 78
     points_to_label='none',
79 79
     factor_name,
80 80
     ellipse='all',
... ...
@@ -83,9 +83,17 @@ pca_scores_plot = function(
83 83
     label_filter=character(0),
84 84
     label_factor='rownames',
85 85
     label_size=3.88,
86
+    components=NULL,
86 87
     ...) {
88
+    
89
+    if (!is.null(components)){
90
+        xcol=components[1]
91
+        ycol=components[2]
92
+    }
93
+    
87 94
     out=struct::new_struct('pca_scores_plot',
88
-        components=components,
95
+        xcol=xcol,
96
+        ycol=ycol,
89 97
         points_to_label=points_to_label,
90 98
         factor_name=factor_name,
91 99
         ellipse=ellipse,
... ...
@@ -94,6 +102,7 @@ pca_scores_plot = function(
94 102
         label_size=label_size,
95 103
         ellipse_type=ellipse_type,
96 104
         ellipse_confidence=ellipse_confidence,
105
+        components = components,
97 106
         ...)
98 107
     return(out)
99 108
 }
... ...
@@ -101,97 +110,17 @@ pca_scores_plot = function(
101 110
 
102 111
 .pca_scores_plot<-setClass(
103 112
     "pca_scores_plot",
104
-    contains='chart',
105
-    slots=c(
106
-        # INPUTS
107
-        components='entity',
108
-        points_to_label='enum',
109
-        factor_name='entity',
110
-        ellipse='enum',
111
-        ellipse_type='enum',
112
-        ellipse_confidence='entity',
113
-        label_filter='entity',
114
-        label_factor='entity',
115
-        label_size='entity'
116
-    ),
117
-    
113
+    contains='scatter_chart',
114
+    slots=c(components='entity'),
118 115
     prototype = list(name='PCA scores plot',
119 116
         description='Plots a 2d scatter plot of the selected components',
120
-        type="scatter",
121
-        .params=c('components','points_to_label','factor_name','ellipse',
122
-            'label_filter','label_factor','label_size','ellipse_type',
123
-            'ellipse_confidence'),
124
-        
125
-        components=entity(name='Components to plot',
126
-            value=c(1,2),
127
-            type='numeric',
128
-            description=paste0('The components selected for plotting.'),
117
+        components = entity(name='Components to plot',
118
+            value=NULL,
119
+            type=c('numeric','integer','NULL'),
120
+            description='The principal components used to generate the plot. If provided this parameter overrides xcol and ycol params.',
129 121
             max_length=2
130 122
         ),
131
-        
132
-        points_to_label=enum(name='Points to label',
133
-            value='none',
134
-            type='character',
135
-            description=c(
136
-                'none' = 'No samples labels are displayed.', 
137
-                "all" = 'The labels for all samples are displayed.', 
138
-                "outliers" = 'Labels for for potential outlier samples are displayed.'
139
-            ),
140
-            allowed=c('none','all','outliers')
141
-        ),
142
-        factor_name=ents$factor_name,
143
-        ellipse=enum(
144
-            name = 'Plot ellipses',
145
-            description=c(
146
-                "all" = paste0('Ellipses are plotted for all groups and all samples.'),
147
-                "group" = 'Ellipses are plotted for all groups.',
148
-                "none" = 'Ellipses are not included on the plot.',
149
-                "sample" = 'An ellipse is plotted for all samples (ignoring group)'),
150
-            allowed=c('all','group','none','sample'),
151
-            value='all'
152
-        ),
153
-        
154
-        ellipse_type=enum(
155
-            name='Type of ellipse',
156
-            description=c(
157
-                'norm' = paste0('Multivariate normal (p = 0.95)'),
158
-                't' = paste0('Multivariate t (p = 0.95)')
159
-                ),
160
-            value='norm',
161
-            type='character',
162
-            max_length = 1,
163
-            allowed=c('norm','t')
164
-        ),
165
-        
166
-        ellipse_confidence=entity(
167
-            name='Ellipse confidence level',
168
-            description='The confidence level for plotting ellipses.',
169
-            value=0.95,
170
-            type='numeric',
171
-            max_length = 1
172
-        ),
173
-        
174
-        label_filter=entity(
175
-            name='Label filter',
176
-            value=character(0),
177
-            type='character',
178
-            description=paste0(
179
-                'Labels are only plotted for the named groups. If ',
180
-                'zero-length then all groups are included.'
181
-            )
182
-        ),
183
-        label_factor=entity(name='Factor for labels',
184
-            description=paste0('The column name of sample_meta to use for ',
185
-                'labelling samples on the plot. "rownames" will use the row ',
186
-                'names from sample_meta.'),
187
-            type='character',
188
-            value='rownames',
189
-            max_length=1),
190
-        label_size=entity(name='Text size of labels',
191
-            description='The text size of labels. Note this is not in Font Units.',
192
-            type='numeric',
193
-            value=3.88,
194
-            max_length=1)
123
+        .params='components'
195 124
     )
196 125
 )
197 126
 
... ...
@@ -207,131 +136,40 @@ setMethod(f="chart_plot",
207 136
     definition=function(obj,dobj)
208 137
     {
209 138
         
210
-        if (obj$points_to_label=='outliers' & !(obj$ellipse %in% c('all','sample'))) {
211
-            warning('Outliers are only labelled when plotting the sample ellipse')
139
+        # if provided convert index to names
140
+        if (is.numeric(obj$xcol)) {
141
+            obj$xcol=colnames(dobj$scores)[obj$xcol]
212 142
         }
213
-        opt=param_list(obj)
143
+        if (is.numeric(obj$ycol)) {
144
+            obj$ycol=colnames(dobj$scores)[obj$ycol]
145
+        }
146
+        
147
+        # percent variance
214 148
         scores=output_value(dobj,'scores')$data
215 149
         pvar = (colSums(scores*scores)/output_value(dobj,'ssx'))*100 # percent variance
216 150
         pvar = round(pvar,digits = 2) # round to 2 decimal places
217 151
         
218
-        if (length(obj$factor_name)==1) {
219
-            shapes = 19 # filled circles for all samples
220
-        } else {
221
-            shapes = factor(dobj$scores$sample_meta[[obj$factor_name[2]]])
222
-        }
223
-        
224
-        if (obj$label_factor=='rownames') {
225
-            slabels = rownames(dobj$scores$sample_meta)
226
-        } else {
227
-            slabels = dobj$scores$sample_meta[[obj$label_factor]]
228
-        }
229
-        opt$factor_name=opt$factor_name[[1]] # only use the first factor from now on
230
-        
231
-        x=scores[,opt$components[1]]
232
-        y=scores[,opt$components[2]]
233
-        xlabel=paste("PC",opt$components[[1]],' (',sprintf("%.1f",pvar[opt$components[[1]]]),'%)',sep='')
234
-        ylabel=paste("PC",opt$components[[2]],' (',sprintf("%.1f",pvar[opt$components[[2]]]),'%)',sep='')
235
-        
236
-        # get the factor from meta data
237
-        opt$groups=dobj$scores$sample_meta[[opt$factor_name]]
238
-        
239
-        # add a space to the front of the labels to offset them from the points, because nudge_x is in data units
240
-        for (i in 1:length(slabels))
241
-        {
242
-            slabels[i]=paste0('  ',slabels[i], '  ')
243
-        }
244
-        
245
-        # filter by label_filter list if provided
246
-        if (length(obj$label_filter)>0) {
247
-            out=!(as.character(opt$groups) %in% obj$label_filter)
248
-            slabels[out]=''
249
-        }
250
-        
251
-        if (is(opt$groups,'factor') | is(opt$groups,'character')) {
252
-            plotClass= structToolbox:::createClassAndColors(opt$groups)
253
-            opt$groups=plotClass$class
254
-        }
255
-        
256
-        # build the plot
257
-        A <- data.frame (group=opt$groups,x=x, y=y,slabels=slabels)
258
-        
259
-        out = ggplot()
152
+        # axis labels
153
+        pc_idx = which(colnames(dobj$scores)==obj$xcol)
154
+        pc_idy = which(colnames(dobj$scores)==obj$ycol)
260 155
         
261
-        # add invisible sample points for ellipse
262
-        out = out+geom_point(data=A,aes_string(x='x',y='y'),alpha=0,show.legend=FALSE)
156
+        xlabel=paste("PC",pc_idx,' (',sprintf("%.1f",pvar[pc_idx]),'%)',sep='')
157
+        ylabel=paste("PC",pc_idy,' (',sprintf("%.1f",pvar[pc_idy]),'%)',sep='')
263 158
         
159
+        # copy inputs to scatter chart
160
+        C = scatter_chart()
161
+        opt=param_list(obj)
162
+        opt$components=NULL # exclude as not in scatter chart
163
+        param_list(C) = opt
264 164
         
265
-        if (length(obj$factor_name)==2) {
266
-            out=out+geom_point(data=A, aes_(x=~x,y=~y,colour=~group,shape=~shapes))
267
-        }   else {
268
-            out=out+geom_point(data=A, aes_(x=~x,y=~y,colour=~group))
269
-        }
270
-        
271
-        
272
-        out=out+
273
-            
274
-            geom_point(na.rm=TRUE) +
275
-            xlab(xlabel) +
276
-            ylab(ylabel) +
277
-            ggtitle('PCA Scores', subtitle=NULL)
278
-        
279
-        if (length(obj$factor_name)==2) {
280
-            out=out+labs(shape=obj$factor_name[[2]],colour=obj$factor_name[[1]])
281
-        } else {
282
-            out=out+labs(shape=obj$factor_name[[1]])
283
-        }
284
-        
285
-        if (obj$ellipse %in% c('all','group')) {
286
-            out = out +stat_ellipse(data=A, aes_(x=~x,y=~y,colour=~group),type=obj$ellipse_type,
287
-                level=obj$ellipse_confidence) # ellipse for individual groups
288
-        }
289
-        
290
-        if (is(opt$groups,'factor')) { # if a factor then plot by group using the colours from pmp package
291
-            out=out+scale_colour_manual(values=plotClass$manual_colors,
292
-                name=opt$factor_name)
293
-        }else {# assume continuous and use the default colour gradient
294
-            out=out+scale_colour_viridis_c(limits=quantile(opt$groups,
295
-                c(0.05,0.95),na.rm = TRUE),oob=squish,name=opt$factor_name)
296
-        }
297
-        out=out+structToolbox:::theme_Publication(base_size = 12)
298
-        # add ellipse for all samples (ignoring group)
299
-        if (obj$ellipse %in% c('all','sample')) {
300
-            out=out+stat_ellipse(type=obj$ellipse_type,mapping=aes(x=x,y=y),
301
-                colour="#C0C0C0",linetype='dashed',data=A,
302
-                level=obj$ellipse_confidence)
303
-        }
304 165
         
305
-        if (obj$ellipse %in% c('all','sample')) { # only do this if we plotted the sample ellipse
306
-            # identify samples outside the ellipse
307
-            build=ggplot_build(out)$data
308
-            points=build[[1]]
309
-            ell=build[[length(build)]]
310
-            # outlier for DatasetExperiment ellipse
311
-            points$in.ell=as.logical(sp::point.in.polygon(points$x,points$y,ell$x,ell$y))
312
-            
313
-            # label outliers if
314
-            if (opt$points_to_label=='outliers')
315
-            {
316
-                if (!all(points$in.ell))
317
-                {
318
-                    temp=subset(points,!points$in.ell)
319
-                    temp$group=opt$groups[!points$in.ell]
320
-                    temp$label=slabels[!points$in.ell]
321
-                    out=out+geom_text(data=temp,aes_(x=~x,y=~y,label=~label,colour=~group),size=obj$label_size,vjust="inward",hjust="inward",show.legend=FALSE)
322
-                    
323
-                }
324
-            }
325
-            # add a list of outliers to the plot object
326
-            out$outliers=trimws(slabels[!points$in.ell])
327
-        }
166
+        # plot
167
+        g = chart_plot(C,dobj$scores)
328 168
         
329
-        # label all points if requested
330
-        if (opt$points_to_label=='all') {
331
-            out=out+geom_text(data=A,aes_string(x='x',y='y',colour='group',label='slabels'),vjust="inward",hjust="inward",show.legend=FALSE)
332
-        }
169
+        # update axis labels
170
+        g = g + xlab(xlabel) +ylab(ylabel) +ggtitle('PCA scores')
333 171
         
334
-        return(out)
172
+        return(g)
335 173
     }
336 174
 )
337 175
 
... ...
@@ -462,7 +300,7 @@ setMethod(f="chart_plot",
462 300
         
463 301
         # plot
464 302
         A=data.frame("x"=P[,opt$components[1]]*sf*0.8,"y"=P[,opt$components[2]]*sf*0.8)
465
-        C=pca_scores_plot(points_to_label=obj$points_to_label,components=obj$components,factor_name=obj$factor_name)
303
+        C=pca_scores_plot(points_to_label=obj$points_to_label,xcol=obj$components[1],ycol=obj$components[2],factor_name=obj$factor_name)
466 304
         out=chart_plot(C,dobj)
467 305
         
468 306
         if (opt$style=='points')
Browse code

fix plotting of group ellipses

Gavin Rhys Lloyd authored on 18/10/2021 15:54:26
Showing 1 changed files
... ...
@@ -283,7 +283,7 @@ setMethod(f="chart_plot",
283 283
         }
284 284
         
285 285
         if (obj$ellipse %in% c('all','group')) {
286
-            out = out +stat_ellipse(type=obj$ellipse_type,
286
+            out = out +stat_ellipse(data=A, aes_(x=~x,y=~y,colour=~group),type=obj$ellipse_type,
287 287
                 level=obj$ellipse_confidence) # ellipse for individual groups
288 288
         }
289 289
         
Browse code

fix #16

Gavin Rhys Lloyd authored on 01/10/2021 16:52:47
Showing 1 changed files
... ...
@@ -249,18 +249,26 @@ setMethod(f="chart_plot",
249 249
         }
250 250
         
251 251
         if (is(opt$groups,'factor') | is(opt$groups,'character')) {
252
-            plotClass= createClassAndColors(opt$groups)
252
+            plotClass= structToolbox:::createClassAndColors(opt$groups)
253 253
             opt$groups=plotClass$class
254 254
         }
255 255
         
256 256
         # build the plot
257
-        A <- data.frame (group=opt$groups,x=x, y=y)
257
+        A <- data.frame (group=opt$groups,x=x, y=y,slabels=slabels)
258
+        
259
+        out = ggplot()
260
+        
261
+        # add invisible sample points for ellipse
262
+        out = out+geom_point(data=A,aes_string(x='x',y='y'),alpha=0,show.legend=FALSE)
263
+        
258 264
         
259 265
         if (length(obj$factor_name)==2) {
260
-            out=ggplot (data=A, aes_(x=~x,y=~y,colour=~group,label=~slabels,shape=~shapes))
266
+            out=out+geom_point(data=A, aes_(x=~x,y=~y,colour=~group,shape=~shapes))
261 267
         }   else {
262
-            out=ggplot (data=A, aes_(x=~x,y=~y,colour=~group,label=~slabels))
268
+            out=out+geom_point(data=A, aes_(x=~x,y=~y,colour=~group))
263 269
         }
270
+        
271
+        
264 272
         out=out+
265 273
             
266 274
             geom_point(na.rm=TRUE) +
... ...
@@ -282,12 +290,11 @@ setMethod(f="chart_plot",
282 290
         if (is(opt$groups,'factor')) { # if a factor then plot by group using the colours from pmp package
283 291
             out=out+scale_colour_manual(values=plotClass$manual_colors,
284 292
                 name=opt$factor_name)
285
-        }
286
-        else {# assume continuous and use the default colour gradient
293
+        }else {# assume continuous and use the default colour gradient
287 294
             out=out+scale_colour_viridis_c(limits=quantile(opt$groups,
288 295
                 c(0.05,0.95),na.rm = TRUE),oob=squish,name=opt$factor_name)
289 296
         }
290
-        out=out+theme_Publication(base_size = 12)
297
+        out=out+structToolbox:::theme_Publication(base_size = 12)
291 298
         # add ellipse for all samples (ignoring group)
292 299
         if (obj$ellipse %in% c('all','sample')) {
293 300
             out=out+stat_ellipse(type=obj$ellipse_type,mapping=aes(x=x,y=y),
... ...
@@ -310,7 +317,8 @@ setMethod(f="chart_plot",
310 317
                 {
311 318
                     temp=subset(points,!points$in.ell)
312 319
                     temp$group=opt$groups[!points$in.ell]
313
-                    out=out+geom_text(data=temp,aes_(x=~x,y=~y,label=~label,colour=~group),size=obj$label_size,vjust="inward",hjust="inward")
320
+                    temp$label=slabels[!points$in.ell]
321
+                    out=out+geom_text(data=temp,aes_(x=~x,y=~y,label=~label,colour=~group),size=obj$label_size,vjust="inward",hjust="inward",show.legend=FALSE)
314 322
                     
315 323
                 }
316 324
             }
... ...
@@ -319,9 +327,8 @@ setMethod(f="chart_plot",
319 327
         }
320 328
         
321 329
         # label all points if requested
322
-        if (opt$points_to_label=='all')
323
-        {
324
-            out=out+geom_text(vjust="inward",hjust="inward")
330
+        if (opt$points_to_label=='all') {
331
+            out=out+geom_text(data=A,aes_string(x='x',y='y',colour='group',label='slabels'),vjust="inward",hjust="inward",show.legend=FALSE)
325 332
         }
326 333
         
327 334
         return(out)
Browse code

update to new ontology system (#63)

Gavin Rhys Lloyd authored on 26/07/2021 09:30:25 • GitHub committed on 26/07/2021 09:30:25
Showing 1 changed files
... ...
@@ -359,7 +359,7 @@ pca_biplot = function(
359 359
 
360 360
 .pca_biplot<-setClass(
361 361
     "pca_biplot",
362
-    contains=c('chart','stato'),
362
+    contains=c('chart'),
363 363
     slots=c(
364 364
         # INPUTS
365 365
         components='entity',
... ...
@@ -626,12 +626,12 @@ pca_scree_plot = function(...) {
626 626
 
627 627
 .pca_scree_plot<-setClass(
628 628
     "pca_scree_plot",
629
-    contains=c('chart','stato'),
629
+    contains=c('chart'),
630 630
     prototype = list(name='Scree plot',
631 631
         description=paste0('A plot of the percent variance and cumulative ',
632 632
             'percent variance for the components of a PCA model. '),
633 633
         type="line",
634
-        stato_id="STATO:0000386"
634
+        ontology="STATO:0000386"
635 635
     )
636 636
 )
637 637
 
... ...
@@ -681,7 +681,7 @@ pca_dstat_plot = function(number_components=2,alpha=0.05,...) {
681 681
 
682 682
 .pca_dstat_plot<-setClass(
683 683
     "pca_dstat_plot",
684
-    contains=c('chart','stato'),
684
+    contains=c('chart'),
685 685
     slots=c(number_components='entity',
686 686
         alpha='entity'),
687 687
     prototype = list(name='d-statistic plot',
... ...
@@ -690,7 +690,7 @@ pca_dstat_plot = function(number_components=2,alpha=0.05,...) {
690 690
             'considered to be outlying.'),
691 691
         type="bar",
692 692
         .params=c('number_components','alpha'),
693
-        stato_id='STATO:0000132',
693
+        ontology='STATO:0000132',
694 694
         number_components=entity(value = 2,
695 695
             name = 'Number of principal components',
696 696
             description = 'The number of principal components to use.',
Browse code

add ellipse plotting options

- allow norm or t distributed ellipses
- allow changing confidence level

Gavin Rhys Lloyd authored on 29/04/2021 13:48:42
Showing 1 changed files
... ...
@@ -78,6 +78,8 @@ pca_scores_plot = function(
78 78
     points_to_label='none',
79 79
     factor_name,
80 80
     ellipse='all',
81
+    ellipse_type='norm',
82
+    ellipse_confidence=0.95,
81 83
     label_filter=character(0),
82 84
     label_factor='rownames',
83 85
     label_size=3.88,
... ...
@@ -90,6 +92,8 @@ pca_scores_plot = function(
90 92
         label_filter=label_filter,
91 93
         label_factor=label_factor,
92 94
         label_size=label_size,
95
+        ellipse_type=ellipse_type,
96
+        ellipse_confidence=ellipse_confidence,
93 97
         ...)
94 98
     return(out)
95 99
 }
... ...
@@ -104,6 +108,8 @@ pca_scores_plot = function(
104 108
         points_to_label='enum',
105 109
         factor_name='entity',
106 110
         ellipse='enum',
111
+        ellipse_type='enum',
112
+        ellipse_confidence='entity',
107 113
         label_filter='entity',
108 114
         label_factor='entity',
109 115
         label_size='entity'
... ...
@@ -112,7 +118,9 @@ pca_scores_plot = function(
112 118
     prototype = list(name='PCA scores plot',
113 119
         description='Plots a 2d scatter plot of the selected components',
114 120
         type="scatter",
115
-        .params=c('components','points_to_label','factor_name','ellipse','label_filter','label_factor','label_size'),
121
+        .params=c('components','points_to_label','factor_name','ellipse',
122
+            'label_filter','label_factor','label_size','ellipse_type',
123
+            'ellipse_confidence'),
116 124
         
117 125
         components=entity(name='Components to plot',
118 126
             value=c(1,2),
... ...
@@ -135,13 +143,34 @@ pca_scores_plot = function(
135 143
         ellipse=enum(
136 144
             name = 'Plot ellipses',
137 145
             description=c(
138
-                "all" = paste0('Hotelling T2 95\\% ellipses are plotted for all groups and all samples.'),
139
-                "group" = 'Hotelling T2 95\\% ellipses are plotted for all groups.',
146
+                "all" = paste0('Ellipses are plotted for all groups and all samples.'),
147
+                "group" = 'Ellipses are plotted for all groups.',
140 148
                 "none" = 'Ellipses are not included on the plot.',
141
-                "sample" = 'A Hotelling T2 95\\% ellipse is plotted for all samples (ignoring group)'),
149
+                "sample" = 'An ellipse is plotted for all samples (ignoring group)'),
142 150
             allowed=c('all','group','none','sample'),
143 151
             value='all'
144 152
         ),
153
+        
154
+        ellipse_type=enum(
155
+            name='Type of ellipse',
156
+            description=c(
157
+                'norm' = paste0('Multivariate normal (p = 0.95)'),
158
+                't' = paste0('Multivariate t (p = 0.95)')
159
+                ),
160
+            value='norm',
161
+            type='character',
162
+            max_length = 1,
163
+            allowed=c('norm','t')
164
+        ),
165
+        
166
+        ellipse_confidence=entity(
167
+            name='Ellipse confidence level',
168
+            description='The confidence level for plotting ellipses.',
169
+            value=0.95,
170
+            type='numeric',
171
+            max_length = 1
172
+        ),
173
+        
145 174
         label_filter=entity(
146 175
             name='Label filter',
147 176
             value=character(0),
... ...
@@ -246,19 +275,24 @@ setMethod(f="chart_plot",
246 275
         }
247 276
         
248 277
         if (obj$ellipse %in% c('all','group')) {
249
-            out = out +stat_ellipse(type='norm') # ellipse for individual groups
278
+            out = out +stat_ellipse(type=obj$ellipse_type,
279
+                level=obj$ellipse_confidence) # ellipse for individual groups
250 280
         }
251 281
         
252 282
         if (is(opt$groups,'factor')) { # if a factor then plot by group using the colours from pmp package
253
-            out=out+scale_colour_manual(values=plotClass$manual_colors,name=opt$factor_name)
283
+            out=out+scale_colour_manual(values=plotClass$manual_colors,
284
+                name=opt$factor_name)
254 285
         }
255 286
         else {# assume continuous and use the default colour gradient
256
-            out=out+scale_colour_viridis_c(limits=quantile(opt$groups,c(0.05,0.95),na.rm = TRUE),oob=squish,name=opt$factor_name)
287
+            out=out+scale_colour_viridis_c(limits=quantile(opt$groups,
288
+                c(0.05,0.95),na.rm = TRUE),oob=squish,name=opt$factor_name)
257 289
         }
258 290
         out=out+theme_Publication(base_size = 12)
259 291
         # add ellipse for all samples (ignoring group)
260 292
         if (obj$ellipse %in% c('all','sample')) {
261
-            out=out+stat_ellipse(type='norm',mapping=aes(x=x,y=y),colour="#C0C0C0",linetype='dashed',data=A)
293
+            out=out+stat_ellipse(type=obj$ellipse_type,mapping=aes(x=x,y=y),
294
+                colour="#C0C0C0",linetype='dashed',data=A,
295
+                level=obj$ellipse_confidence)
262 296
         }
263 297
         
264 298
         if (obj$ellipse %in% c('all','sample')) { # only do this if we plotted the sample ellipse
Browse code

Release 3 12 candidate (#32)

* fix base=10 regardless of input (see #15)

class constructor was always setting base to 10 instead of the input value

* merge bug fix 1.01 into dev (#19)

* bug fix issue #7

Correctly re-order the sample_meta column for colouring samples in the dendrogram plot

* version bump

bug fix issue #7

* fix for https://github.com/computational-metabolomics/structToolbox/issues/18 (#20)

correctly reorder the factor labels so that the control group always ends up in the denominator for the fold change calculation.

* fix for https://github.com/computational-metabolomics/structToolbox/issues/18

fixed incorrect length check on matching class labels.

* Issue 17 ttest factor (#21)

* convert to factor if not one already

fix for issue #17

* update roxygen version

* fix for issue #9 (#22)

changed from lapply to vapply and used drop=FALSE to ensure compatibility with a single factor.

* allow user to set lambda (#24)

- lambda changed to input parameter. NULL = uses pmp optimisation
- model_predict now uses the set value of lambda, or lambda_opt if used.
- documentation updated

* Feature non parametric fold change (#26)

* add "median" method

based on DOI: 10.1080/00949650212140 can now calcuate fold changes equivalent to using medians and corresponding confidence intervals

* update documentation

* update median method

now correctly calculates ratio of medians

* use wilcox for paired median intervals

make use of wilcox.test to estimate intervals for the median when using median for paired samples

* Issue 23 filter by name (#27)

* fix for #23

moved all model_apply functionality to model_predict so that model_train and model_predict can be used as well as model_apply

* update documentation

* Update mean_of_medians.R (#29)

fix for #28
- correctly loop over all levels in the named factor

* Feature documentation 3 12 (#31)

* update documentation

Description and inputs now pulled from the object definitions for consistency.

* fix definition of label_features

allows NULL and description updated

* replace non ascii characters

* export mixed_effect object

* use correct object name to generate documentation

* export mixed_effect object

* remove non ascii characters

* update tests with new object name

* add import for capture.output

* add import for capture.output

* use pca_biplot in tests

chart was renamed

* add utils import

* update struct dependency version

* update documentation

* update news, version bump

Gavin Rhys Lloyd authored on 25/10/2020 08:50:13 • GitHub committed on 25/10/2020 08:50:13
Showing 1 changed files
... ...
@@ -1,11 +1,5 @@
1
-#' pca_correlation_plot class
2
-#'
3
-#' Plots the correlation between features and selected components.
4
-#'
1
+#' @eval get_description('pca_correlation_plot')
5 2
 #' @import struct
6
-#' @param components The principal components to plot (\code{numeric(2)})
7
-#' @param ... additional slots and values passed to struct_class
8
-#' @return struct object
9 3
 #' @export pca_correlation_plot
10 4
 #' @include PCA_class.R
11 5
 #' @examples
... ...
@@ -25,15 +19,18 @@ pca_correlation_plot = function(components=c(1,2),...) {
25 19
         # INPUTS
26 20
         components='entity'
27 21
     ),
28
-    prototype = list(name='Feature boxplot',
29
-        description='plots a boxplot of a chosen feature for each group of a DatasetExperiment.',
22
+    prototype = list(name='PCA correlation plot',
23
+        description=paste0(
24
+            'A plot of the correlation between the variables/features and ',
25
+            'the selected principal component scores. Features with high ',
26
+            'correlation are well represented by the selected component(s)'),
30 27
         type="boxlot",
31 28
         .params=c('components'),
32 29
         
33 30
         components=entity(name='Components to plot',
34 31
             value=c(1,2),
35 32
             type='numeric',
36
-            description='the components to be plotted e.g. c(1,2) plots component 1 on the x axis and component 2 on the y axis.',
33
+            description='The Principal Components used to generate the plot.',
37 34
             max_length=2
38 35
         )
39 36
         
... ...
@@ -65,25 +62,8 @@ setMethod(f="chart_plot",
65 62
 #################################################
66 63
 #################################################
67 64
 
68
-#' pca_scores_plot class
69
-#'
70
-#' 2d scatter plot of principal component scores.
71
-#'
65
+#' @eval get_description('pca_scores_plot')
72 66
 #' @import struct
73
-#' @param components The principal components to plot (\code{numeric(2)})
74
-#' @param points_to_label "none", "all", or "outliers" will be labelled on the plot.
75
-#' @param factor_name The sample_meta column name to use for colouring the points.
76
-#' You can provide up to two factors for this plot.
77
-#' @param ellipse "all" will plot all ellipses, "group" will only plot group ellipses,
78
-#' "none" will not plot any ellipses and "sample" will plot ellipse for all samples (ignoring group).
79
-#' @param label_filter Only include labels for samples in the group specified by label_filter.
80
-#' If zero length then all labels will be included.
81
-#' @param label_factor The sample_meta column to use for labelling the samples.
82
-#' If 'rownames' then the rownames will be used.
83
-#' @param label_size The text size of the labels.NB ggplot units, not font size units.
84
-#' Default 3.88.
85
-#' @param ... additional slots and values passed to struct_class
86
-#' @return struct object
87 67
 #' @export pca_scores_plot
88 68
 #' @include PCA_class.R
89 69
 #' @examples
... ...
@@ -137,42 +117,52 @@ pca_scores_plot = function(
137 117
         components=entity(name='Components to plot',
138 118
             value=c(1,2),
139 119
             type='numeric',
140
-            description='the components to be plotted e.g. c(1,2) plots component 1 on the x axis and component 2 on the y axis.',
120
+            description=paste0('The components selected for plotting.'),
141 121
             max_length=2
142 122
         ),
143 123
         
144
-        points_to_label=enum(name='points_to_label',
124
+        points_to_label=enum(name='Points to label',
145 125
             value='none',
146 126
             type='character',
147
-            description='("none"), "all", or "outliers" will be labelled on the plot.',
127
+            description=c(
128
+                'none' = 'No samples labels are displayed.', 
129
+                "all" = 'The labels for all samples are displayed.', 
130
+                "outliers" = 'Labels for for potential outlier samples are displayed.'
131
+            ),
148 132
             allowed=c('none','all','outliers')
149 133
         ),
150
-        factor_name=entity(name='Factor name',
151
-            value='factor',
152
-            type='character',
153
-            description='The column name of sample meta to use for plotting. A second column can be included to plot using symbols.',
154
-            max_length=2
155
-        ),
156
-        ellipse=enum(name = 'Plot ellipses',description=c(
157
-            '"all" will plot all ellipses',
158
-            '"group" will only plot group ellipses',
159
-            '"none" will not plot any ellipses',
160
-            '"sample" will plot ellipse for all samples (ignoring group)'),
134
+        factor_name=ents$factor_name,
135
+        ellipse=enum(
136
+            name = 'Plot ellipses',
137
+            description=c(
138
+                "all" = paste0('Hotelling T2 95\\% ellipses are plotted for all groups and all samples.'),
139
+                "group" = 'Hotelling T2 95\\% ellipses are plotted for all groups.',
140
+                "none" = 'Ellipses are not included on the plot.',
141
+                "sample" = 'A Hotelling T2 95\\% ellipse is plotted for all samples (ignoring group)'),
161 142
             allowed=c('all','group','none','sample'),
162
-            value='all'),
163
-        label_filter=entity(name='Label filter',
143
+            value='all'
144
+        ),
145
+        label_filter=entity(
146
+            name='Label filter',
164 147
             value=character(0),
165 148
             type='character',
166
-            description='Only include the param.group labels included in label_filter. If zero length then all labels will be included.'
149
+            description=paste0(
150
+                'Labels are only plotted for the named groups. If ',
151
+                'zero-length then all groups are included.'
152
+            )
167 153
         ),
168 154
         label_factor=entity(name='Factor for labels',
169
-            description='The column name of sample_meta to use as labels. "rownames" will use the row names from sample_meta.',
155
+            description=paste0('The column name of sample_meta to use for ',
156
+                'labelling samples on the plot. "rownames" will use the row ',
157
+                'names from sample_meta.'),
170 158
             type='character',
171
-            value='rownames'),
159
+            value='rownames',
160
+            max_length=1),
172 161
         label_size=entity(name='Text size of labels',
173
-            description='The text size of labels. Note this is not in Font Units. Default 3.88.',
162
+            description='The text size of labels. Note this is not in Font Units.',
174 163
             type='numeric',
175
-            value=3.88)
164
+            value=3.88,
165
+            max_length=1)
176 166
     )
177 167
 )
178 168
 
... ...
@@ -307,26 +297,13 @@ setMethod(f="chart_plot",
307 297
 #################################################################
308 298
 #################################################################
309 299
 
310
-#' pca_biplot_plot class
311
-#'
312
-#' 2d scatter plot of principal component scores overlaid with principal component loadings.
313
-#'
300
+#' @eval get_description('pca_biplot')
314 301
 #' @import struct
315
-#' @param components The principal components to plot (\code{numeric(2)})
316
-#' @param points_to_label "none", "all", or "outliers" will be labelled on the plot.
317
-#' @param factor_name The sample_meta column name to use for colouring the points.
318
-#' You can provide up to two factors for this plot.
319
-#' @param scale_factor Scaling factor to apply to loadings. Default = 0.95.
320
-#' @param style Plot style for loadings. Can be 'points' (default) or 'arrows'.
321
-#' @param label_features 'Include feature labels from this variable meta column. 
322
-#' Special keyword "rownames" will use the rownames of the variable_meta data.frame'
323
-#' @param ... additional slots and values passed to struct_class
324
-#' @return struct object
325
-#' @export pca_biplot_plot
302
+#' @export pca_biplot
326 303
 #' @include PCA_class.R
327 304
 #' @examples
328
-#' C = pca_biplot_plot(factor_name='Species')
329
-pca_biplot_plot = function(
305
+#' C = pca_biplot(factor_name='Species')
306
+pca_biplot = function(
330 307
     components=c(1,2),
331 308
     points_to_label='none',
332 309
     factor_name,
... ...
@@ -334,7 +311,7 @@ pca_biplot_plot = function(
334 311
     style='points',
335 312
     label_features=FALSE,
336 313
     ...) {
337
-    out=struct::new_struct('pca_biplot_plot',
314
+    out=struct::new_struct('pca_biplot',
338 315
         components=components,
339 316
         points_to_label=points_to_label,
340 317
         factor_name=factor_name,
... ...
@@ -346,9 +323,9 @@ pca_biplot_plot = function(
346 323
 }
347 324
 
348 325
 
349
-.pca_biplot_plot<-setClass(
350
-    "pca_biplot_plot",
351
-    contains='chart',
326
+.pca_biplot<-setClass(
327
+    "pca_biplot",
328
+    contains=c('chart','stato'),
352 329
     slots=c(
353 330
         # INPUTS
354 331
         components='entity',
... ...
@@ -358,43 +335,48 @@ pca_biplot_plot = function(
358 335
         style='enum',
359 336
         label_features='entity'
360 337
     ),
361
-    prototype = list(name='Feature boxplot',
362
-        description='plots a boxplot of a chosen feature for each group of a DatasetExperiment.',
338
+    prototype = list(name='PCA biplot',
339
+        description=paste0('A scatter plot of the selected principal ',
340
+            'component scores overlaid with the corresponding principal ',
341
+            'component loadings.'),
363 342
         type="boxlot",
364 343
         .params=c('components','points_to_label','factor_name','scale_factor','style','label_features'),
365 344
         
366 345
         components=entity(name='Components to plot',
367 346
             value=c(1,2),
368 347
             type='numeric',
369
-            description='the components to be plotted e.g. c(1,2) plots component 1 on the x axis and component 2 on the y axis.',
348
+            description='The principal components used to generate the plot.',
370 349
             max_length=2
371 350
         ),
372 351
         points_to_label=enum(name='points_to_label',
373 352
             value='none',
374 353
             type='character',
375
-            description='("none"), "all", or "outliers" will be labelled on the plot.',
354
+            description=c(
355
+                "none" = 'No samples are labelled on the plot.',
356
+                "all" = 'All samples are labelled on the plot.',
357
+                "outliers" = 'Potential outliers are labelled on the plot.'),
376 358
             allowed=c('none','all','outliers')
377 359
         ),
378
-        factor_name=entity(name='Factor name',
379
-            value='factor',
380
-            type='character',
381
-            description='The name of the factor to be displayed on the plot. Appears on axis and legend titles, for example. By default the column name of the meta data will be used where possible.'
382
-        ),
360
+        factor_name=ents$factor_name,
383 361
         scale_factor=entity(name='Loadings scale factor',
384 362
             value=0.95,
385 363
             type='numeric',
386
-            description='Scaling factor to apply to loadings. Default = 0.95.'
364
+            description='The scaling factor applied to the loadings.'
387 365
         ),
388 366
         style=enum(name='Plot style',
389 367
             value='points',
390 368
             type='character',
391
-            description='Named plot styles for the biplot. [points], arrows',
369
+            description=c(
370
+                "points" = 'Loadings and scores are plotted as a scatter plot.',
371
+                'arrows' = 'The loadings are plotted as arrow vectors.'),
392 372
             allowed=c('points','arrows')
393 373
         ),
394 374
         label_features=entity(name='Add feature labels',
395 375
             value=FALSE,
396 376
             type='logical',
397
-            description='Include feature labels on the plot'
377
+            description=c(
378
+                "TRUE" = 'Features are labelled.',
379
+                "FALSE" = "Features are not labelled.")
398 380
         )
399 381
     )
400 382
     
... ...
@@ -403,7 +385,7 @@ pca_biplot_plot = function(
403 385
 #' @export
404 386
 #' @template chart_plot
405 387
 setMethod(f="chart_plot",
406
-    signature=c("pca_biplot_plot",'PCA'),
388
+    signature=c("pca_biplot",'PCA'),
407 389
     definition=function(obj,dobj)
408 390
     {
409 391
         opt=param_list(obj)
... ...
@@ -480,17 +462,8 @@ setMethod(f="chart_plot",
480 462
 ##################################################################
481 463
 ##################################################################
482 464
 
483
-#' pca_loadings_plot class
484
-#'
485
-#' 2d scatter plot of princpal component loadings.
486
-#'
465
+#' @eval get_description('pca_loadings_plot')
487 466
 #' @import struct
488
-#' @param components The principal components to plot (\code{numeric(2)})
489
-#' @param style Plot style for loadings. Can be 'points' (default) or 'arrows'.
490
-#' @param label_features 'A list of labels to use, one for each feature. Special 
491
-#' keyword "rownames" will use the rownames of the variable_meta data.frame'
492
-#' @param ... additional slots and values passed to struct_class
493
-#' @return struct object
494 467
 #' @export pca_loadings_plot
495 468
 #' @include PCA_class.R
496 469
 #' @examples
... ...
@@ -514,27 +487,34 @@ pca_loadings_plot = function(components=c(1,2),style='points',label_features=NUL
514 487
         style='enum',
515 488
         label_features='entity'
516 489
     ),
517
-    prototype = list(name='Feature boxplot',
518
-        description='plots a boxplot of a chosen feature for each group of a DatasetExperiment.',
490
+    prototype = list(name='PCA loadings plot',
491
+        description=paste0('A barchart (one component) or scatter plot ',
492
+            '(two components) of the selected principal component loadings.'),
519 493
         type="boxlot",
520 494
         .params=c('components','style','label_features'),
521 495
         
522 496
         components=entity(name='Components to plot',
523 497
             value=c(1,2),
524 498
             type='numeric',
525
-            description='the components to be plotted e.g. c(1,2) plots component 1 on the x axis and component 2 on the y axis.',
499
+            description='The principal components used to generate the plot.',
526 500
             max_length=2
527 501
         ),
528 502
         style=enum(name='Plot style',
529 503
             value='points',
530 504
             type='character',
531
-            description='Named plot styles for the biplot. [points], arrows',
505
+            description=c(
506
+                "points" = 'Loadings and scores are plotted as a scatter plot.',
507
+                'arrows' = 'The loadings are plotted as arrow vectors.'),
532 508
             allowed=c('points','arrows')
533 509
         ),
534
-        label_features=entity(name='Label features',
510
+        label_features=entity(name='Feature labels',
535 511
             value=NULL,
536
-            type=c('NULL','character'),
537
-            description='Include feature labels from this variable meta column. Special keyword "rownames" will use the rownames of the variable_meta data.frame'
512
+            type=c('character',"NULL"),
513
+            description=c(
514
+                'character()'='A vector of labels for the features',
515
+                'NULL' = 'No labels',
516
+                'row.names' = 'Labels will be extracted from the column names of the data matrix.'
517
+            )
538 518
         )
539 519
     )
540 520
     
... ...
@@ -597,12 +577,8 @@ setMethod(f="chart_plot",
597 577
 )
598 578
 
599 579
 
600
-#' pca_scree_plot_plot class
601
-#'
602
-#' Line plot showing percent variance and cumulative percent variance for the computed components.
603
-#'
580
+#' @eval get_description('pca_scree_plot')
604 581
 #' @import struct
605
-#' @param ... additional slots and values passed to struct_class
606 582
 #' @return struct object
607 583
 #' @export pca_scree_plot
608 584
 #' @include PCA_class.R
... ...
@@ -616,10 +592,12 @@ pca_scree_plot = function(...) {
616 592
 
617 593
 .pca_scree_plot<-setClass(
618 594
     "pca_scree_plot",
619
-    contains=c('chart'),
595
+    contains=c('chart','stato'),
620 596
     prototype = list(name='Scree plot',
621
-        description='plots the percent and cumulative percent variance for the calculated components',
622
-        type="line"
597
+        description=paste0('A plot of the percent variance and cumulative ',
598
+            'percent variance for the components of a PCA model. '),
599
+        type="line",
600
+        stato_id="STATO:0000386"
623 601
     )
624 602
 )
625 603
 
... ...
@@ -652,16 +630,8 @@ setMethod(f="chart_plot",
652 630
     }
653 631
 )
654 632
 
655
-#' pca_dstat_plot_plot class
656
-#'
657
-#' Bar chart showing mahalanobis distance from the mean in PCA scores space. A threshold is
658
-#' plotted at a chosen confidence as an indicator for rejecting outliers.
659
-#'
633
+#' @eval get_description('pca_dstat_plot')
660 634
 #' @import struct
661
-#' @param number_components The number of components to use.
662
-#' @param alpha The confidence level to plot.
663
-#' @param ... additional slots and values passed to struct_class
664
-#' @return struct object
665 635
 #' @export pca_dstat_plot
666 636
 #' @include PCA_class.R
667 637
 #' @examples
... ...
@@ -677,21 +647,23 @@ pca_dstat_plot = function(number_components=2,alpha=0.05,...) {
677 647
 
678 648
 .pca_dstat_plot<-setClass(
679 649
     "pca_dstat_plot",
680
-    contains=c('chart'),
650
+    contains=c('chart','stato'),
681 651
     slots=c(number_components='entity',
682 652
         alpha='entity'),
683 653
     prototype = list(name='d-statistic plot',
684
-        description='a bar chart of the d-statistics for samples in the input PCA model',
654
+        description=paste0('A bar chart of the d-statistics for samples in ',
655
+            'the input PCA model. Samples above the indicated threshold are ',
656
+            'considered to be outlying.'),
685 657
         type="bar",
686 658
         .params=c('number_components','alpha'),
687
-        
659
+        stato_id='STATO:0000132',
688 660
         number_components=entity(value = 2,
689
-            name = 'number of principal components',
690
-            description = 'number of principal components to use for the plot',
661
+            name = 'Number of principal components',
662
+            description = 'The number of principal components to use.',
691 663
             type='numeric'),
692 664
         alpha=entity(value=0.95,
693
-            name='threshold for rejecting outliers',
694
-            description='a confidence threshold for rejecting samples based on the d-statistic',
665
+            name='Outlier threshold',
666
+            description='A confidence threshold for rejecting samples based on the d-statistic',
695 667
             type='numeric')
696 668
     )
697 669
 )
Browse code

update documentation

Gavin Rhys Lloyd authored on 22/04/2020 20:10:12
Showing 1 changed files
... ...
@@ -318,7 +318,8 @@ setMethod(f="chart_plot",
318 318
 #' You can provide up to two factors for this plot.
319 319
 #' @param scale_factor Scaling factor to apply to loadings. Default = 0.95.
320 320
 #' @param style Plot style for loadings. Can be 'points' (default) or 'arrows'.
321
-#' @param label_features TRUE or FALSE to label features on the plot. Default is FALSE.
321
+#' @param label_features 'Include feature labels from this variable meta column. 
322
+#' Special keyword "rownames" will use the rownames of the variable_meta data.frame'
322 323
 #' @param ... additional slots and values passed to struct_class
323 324
 #' @return struct object
324 325
 #' @export pca_biplot_plot
... ...
@@ -486,7 +487,8 @@ setMethod(f="chart_plot",
486 487
 #' @import struct
487 488
 #' @param components The principal components to plot (\code{numeric(2)})
488 489
 #' @param style Plot style for loadings. Can be 'points' (default) or 'arrows'.
489
-#' @param label_features TRUE or FALSE to label features on the plot. Default is FALSE.
490
+#' @param label_features 'A list of labels to use, one for each feature. Special 
491
+#' keyword "rownames" will use the rownames of the variable_meta data.frame'
490 492
 #' @param ... additional slots and values passed to struct_class
491 493
 #' @return struct object
492 494
 #' @export pca_loadings_plot
Browse code

update documentation

Gavin Rhys Lloyd authored on 23/03/2020 13:53:17
Showing 1 changed files
... ...
@@ -181,7 +181,6 @@ pca_scores_plot = function(
181 181
 #' @importFrom sp point.in.polygon
182 182
 #' @import ggplot2
183 183
 #' @importFrom scales squish
184
-#' @param ... additional slots and values passed to struct_class
185 184
 #' @export
186 185
 #' @template chart_plot
187 186
 setMethod(f="chart_plot",
Browse code

allow custom labels for features in loadings plot

Gavin Rhys Lloyd authored on 06/03/2020 12:01:47
Showing 1 changed files
... ...
@@ -29,14 +29,14 @@ pca_correlation_plot = function(components=c(1,2),...) {
29 29
         description='plots a boxplot of a chosen feature for each group of a DatasetExperiment.',
30 30
         type="boxlot",
31 31
         .params=c('components'),
32
-
32
+        
33 33
         components=entity(name='Components to plot',
34 34
             value=c(1,2),
35 35
             type='numeric',
36 36
             description='the components to be plotted e.g. c(1,2) plots component 1 on the x axis and component 2 on the y axis.',
37 37
             max_length=2
38 38
         )
39
-
39
+        
40 40
     )
41 41
 )
42 42
 
... ...
@@ -49,15 +49,15 @@ setMethod(f="chart_plot",
49 49
         opt=param_list(obj)
50 50
         A=data.frame(x=output_value(dobj,'correlation')[,opt$components[1]],y=output_value(dobj,'correlation')[,opt$components[2]])
51 51
         dat <- circleFun(c(0,0),2,npoints = 50)
52
-
52
+        
53 53
         out=ggplot(data=A,aes_(x=~x,y=~y)) +
54 54
             geom_point() +
55 55
             scale_colour_Publication() +
56 56
             theme_Publication(base_size = 12)+
57 57
             coord_fixed(xlim = c(-1,1),ylim=c(-1,1)) +
58
-
58
+            
59 59
             geom_path(data=dat,aes_(x=~x,y=~y),inherit.aes = FALSE)
60
-
60
+        
61 61
         return(out)
62 62
     }
63 63
 )
... ...
@@ -128,19 +128,19 @@ pca_scores_plot = function(
128 128
         label_factor='entity',
129 129
         label_size='entity'
130 130
     ),
131
-
131
+    
132 132
     prototype = list(name='PCA scores plot',
133 133
         description='Plots a 2d scatter plot of the selected components',
134 134
         type="scatter",
135 135
         .params=c('components','points_to_label','factor_name','ellipse','label_filter','label_factor','label_size'),
136
-
136
+        
137 137
         components=entity(name='Components to plot',
138 138
             value=c(1,2),
139 139
             type='numeric',
140 140
             description='the components to be plotted e.g. c(1,2) plots component 1 on the x axis and component 2 on the y axis.',
141 141
             max_length=2
142 142
         ),
143
-
143
+        
144 144
         points_to_label=enum(name='points_to_label',
145 145
             value='none',
146 146
             type='character',
... ...
@@ -188,7 +188,7 @@ setMethod(f="chart_plot",
188 188
     signature=c("pca_scores_plot",'PCA'),
189 189
     definition=function(obj,dobj)
190 190
     {
191
-
191
+        
192 192
         if (obj$points_to_label=='outliers' & !(obj$ellipse %in% c('all','sample'))) {
193 193
             warning('Outliers are only labelled when plotting the sample ellipse')
194 194
         }
... ...
@@ -196,70 +196,70 @@ setMethod(f="chart_plot",
196 196
         scores=output_value(dobj,'scores')$data
197 197
         pvar = (colSums(scores*scores)/output_value(dobj,'ssx'))*100 # percent variance
198 198
         pvar = round(pvar,digits = 2) # round to 2 decimal places
199
-
199
+        
200 200
         if (length(obj$factor_name)==1) {
201 201
             shapes = 19 # filled circles for all samples
202 202
         } else {
203 203
             shapes = factor(dobj$scores$sample_meta[[obj$factor_name[2]]])
204 204
         }
205
-
205
+        
206 206
         if (obj$label_factor=='rownames') {
207 207
             slabels = rownames(dobj$scores$sample_meta)
208 208
         } else {
209 209
             slabels = dobj$scores$sample_meta[[obj$label_factor]]
210 210
         }
211 211
         opt$factor_name=opt$factor_name[[1]] # only use the first factor from now on
212
-
212
+        
213 213
         x=scores[,opt$components[1]]
214 214
         y=scores[,opt$components[2]]
215 215
         xlabel=paste("PC",opt$components[[1]],' (',sprintf("%.1f",pvar[opt$components[[1]]]),'%)',sep='')
216 216
         ylabel=paste("PC",opt$components[[2]],' (',sprintf("%.1f",pvar[opt$components[[2]]]),'%)',sep='')
217
-
217
+        
218 218
         # get the factor from meta data
219 219
         opt$groups=dobj$scores$sample_meta[[opt$factor_name]]
220
-
220
+        
221 221
         # add a space to the front of the labels to offset them from the points, because nudge_x is in data units
222 222
         for (i in 1:length(slabels))
223 223
         {
224 224
             slabels[i]=paste0('  ',slabels[i], '  ')
225 225
         }
226
-
226
+        
227 227
         # filter by label_filter list if provided
228 228
         if (length(obj$label_filter)>0) {
229 229
             out=!(as.character(opt$groups) %in% obj$label_filter)
230 230
             slabels[out]=''
231 231
         }
232
-
232
+        
233 233
         if (is(opt$groups,'factor') | is(opt$groups,'character')) {
234 234
             plotClass= createClassAndColors(opt$groups)
235 235
             opt$groups=plotClass$class
236 236
         }
237
-
237
+        
238 238
         # build the plot
239 239
         A <- data.frame (group=opt$groups,x=x, y=y)
240
-
240
+        
241 241
         if (length(obj$factor_name)==2) {
242 242
             out=ggplot (data=A, aes_(x=~x,y=~y,colour=~group,label=~slabels,shape=~shapes))
243 243
         }   else {
244 244
             out=ggplot (data=A, aes_(x=~x,y=~y,colour=~group,label=~slabels))
245 245
         }
246 246
         out=out+
247
-
247
+            
248 248
             geom_point(na.rm=TRUE) +
249 249
             xlab(xlabel) +
250 250
             ylab(ylabel) +
251 251
             ggtitle('PCA Scores', subtitle=NULL)
252
-
252
+        
253 253
         if (length(obj$factor_name)==2) {
254 254
             out=out+labs(shape=obj$factor_name[[2]],colour=obj$factor_name[[1]])
255 255
         } else {
256 256
             out=out+labs(shape=obj$factor_name[[1]])
257 257
         }
258
-
258
+        
259 259
         if (obj$ellipse %in% c('all','group')) {
260 260
             out = out +stat_ellipse(type='norm') # ellipse for individual groups
261 261
         }
262
-
262
+        
263 263
         if (is(opt$groups,'factor')) { # if a factor then plot by group using the colours from pmp package
264 264
             out=out+scale_colour_manual(values=plotClass$manual_colors,name=opt$factor_name)
265 265
         }
... ...
@@ -271,7 +271,7 @@ setMethod(f="chart_plot",
271 271
         if (obj$ellipse %in% c('all','sample')) {
272 272
             out=out+stat_ellipse(type='norm',mapping=aes(x=x,y=y),colour="#C0C0C0",linetype='dashed',data=A)
273 273
         }
274
-
274
+        
275 275
         if (obj$ellipse %in% c('all','sample')) { # only do this if we plotted the sample ellipse
276 276
             # identify samples outside the ellipse
277 277
             build=ggplot_build(out)$data
... ...
@@ -279,7 +279,7 @@ setMethod(f="chart_plot",
279 279
             ell=build[[length(build)]]
280 280
             # outlier for DatasetExperiment ellipse
281 281
             points$in.ell=as.logical(sp::point.in.polygon(points$x,points$y,ell$x,ell$y))
282
-
282
+            
283 283
             # label outliers if
284 284
             if (opt$points_to_label=='outliers')
285 285
             {
... ...
@@ -288,19 +288,19 @@ setMethod(f="chart_plot",
288 288
                     temp=subset(points,!points$in.ell)
289 289
                     temp$group=opt$groups[!points$in.ell]
290 290
                     out=out+geom_text(data=temp,aes_(x=~x,y=~y,label=~label,colour=~group),size=obj$label_size,vjust="inward",hjust="inward")
291
-
291
+                    
292 292
                 }
293 293
             }
294 294
             # add a list of outliers to the plot object
295 295
             out$outliers=trimws(slabels[!points$in.ell])
296 296
         }
297
-
297
+        
298 298
         # label all points if requested
299 299
         if (opt$points_to_label=='all')
300 300
         {
301 301
             out=out+geom_text(vjust="inward",hjust="inward")
302 302
         }
303
-
303
+        
304 304
         return(out)
305 305
     }
306 306
 )
... ...
@@ -362,7 +362,7 @@ pca_biplot_plot = function(
362 362
         description='plots a boxplot of a chosen feature for each group of a DatasetExperiment.',
363 363
         type="boxlot",
364 364
         .params=c('components','points_to_label','factor_name','scale_factor','style','label_features'),
365
-
365
+        
366 366
         components=entity(name='Components to plot',
367 367
             value=c(1,2),
368 368
             type='numeric',
... ...
@@ -397,7 +397,7 @@ pca_biplot_plot = function(
397 397
             description='Include feature labels on the plot'
398 398
         )
399 399
     )
400
-
400
+    
401 401
 )
402 402
 
403 403
 #' @export
... ...
@@ -412,22 +412,22 @@ setMethod(f="chart_plot",
412 412
         pvar=round(pvar,digits = 1)
413 413
         xlabel=paste("PC",opt$components[[1]],' (',sprintf("%.1f",pvar[opt$components[[1]]]),'%)',sep='')
414 414
         ylabel=paste("PC",opt$components[[2]],' (',sprintf("%.1f",pvar[opt$components[[2]]]),'%)',sep='')
415
-
415
+        
416 416
         P=output_value(dobj,'loadings')
417 417
         Ev=output_value(dobj,'eigenvalues')
418
-
418
+        
419 419
         # eigenvalues were square rooted when training PCA
420 420
         Ev=Ev[,1]
421 421
         Ev=Ev^2
422
-
422
+        
423 423
         ## unscale the scores
424 424
         #ev are the norms of scores
425 425
         Ts=as.matrix(Ts) %*% diag(1/Ev) # these are normalised scores
426
-
426
+        
427 427
         # scale scores and loadings by alpha
428 428
         Ts=Ts %*% diag(Ev^(1-opt$scale_factor))
429 429
         P=as.matrix(P) %*% diag(Ev^(opt$scale_factor))
430
-
430
+        
431 431
         # additionally scale the loadings
432 432
         sf=min(max(abs(Ts[,opt$components[1]]))/max(abs(P[,opt$components[1]])),
433 433
             max(abs(Ts[,opt$components[2]]))/max(abs(P[,opt$components[2]])))
... ...
@@ -436,12 +436,12 @@ setMethod(f="chart_plot",
436 436
         rownames(Ts)=rownames(dobj$scores) # fix dimnames for SE object
437 437
         colnames(Ts)=colnames(dobj$scores)
438 438
         dobj$scores$data=as.data.frame(Ts) # nb object not returned, so only temporary scaling
439
-
439
+        
440 440
         # plot
441 441
         A=data.frame("x"=P[,opt$components[1]]*sf*0.8,"y"=P[,opt$components[2]]*sf*0.8)
442 442
         C=pca_scores_plot(points_to_label=obj$points_to_label,components=obj$components,factor_name=obj$factor_name)
443 443
         out=chart_plot(C,dobj)
444
-
444
+        
445 445
         if (opt$style=='points')
446 446
         {
447 447
             out=out+
... ...
@@ -450,13 +450,13 @@ setMethod(f="chart_plot",
450 450
         if (opt$style=='arrows')
451 451
         {
452 452
             out=out+
453
-
453
+                
454 454
                 geom_segment(data=A,inherit.aes = FALSE,color='black',mapping = aes_(x=~0,y=~0,xend=~x,yend=~y),arrow=arrow(length=unit(8,'points')))
455
-
455
+            
456 456
         }
457 457
         out=out+ggtitle('PCA biplot', subtitle=NULL) +
458 458
             xlab(xlabel) + ylab(ylabel)
459
-
459
+        
460 460
         #label features if requested
461 461
         if (opt$label_features)
462 462
         {
... ...
@@ -467,9 +467,9 @@ setMethod(f="chart_plot",
467 467
             }
468 468
             A$vlabels=vlabels
469 469
             out=out+
470
-
470
+                
471 471
                 geom_text(data=A,aes_(x=~x,y=~y,label=~vlabels),vjust="inward",hjust="inward",inherit.aes = FALSE)
472
-
472
+            
473 473
         }
474 474
         return(out)
475 475
     }
... ...
@@ -494,7 +494,7 @@ setMethod(f="chart_plot",
494 494
 #' @include PCA_class.R
495 495
 #' @examples
496 496
 #' C = pca_loadings_plot()
497
-pca_loadings_plot = function(components=c(1,2),style='points',label_features=FALSE,...) {
497
+pca_loadings_plot = function(components=c(1,2),style='points',label_features=NULL,...) {
498 498
     out=struct::new_struct('pca_loadings_plot',
499 499
         components=components,
500 500
         style=style,
... ...
@@ -517,7 +517,7 @@ pca_loadings_plot = function(components=c(1,2),style='points',label_features=FAL
517 517
         description='plots a boxplot of a chosen feature for each group of a DatasetExperiment.',
518 518
         type="boxlot",
519 519
         .params=c('components','style','label_features'),
520
-
520
+        
521 521
         components=entity(name='Components to plot',
522 522
             value=c(1,2),
523 523
             type='numeric',
... ...
@@ -530,13 +530,13 @@ pca_loadings_plot = function(components=c(1,2),style='points',label_features=FAL
530 530
             description='Named plot styles for the biplot. [points], arrows',
531 531
             allowed=c('points','arrows')
532 532
         ),
533
-        label_features=entity(name='Add feature labels',
534
-            value=FALSE,
535
-            type='logical',
536
-            description='Include feature labels on the plot'
533
+        label_features=entity(name='Label features',
534
+            value=NULL,
535
+            type=c('NULL','character'),
536
+            description='Include feature labels from this variable meta column. Special keyword "rownames" will use the rownames of the variable_meta data.frame'
537 537
         )
538 538
     )
539
-
539
+    
540 540
 )
541 541
 
542 542
 #' @export
... ...
@@ -546,11 +546,10 @@ setMethod(f="chart_plot",
546 546
     definition=function(obj,dobj)
547 547
     {
548 548
         opt=param_list(obj)
549
-
549
+        
550 550
         P=output_value(dobj,'loadings')
551 551
         # 1D plot
552
-        if (length(opt$components)==1)
553
-        {
552
+        if (length(opt$components)==1) {
554 553
             A=data.frame("x"=1:nrow(P),"y"=P[,opt$components[1]])
555 554
             out=ggplot(data=A,aes_(x=~x,y=~y)) +
556 555
                 geom_line() +
... ...
@@ -559,11 +558,9 @@ setMethod(f="chart_plot",
559 558
                 ylab('Loading') +
560 559
                 scale_colour_Publication() +
561 560
                 theme_Publication(base_size = 12)
562
-            return(out)
563 561
         }
564 562
         # 2D plot
565
-        if (length(opt$components)==2)
566
-        {
563
+        if (length(opt$components)==2) {
567 564
             A=data.frame("x"=P[,opt$components[1]],"y"=P[,opt$components[2]])
568 565
             out=ggplot(data=A,aes_(x=~x,y=~y)) +
569 566
                 geom_point() +
... ...
@@ -572,13 +569,29 @@ setMethod(f="chart_plot",
572 569
                 ylab(paste0('Component ',opt$components[2])) +
573 570
                 scale_colour_Publication() +
574 571
                 theme_Publication(base_size = 12)
575
-            return(out)
572
+            
573
+            if (!is.null(obj$label_features)) {
574
+                
575
+                if (obj$label_features=='rownames') {
576
+                    vlabels=rownames(dobj$loadings)
577
+                } else {
578
+                    vlabels=obj$label_features
579
+                }
580
+                
581
+                for (i in 1:length(vlabels)) {
582
+                    vlabels[i]=paste0('  ',vlabels[i], '  ')
583
+                }
584
+                A$vlabels=vlabels
585
+                out=out+geom_text(data=A,aes_(x=~x,y=~y,label=~vlabels),
586
+                    vjust="inward",hjust="inward",inherit.aes = FALSE)
587
+            }
576 588
         }
577
-        if (length(opt$components)>2)
578
-        {
589
+        if (length(opt$components)>2) {
579 590
             stop('can only plot loadings for 1 or 2 components at a time')
580 591
         }
581
-
592
+        
593
+        return(out)
594
+        
582 595
     }
583 596
 )
584 597
 
... ...
@@ -621,12 +634,12 @@ setMethod(f="chart_plot",
621 634
         A=data.frame("x"=1:length(pvar),"y"=c(pvar,cumsum(pvar)),"Variance"=as.factor(c(rep('Single component',length(pvar)),rep('Cumulative',length(pvar)))))
622 635
         labels=round(A$y,digits = 1)
623 636
         labels=format(labels,1)
624
-
637
+        
625 638
         out=ggplot(data=A, aes_(x=~x,y=~y,color=~Variance)) +
626 639
             geom_line() +
627 640
             geom_point() +
628 641
             geom_text(aes_(label=~labels),color='black',vjust=0,nudge_y = 5) +
629
-
642
+            
630 643
             ggtitle('Scree Plot', subtitle=NULL) +
631 644
             xlab('Component') +
632 645
             ylab('Variance (%)') +
... ...
@@ -670,7 +683,7 @@ pca_dstat_plot = function(number_components=2,alpha=0.05,...) {
670 683
         description='a bar chart of the d-statistics for samples in the input PCA model',
671 684
         type="bar",
672 685
         .params=c('number_components','alpha'),
673
-
686
+        
674 687
         number_components=entity(value = 2,
675 688
             name = 'number of principal components',
676 689
             description = 'number of principal components to use for the plot',
... ...
@@ -702,16 +715,16 @@ setMethod(f="chart_plot",
702 715
         {
703 716
             H[i]=scores[i,,drop=FALSE]%*%covT%*%t(scores[i,,drop=FALSE])
704 717
         } #leverage value
705
-
718
+        
706 719
         # threshold at alpha
707 720
         F=qf(p = opt$alpha,df1 = a,df2=I-a)
708 721
         sf=(a*(I-1)*(I+1))/(I*(I-a))
709 722
         threshold=sf*F
710 723
         # ggplot
711 724
         df=data.frame(x=sample_names,y=H)
712
-
725
+        
713 726
         out=ggplot(data=df, aes_(x=~x,y=~y)) +
714
-
727
+            
715 728
             geom_bar(stat="identity") +
716 729
             geom_hline(yintercept=threshold, linetype='dashed', color='grey') +
717 730
             ggtitle('d-statistic', subtitle=paste0('Number of components = ',a)) +
Browse code

fix dimnames mismatch

Gavin Rhys Lloyd authored on 24/02/2020 11:38:17
Showing 1 changed files
... ...
@@ -428,9 +428,13 @@ setMethod(f="chart_plot",
428 428
         Ts=Ts %*% diag(Ev^(1-opt$scale_factor))
429 429
         P=as.matrix(P) %*% diag(Ev^(opt$scale_factor))
430 430
 
431
-        # additionaly scale the loadings
431
+        # additionally scale the loadings
432 432
         sf=min(max(abs(Ts[,opt$components[1]]))/max(abs(P[,opt$components[1]])),
433 433
             max(abs(Ts[,opt$components[2]]))/max(abs(P[,opt$components[2]])))
434
+        Ts=as.data.frame(Ts)
435
+        
436
+        rownames(Ts)=rownames(dobj$scores) # fix dimnames for SE object
437
+        colnames(Ts)=colnames(dobj$scores)
434 438
         dobj$scores$data=as.data.frame(Ts) # nb object not returned, so only temporary scaling
435 439
 
436 440
         # plot
Browse code

rename to use underscores instead of dot

Gavin Rhys Lloyd authored on 11/02/2020 13:19:28
Showing 1 changed files
... ...
@@ -579,25 +579,25 @@ setMethod(f="chart_plot",
579 579
 )
580 580
 
581 581
 
582
-#' pca_scree_plot class
582
+#' pca_scree_plot_plot class
583 583
 #'
584 584
 #' Line plot showing percent variance and cumulative percent variance for the computed components.
585 585
 #'
586 586
 #' @import struct
587 587
 #' @param ... additional slots and values passed to struct_class
588 588
 #' @return struct object
589
-#' @export pca_scree
589
+#' @export pca_scree_plot
590 590
 #' @include PCA_class.R
591 591
 #' @examples
592
-#' C = pca_scree()
593
-pca_scree = function(...) {
594
-    out=struct::new_struct('pca_scree',...)
592
+#' C = pca_scree_plot()
593
+pca_scree_plot = function(...) {
594
+    out=struct::new_struct('pca_scree_plot',...)
595 595
     return(out)
596 596
 }
597 597
 
598 598
 
599
-.pca_scree<-setClass(
600
-    "pca_scree",
599
+.pca_scree_plot<-setClass(
600
+    "pca_scree_plot",
601 601
     contains=c('chart'),
602 602
     prototype = list(name='Scree plot',
603 603
         description='plots the percent and cumulative percent variance for the calculated components',
... ...
@@ -608,7 +608,7 @@ pca_scree = function(...) {
608 608
 #' @export
609 609
 #' @template chart_plot
610 610
 setMethod(f="chart_plot",
611
-    signature=c("pca_scree",'PCA'),
611
+    signature=c("pca_scree_plot",'PCA'),
612 612
     definition=function(obj,dobj)
613 613
     {
614 614
         ## percent variance
... ...
@@ -634,7 +634,7 @@ setMethod(f="chart_plot",
634 634
     }
635 635
 )
636 636
 
637
-#' pca_dstat_plot class
637
+#' pca_dstat_plot_plot class
638 638
 #'
639 639
 #' Bar chart showing mahalanobis distance from the mean in PCA scores space. A threshold is
640 640
 #' plotted at a chosen confidence as an indicator for rejecting outliers.
... ...
@@ -644,12 +644,12 @@ setMethod(f="chart_plot",
644 644
 #' @param alpha The confidence level to plot.
645 645
 #' @param ... additional slots and values passed to struct_class
646 646
 #' @return struct object
647
-#' @export PCA_dstat
647
+#' @export pca_dstat_plot
648 648
 #' @include PCA_class.R
649 649
 #' @examples
650
-#' C = PCA_dstat()
651
-PCA_dstat = function(number_components=2,alpha=0.05,...) {
652
-    out=struct::new_struct('PCA_dstat',
650
+#' C = pca_dstat_plot()
651
+pca_dstat_plot = function(number_components=2,alpha=0.05,...) {
652
+    out=struct::new_struct('pca_dstat_plot',
653 653
         number_components=number_components,
654 654
         alpha=alpha,
655 655
         ...)
... ...
@@ -657,8 +657,8 @@ PCA_dstat = function(number_components=2,alpha=0.05,...) {
657 657
 }
658 658
 
659 659
 
660
-.PCA_dstat<-setClass(
661
-    "PCA_dstat",
660
+.pca_dstat_plot<-setClass(
661
+    "pca_dstat_plot",
662 662
     contains=c('chart'),
663 663
     slots=c(number_components='entity',
664 664
         alpha='entity'),
... ...
@@ -681,7 +681,7 @@ PCA_dstat = function(number_components=2,alpha=0.05,...) {
681 681
 #' @export
682 682
 #' @template chart_plot
683 683
 setMethod(f="chart_plot",
684
-    signature=c("PCA_dstat",'PCA'),
684
+    signature=c("pca_dstat_plot",'PCA'),
685 685
     definition=function(obj,dobj)
686 686
     {
687 687
         opt=param_list(obj)
Browse code

fix/update examples

Gavin Rhys Lloyd authored on 07/02/2020 17:02:22
Showing 1 changed files
... ...
@@ -87,7 +87,12 @@ setMethod(f="chart_plot",
87 87
 #' @export pca_scores_plot
88 88
 #' @include PCA_class.R
89 89
 #' @examples
90
-#' C = pca_scores_plot()
90
+#' D = iris_DatasetExperiment()
91
+#' M = mean_centre() + PCA()
92
+#' M = model_apply(M,D)
93
+#' C = pca_scores_plot(factor_name = 'Species')
94
+#' chart_plot(C,M[2])
95
+#'
91 96
 pca_scores_plot = function(
92 97
     components=c(1,2),
93 98
     points_to_label='none',
... ...
@@ -320,7 +325,7 @@ setMethod(f="chart_plot",
320 325
 #' @export pca_biplot_plot
321 326
 #' @include PCA_class.R
322 327
 #' @examples
323
-#' C = pca_biplot_plot()
328
+#' C = pca_biplot_plot(factor_name='Species')
324 329
 pca_biplot_plot = function(
325 330
     components=c(1,2),
326 331
     points_to_label='none',
Browse code

fix/update tests

Gavin Rhys Lloyd authored on 07/02/2020 09:54:30
Showing 1 changed files
... ...
@@ -79,7 +79,7 @@ setMethod(f="chart_plot",
79 79
 #' @param label_filter Only include labels for samples in the group specified by label_filter.
80 80
 #' If zero length then all labels will be included.
81 81
 #' @param label_factor The sample_meta column to use for labelling the samples.
82
-#' If zero length then the rownames will be used.
82
+#' If 'rownames' then the rownames will be used.
83 83
 #' @param label_size The text size of the labels.NB ggplot units, not font size units.
84 84
 #' Default 3.88.
85 85
 #' @param ... additional slots and values passed to struct_class
... ...
@@ -94,10 +94,10 @@ pca_scores_plot = function(
94 94
     factor_name,
95 95
     ellipse='all',
96 96
     label_filter=character(0),
97
-    label_factor=character(0),
97
+    label_factor='rownames',
98 98
     label_size=3.88,
99 99
     ...) {
100
-    out=struct::new_struct(pca_scores_plot,
100
+    out=struct::new_struct('pca_scores_plot',
101 101
         components=components,
102 102
         points_to_label=points_to_label,
103 103
         factor_name=factor_name,
... ...
@@ -356,6 +356,8 @@ pca_biplot_plot = function(
356 356
     prototype = list(name='Feature boxplot',
357 357
         description='plots a boxplot of a chosen feature for each group of a DatasetExperiment.',
358 358
         type="boxlot",
359
+        .params=c('components','points_to_label','factor_name','scale_factor','style','label_features'),
360
+
359 361
         components=entity(name='Components to plot',
360 362
             value=c(1,2),
361 363
             type='numeric',
... ...
@@ -483,7 +485,7 @@ setMethod(f="chart_plot",
483 485
 #' @include PCA_class.R
484 486
 #' @examples
485 487
 #' C = pca_loadings_plot()
486
-pca_loadings_plot = function(components=c(1,2),style='points',label_featurs=FALSE,...) {
488
+pca_loadings_plot = function(components=c(1,2),style='points',label_features=FALSE,...) {
487 489
     out=struct::new_struct('pca_loadings_plot',
488 490
         components=components,
489 491
         style=style,
... ...
@@ -629,17 +631,23 @@ setMethod(f="chart_plot",
629 631
 
630 632
 #' pca_dstat_plot class
631 633
 #'
632
-#' Line plot showing percent variance and cumulative percent variance for the computed components.
634
+#' Bar chart showing mahalanobis distance from the mean in PCA scores space. A threshold is
635
+#' plotted at a chosen confidence as an indicator for rejecting outliers.
633 636
 #'
634 637
 #' @import struct
638
+#' @param number_components The number of components to use.
639
+#' @param alpha The confidence level to plot.
635 640
 #' @param ... additional slots and values passed to struct_class
636 641
 #' @return struct object
637 642
 #' @export PCA_dstat
638 643
 #' @include PCA_class.R
639 644
 #' @examples
640 645
 #' C = PCA_dstat()
641
-PCA_dstat = function(...) {
642
-    out=struct::new_struct('PCA_dstat',...)
646
+PCA_dstat = function(number_components=2,alpha=0.05,...) {
647
+    out=struct::new_struct('PCA_dstat',
648
+        number_components=number_components,
649
+        alpha=alpha,
650
+        ...)
643 651
     return(out)
644 652
 }
645 653
 
... ...
@@ -652,6 +660,8 @@ PCA_dstat = function(...) {
652 660
     prototype = list(name='d-statistic plot',
653 661
         description='a bar chart of the d-statistics for samples in the input PCA model',
654 662
         type="bar",
663
+        .params=c('number_components','alpha'),
664
+
655 665
         number_components=entity(value = 2,
656 666
             name = 'number of principal components',
657 667
             description = 'number of principal components to use for the plot',
Browse code

update to use new struct class constructors

Gavin Rhys Lloyd authored on 06/02/2020 13:51:52
Showing 1 changed files
... ...
@@ -1,17 +1,19 @@
1 1
 #' pca_correlation_plot class
2 2
 #'
3
-#' plots the correlation between features and selected components.
3
+#' Plots the correlation between features and selected components.
4 4
 #'
5 5
 #' @import struct
6
+#' @param components The principal components to plot (\code{numeric(2)})
6 7
 #' @param ... additional slots and values passed to struct_class
7 8
 #' @return struct object
8 9
 #' @export pca_correlation_plot
9 10
 #' @include PCA_class.R
10 11
 #' @examples
11 12
 #' C = pca_correlation_plot()
12
-pca_correlation_plot = function(...) {
13
-    out=.pca_correlation_plot()
14
-    out=struct::new_struct(out,...)
13
+pca_correlation_plot = function(components=c(1,2),...) {
14
+    out=struct::new_struct('pca_correlation_plot',
15
+        components=components,
16
+        ...)
15 17
     return(out)
16 18
 }
17 19
 
... ...
@@ -26,6 +28,8 @@ pca_correlation_plot = function(...) {
26 28
     prototype = list(name='Feature boxplot',
27 29
         description='plots a boxplot of a chosen feature for each group of a DatasetExperiment.',
28 30
         type="boxlot",
31
+        .params=c('components'),
32
+
29 33
         components=entity(name='Components to plot',
30 34
             value=c(1,2),
31 35
             type='numeric',
... ...
@@ -36,7 +40,6 @@ pca_correlation_plot = function(...) {
36 40
     )
37 41
 )
38 42
 
39
-#' @param ... additional slots and values passed to struct_class
40 43
 #' @export
41 44
 #' @template chart_plot
42 45
 setMethod(f="chart_plot",
... ...
@@ -64,18 +67,45 @@ setMethod(f="chart_plot",
64 67
 
65 68
 #' pca_scores_plot class
66 69
 #'
67
-#' 2d scatter plot of princpal component scores.
70
+#' 2d scatter plot of principal component scores.
68 71
 #'
69 72
 #' @import struct
73
+#' @param components The principal components to plot (\code{numeric(2)})
74
+#' @param points_to_label "none", "all", or "outliers" will be labelled on the plot.
75
+#' @param factor_name The sample_meta column name to use for colouring the points.
76
+#' You can provide up to two factors for this plot.
77
+#' @param ellipse "all" will plot all ellipses, "group" will only plot group ellipses,
78
+#' "none" will not plot any ellipses and "sample" will plot ellipse for all samples (ignoring group).
79
+#' @param label_filter Only include labels for samples in the group specified by label_filter.
80
+#' If zero length then all labels will be included.
81
+#' @param label_factor The sample_meta column to use for labelling the samples.
82
+#' If zero length then the rownames will be used.
83
+#' @param label_size The text size of the labels.NB ggplot units, not font size units.
84
+#' Default 3.88.
70 85
 #' @param ... additional slots and values passed to struct_class
71 86
 #' @return struct object
72 87
 #' @export pca_scores_plot
73 88
 #' @include PCA_class.R
74 89
 #' @examples
75 90
 #' C = pca_scores_plot()
76
-pca_scores_plot = function(...) {
77
-    out=.pca_scores_plot()
78
-    out=struct::new_struct(out,...)
91
+pca_scores_plot = function(
92
+    components=c(1,2),
93
+    points_to_label='none',
94
+    factor_name,
95
+    ellipse='all',
96
+    label_filter=character(0),
97
+    label_factor=character(0),
98
+    label_size=3.88,
99
+    ...) {
100
+    out=struct::new_struct(pca_scores_plot,
101
+        components=components,
102
+        points_to_label=points_to_label,
103
+        factor_name=factor_name,
104
+        ellipse=ellipse,
105
+        label_filter=label_filter,
106
+        label_factor=label_factor,
107
+        label_size=label_size,
108
+        ...)
79 109
     return(out)
80 110
 }
81 111
 
... ...
@@ -90,7 +120,6 @@ pca_scores_plot = function(...) {
90 120
         factor_name='entity',
91 121
         ellipse='enum',
92 122
         label_filter='entity',
93
-        groups='ANY', # will be deprecated
94 123
         label_factor='entity',
95 124
         label_size='entity'
96 125
     ),
... ...
@@ -98,6 +127,7 @@ pca_scores_plot = function(...) {
98 127
     prototype = list(name='PCA scores plot',
99 128
         description='Plots a 2d scatter plot of the selected components',
100 129
         type="scatter",
130
+        .params=c('components','points_to_label','factor_name','ellipse','label_filter','label_factor','label_size'),
101 131
 
102 132
         components=entity(name='Components to plot',
103 133
             value=c(1,2),
... ...
@@ -191,11 +221,11 @@ setMethod(f="chart_plot",
191 221
 
192 222
         # filter by label_filter list if provided
193 223
         if (length(obj$label_filter)>0) {
194
-            out=!(opt$groups %in% obj$label_filter)
224
+            out=!(as.character(opt$groups) %in% obj$label_filter)
195 225
             slabels[out]=''
196 226
         }
197 227
 
198
-        if (is(opt$groups,'factor')) {
228
+        if (is(opt$groups,'factor') | is(opt$groups,'character')) {
199 229
             plotClass= createClassAndColors(opt$groups)
200 230
             opt$groups=plotClass$class
201 231
         }
... ...
@@ -275,18 +305,38 @@ setMethod(f="chart_plot",
275 305
 
276 306
 #' pca_biplot_plot class
277 307
 #'
278
-#' 2d scatter plot of princpal component scores overlaid with principal component loadings.
308
+#' 2d scatter plot of principal component scores overlaid with principal component loadings.
279 309
 #'
280 310
 #' @import struct
311
+#' @param components The principal components to plot (\code{numeric(2)})
312
+#' @param points_to_label "none", "all", or "outliers" will be labelled on the plot.
313
+#' @param factor_name The sample_meta column name to use for colouring the points.
314
+#' You can provide up to two factors for this plot.
315
+#' @param scale_factor Scaling factor to apply to loadings. Default = 0.95.
316
+#' @param style Plot style for loadings. Can be 'points' (default) or 'arrows'.
317
+#' @param label_features TRUE or FALSE to label features on the plot. Default is FALSE.
281 318
 #' @param ... additional slots and values passed to struct_class
282 319
 #' @return struct object
283 320
 #' @export pca_biplot_plot
284 321
 #' @include PCA_class.R
285 322
 #' @examples
286 323
 #' C = pca_biplot_plot()
287
-pca_biplot_plot = function(...) {
288
-    out=.pca_biplot_plot()
289
-    out=struct::new_struct(out,...)
324
+pca_biplot_plot = function(
325
+    components=c(1,2),
326
+    points_to_label='none',
327
+    factor_name,
328
+    scale_factor=0.95,
329
+    style='points',
330
+    label_features=FALSE,
331
+    ...) {
332
+    out=struct::new_struct('pca_biplot_plot',
333
+        components=components,
334
+        points_to_label=points_to_label,
335
+        factor_name=factor_name,
336
+        scale_factor=scale_factor,
337
+        style=style,
338
+        label_features=label_features,
339
+        ...)
290 340
     return(out)
291 341
 }
292 342
 
... ...
@@ -299,7 +349,6 @@ pca_biplot_plot = function(...) {
299 349
         components='entity',
300 350
         points_to_label='entity',
301 351
         factor_name='entity',
302
-        groups='entity',
303 352
         scale_factor='entity',
304 353
         style='enum',
305 354
         label_features='entity'
... ...
@@ -324,11 +373,6 @@ pca_biplot_plot = function(...) {
324 373
             type='character',
325 374
             description='The name of the factor to be displayed on the plot. Appears on axis and legend titles, for example. By default the column name of the meta data will be used where possible.'
326 375
         ),
327
-        groups=entity(name='Groups',
328
-            value=factor(),
329
-            type='factor',
330
-            description='The name of the factor to be displayed on the plot. Appears on axis and legend titles, for example. By default the column name of the meta data will be used where possible.'
331
-        ),
332 376
         scale_factor=entity(name='Loadings scale factor',
333 377
             value=0.95,
334 378
             type='numeric',
... ...
@@ -349,7 +393,6 @@ pca_biplot_plot = function(...) {
349 393
 
350 394
 )
351 395
 
352
-#' @param ... additional slots and values passed to struct_class
353 396
 #' @export
354 397
 #' @template chart_plot
355 398
 setMethod(f="chart_plot",
... ...
@@ -385,7 +428,7 @@ setMethod(f="chart_plot",
385 428
 
386 429
         # plot
387 430
         A=data.frame("x"=P[,opt$components[1]]*sf*0.8,"y"=P[,opt$components[2]]*sf*0.8)
388
-        C=pca_scores_plot(groups=obj$groups,points_to_label=obj$points_to_label,components=obj$components,factor_name=obj$factor_name)
431
+        C=pca_scores_plot(points_to_label=obj$points_to_label,components=obj$components,factor_name=obj$factor_name)
389 432
         out=chart_plot(C,dobj)
390 433
 
391 434
         if (opt$style=='points')
... ...
@@ -431,15 +474,21 @@ setMethod(f="chart_plot",
431 474
 #' 2d scatter plot of princpal component loadings.
432 475
 #'
433 476
 #' @import struct
477
+#' @param components The principal components to plot (\code{numeric(2)})
478
+#' @param style Plot style for loadings. Can be 'points' (default) or 'arrows'.
479
+#' @param label_features TRUE or FALSE to label features on the plot. Default is FALSE.
434 480
 #' @param ... additional slots and values passed to struct_class
435 481
 #' @return struct object
436 482
 #' @export pca_loadings_plot
437 483
 #' @include PCA_class.R
438 484
 #' @examples
439 485
 #' C = pca_loadings_plot()
440
-pca_loadings_plot = function(...) {
441
-    out=.pca_loadings_plot()
442
-    out=struct::new_struct(out,...)
486
+pca_loadings_plot = function(components=c(1,2),style='points',label_featurs=FALSE,...) {
487
+    out=struct::new_struct('pca_loadings_plot',
488
+        components=components,
489
+        style=style,
490
+        label_features=label_features,
491
+        ...)
443 492
     return(out)
444 493
 }
445 494
 
... ...
@@ -456,6 +505,8 @@ pca_loadings_plot = function(...) {
456 505
     prototype = list(name='Feature boxplot',
457 506
         description='plots a boxplot of a chosen feature for each group of a DatasetExperiment.',
458 507
         type="boxlot",
508
+        .params=c('components','style','label_features'),
509
+
459 510
         components=entity(name='Components to plot',
460 511
             value=c(1,2),
461 512
             type='numeric',
... ...
@@ -477,10 +528,6 @@ pca_loadings_plot = function(...) {
477 528
 
478 529
 )
479 530
 
480
-
481
-
482
-
483
-#' @param ... additional slots and values passed to struct_class
484 531
 #' @export
485 532
 #' @template chart_plot
486 533
 setMethod(f="chart_plot",
... ...
@@ -527,7 +574,7 @@ setMethod(f="chart_plot",
527 574
 
528 575
 #' pca_scree_plot class
529 576
 #'
530
-#' line plot showing percent variance and cumulative peercent variance for the computed components.
577
+#' Line plot showing percent variance and cumulative percent variance for the computed components.
531 578
 #'
532 579
 #' @import struct
533 580
 #' @param ... additional slots and values passed to struct_class
... ...
@@ -537,8 +584,7 @@ setMethod(f="chart_plot",
537 584
 #' @examples
538 585
 #' C = pca_scree()
539 586
 pca_scree = function(...) {
540
-    out=.pca_scree()
541
-    out=struct::new_struct(out,...)
587
+    out=struct::new_struct('pca_scree',...)
542 588
     return(out)
543 589
 }
544 590
 
... ...
@@ -552,7 +598,6 @@ pca_scree = function(...) {
552 598
     )
553 599
 )
554 600
 
555
-#' @param ... additional slots and values passed to struct_class
556 601
 #' @export
557 602
 #' @template chart_plot
558 603
 setMethod(f="chart_plot",
... ...
@@ -584,7 +629,7 @@ setMethod(f="chart_plot",
584 629
 
585 630
 #' pca_dstat_plot class
586 631
 #'
587
-#' line plot showing percent variance and cumulative peercent variance for the computed components.
632
+#' Line plot showing percent variance and cumulative percent variance for the computed components.
588 633
 #'
589 634
 #' @import struct
590 635
 #' @param ... additional slots and values passed to struct_class
... ...
@@ -594,8 +639,7 @@ setMethod(f="chart_plot",
594 639
 #' @examples
595 640
 #' C = PCA_dstat()
596 641
 PCA_dstat = function(...) {
597
-    out=.PCA_dstat()
598
-    out=struct::new_struct(out,...)
642
+    out=struct::new_struct('PCA_dstat',...)
599 643
     return(out)
600 644
 }
601 645
 
... ...
@@ -619,7 +663,6 @@ PCA_dstat = function(...) {
619 663
     )
620 664
 )
621 665
 
622
-#' @param ... additional slots and values passed to struct_class
623 666
 #' @export
624 667
 #' @template chart_plot
625 668
 setMethod(f="chart_plot",
Browse code

incremental changes to use struct class constructors

Gavin Rhys Lloyd authored on 04/02/2020 17:18:11
Showing 1 changed files
... ...
@@ -3,7 +3,7 @@
3 3
 #' plots the correlation between features and selected components.
4 4
 #'
5 5
 #' @import struct
6
-#' @param ... slots and values for the new object
6
+#' @param ... additional slots and values passed to struct_class
7 7
 #' @return struct object
8 8
 #' @export pca_correlation_plot
9 9
 #' @include PCA_class.R
... ...
@@ -36,7 +36,7 @@ pca_correlation_plot = function(...) {
36 36
     )
37 37
 )
38 38
 
39
-#' @param ... slots and values for the new object
39
+#' @param ... additional slots and values passed to struct_class
40 40
 #' @export
41 41
 #' @template chart_plot
42 42
 setMethod(f="chart_plot",
... ...
@@ -67,7 +67,7 @@ setMethod(f="chart_plot",
67 67
 #' 2d scatter plot of princpal component scores.
68 68
 #'
69 69
 #' @import struct
70
-#' @param ... slots and values for the new object
70
+#' @param ... additional slots and values passed to struct_class
71 71
 #' @return struct object
72 72
 #' @export pca_scores_plot
73 73
 #' @include PCA_class.R
... ...
@@ -146,7 +146,7 @@ pca_scores_plot = function(...) {
146 146
 #' @importFrom sp point.in.polygon
147 147
 #' @import ggplot2
148 148
 #' @importFrom scales squish
149
-#' @param ... slots and values for the new object
149
+#' @param ... additional slots and values passed to struct_class
150 150
 #' @export
151 151
 #' @template chart_plot
152 152
 setMethod(f="chart_plot",
... ...
@@ -278,7 +278,7 @@ setMethod(f="chart_plot",
278 278
 #' 2d scatter plot of princpal component scores overlaid with principal component loadings.
279 279
 #'
280 280
 #' @import struct
281
-#' @param ... slots and values for the new object
281
+#' @param ... additional slots and values passed to struct_class
282 282
 #' @return struct object
283 283
 #' @export pca_biplot_plot
284 284
 #' @include PCA_class.R
... ...
@@ -349,7 +349,7 @@ pca_biplot_plot = function(...) {
349 349
 
350 350
 )
351 351
 
352
-#' @param ... slots and values for the new object
352
+#' @param ... additional slots and values passed to struct_class
353 353
 #' @export
354 354
 #' @template chart_plot
355 355
 setMethod(f="chart_plot",
... ...
@@ -431,7 +431,7 @@ setMethod(f="chart_plot",
431 431
 #' 2d scatter plot of princpal component loadings.
432 432
 #'
433 433
 #' @import struct
434
-#' @param ... slots and values for the new object
434
+#' @param ... additional slots and values passed to struct_class
435 435
 #' @return struct object
436 436
 #' @export pca_loadings_plot
437 437
 #' @include PCA_class.R
... ...
@@ -480,7 +480,7 @@ pca_loadings_plot = function(...) {
480 480
 
481 481
 
482 482
 
483
-#' @param ... slots and values for the new object
483
+#' @param ... additional slots and values passed to struct_class
484 484
 #' @export
485 485
 #' @template chart_plot
486 486
 setMethod(f="chart_plot",
... ...
@@ -530,7 +530,7 @@ setMethod(f="chart_plot",
530 530
 #' line plot showing percent variance and cumulative peercent variance for the computed components.
531 531
 #'
532 532
 #' @import struct
533
-#' @param ... slots and values for the new object
533
+#' @param ... additional slots and values passed to struct_class
534 534
 #' @return struct object
535 535
 #' @export pca_scree
536 536
 #' @include PCA_class.R
... ...
@@ -552,7 +552,7 @@ pca_scree = function(...) {
552 552
     )
553 553
 )
554 554
 
555
-#' @param ... slots and values for the new object
555
+#' @param ... additional slots and values passed to struct_class
556 556
 #' @export
557 557
 #' @template chart_plot
558 558
 setMethod(f="chart_plot",
... ...
@@ -587,7 +587,7 @@ setMethod(f="chart_plot",
587 587
 #' line plot showing percent variance and cumulative peercent variance for the computed components.
588 588
 #'
589 589
 #' @import struct
590
-#' @param ... slots and values for the new object
590
+#' @param ... additional slots and values passed to struct_class
591 591
 #' @return struct object
592 592
 #' @export PCA_dstat
593 593
 #' @include PCA_class.R
... ...
@@ -619,7 +619,7 @@ PCA_dstat = function(...) {
619 619
     )
620 620
 )
621 621
 
622
-#' @param ... slots and values for the new object
622
+#' @param ... additional slots and values passed to struct_class
623 623
 #' @export
624 624
 #' @template chart_plot
625 625
 setMethod(f="chart_plot",
Browse code

remove all params_ and outputs_ tags

also fix resulting duplicate slot name 'type' for mixed_effects

Gavin Rhys Lloyd authored on 04/02/2020 10:28:42
Showing 1 changed files
... ...
@@ -11,7 +11,7 @@
11 11
 #' C = pca_correlation_plot()
12 12
 pca_correlation_plot = function(...) {
13 13
     out=.pca_correlation_plot()
14
-    out=struct::.initialize_struct_class(out,...)
14
+    out=struct::new_struct(out,...)
15 15
     return(out)
16 16
 }
17 17
 
... ...
@@ -21,12 +21,12 @@ pca_correlation_plot = function(...) {
21 21
     contains='chart',
22 22
     slots=c(
23 23
         # INPUTS
24
-        params_components='entity'
24
+        components='entity'
25 25
     ),
26 26
     prototype = list(name='Feature boxplot',
27 27
         description='plots a boxplot of a chosen feature for each group of a DatasetExperiment.',
28 28
         type="boxlot",
29
-        params_components=entity(name='Components to plot',
29
+        components=entity(name='Components to plot',
30 30
             value=c(1,2),
31 31
             type='numeric',
32 32
             description='the components to be plotted e.g. c(1,2) plots component 1 on the x axis and component 2 on the y axis.',
... ...
@@ -75,7 +75,7 @@ setMethod(f="chart_plot",
75 75
 #' C = pca_scores_plot()
76 76
 pca_scores_plot = function(...) {
77 77
     out=.pca_scores_plot()
78
-    out=struct::.initialize_struct_class(out,...)
78
+    out=struct::new_struct(out,...)
79 79
     return(out)
80 80
 }
81 81
 
... ...
@@ -85,56 +85,56 @@ pca_scores_plot = function(...) {
85 85
     contains='chart',
86 86
     slots=c(
87 87
         # INPUTS
88
-        params_components='entity',
89
-        params_points_to_label='enum',
90
-        params_factor_name='entity',
91
-        params_ellipse='enum',
92
-        params_label_filter='entity',
93
-        params_groups='ANY', # will be deprecated
94
-        params_label_factor='entity',
95
-        params_label_size='entity'
88
+        components='entity',
89
+        points_to_label='enum',
90
+        factor_name='entity',
91
+        ellipse='enum',
92
+        label_filter='entity',
93
+        groups='ANY', # will be deprecated
94
+        label_factor='entity',
95
+        label_size='entity'
96 96
     ),
97 97
 
98 98
     prototype = list(name='PCA scores plot',
99 99
         description='Plots a 2d scatter plot of the selected components',
100 100
         type="scatter",
101 101
 
102
-        params_components=entity(name='Components to plot',
102
+        components=entity(name='Components to plot',
103 103
             value=c(1,2),
104 104
             type='numeric',
105 105
             description='the components to be plotted e.g. c(1,2) plots component 1 on the x axis and component 2 on the y axis.',
106 106
             max_length=2
107 107
         ),
108 108
 
109
-        params_points_to_label=enum(name='points_to_label',
109
+        points_to_label=enum(name='points_to_label',
110 110
             value='none',
111 111
             type='character',
112 112
             description='("none"), "all", or "outliers" will be labelled on the plot.',
113 113
             allowed=c('none','all','outliers')
114 114
         ),
115
-        params_factor_name=entity(name='Factor name',
115
+        factor_name=entity(name='Factor name',
116 116
             value='factor',
117 117
             type='character',
118 118
             description='The column name of sample meta to use for plotting. A second column can be included to plot using symbols.',
119 119
             max_length=2
120 120
         ),
121
-        params_ellipse=enum(name = 'Plot ellipses',description=c(
121
+        ellipse=enum(name = 'Plot ellipses',description=c(
122 122
             '"all" will plot all ellipses',
123 123
             '"group" will only plot group ellipses',
124 124
             '"none" will not plot any ellipses',
125 125
             '"sample" will plot ellipse for all samples (ignoring group)'),
126 126
             allowed=c('all','group','none','sample'),
127 127
             value='all'),
128
-        params_label_filter=entity(name='Label filter',
128
+        label_filter=entity(name='Label filter',
129 129
             value=character(0),
130 130
             type='character',
131
-            description='Only include the param.group labels included in params_label_filter. If zero length then all labels will be included.'
131
+            description='Only include the param.group labels included in label_filter. If zero length then all labels will be included.'
132 132
         ),
133
-        params_label_factor=entity(name='Factor for labels',
133
+        label_factor=entity(name='Factor for labels',
134 134
             description='The column name of sample_meta to use as labels. "rownames" will use the row names from sample_meta.',
135 135
             type='character',
136 136
             value='rownames'),
137
-        params_label_size=entity(name='Text size of labels',
137
+        label_size=entity(name='Text size of labels',
138 138
             description='The text size of labels. Note this is not in Font Units. Default 3.88.',
139 139
             type='numeric',
140 140
             value=3.88)
... ...
@@ -286,7 +286,7 @@ setMethod(f="chart_plot",
286 286
 #' C = pca_biplot_plot()
287 287
 pca_biplot_plot = function(...) {
288 288
     out=.pca_biplot_plot()
289
-    out=struct::.initialize_struct_class(out,...)
289
+    out=struct::new_struct(out,...)
290 290
     return(out)
291 291
 }
292 292
 
... ...
@@ -296,51 +296,51 @@ pca_biplot_plot = function(...) {
296 296
     contains='chart',
297 297
     slots=c(
298 298
         # INPUTS
299
-        params_components='entity',
300
-        params_points_to_label='entity',
301
-        params_factor_name='entity',
302
-        params_groups='entity',
303
-        params_scale_factor='entity',
304
-        params_style='enum',
305
-        params_label_features='entity'
299
+        components='entity',
300
+        points_to_label='entity',
301
+        factor_name='entity',
302
+        groups='entity',
303
+        scale_factor='entity',
304
+        style='enum',
305
+        label_features='entity'
306 306
     ),
307 307
     prototype = list(name='Feature boxplot',
308 308
         description='plots a boxplot of a chosen feature for each group of a DatasetExperiment.',
309 309
         type="boxlot",
310
-        params_components=entity(name='Components to plot',
310
+        components=entity(name='Components to plot',
311 311
             value=c(1,2),
312 312
             type='numeric',
313 313
             description='the components to be plotted e.g. c(1,2) plots component 1 on the x axis and component 2 on the y axis.',
314 314
             max_length=2
315 315
         ),
316
-        params_points_to_label=enum(name='points_to_label',
316
+        points_to_label=enum(name='points_to_label',
317 317
             value='none',
318 318
             type='character',
319 319
             description='("none"), "all", or "outliers" will be labelled on the plot.',
320 320
             allowed=c('none','all','outliers')
321 321
         ),
322
-        params_factor_name=entity(name='Factor name',
322
+        factor_name=entity(name='Factor name',
323 323
             value='factor',
324 324
             type='character',
325 325
             description='The name of the factor to be displayed on the plot. Appears on axis and legend titles, for example. By default the column name of the meta data will be used where possible.'
326 326
         ),
327
-        params_groups=entity(name='Groups',
327
+        groups=entity(name='Groups',
328 328
             value=factor(),
329 329
             type='factor',
330 330
             description='The name of the factor to be displayed on the plot. Appears on axis and legend titles, for example. By default the column name of the meta data will be used where possible.'
331 331
         ),
332
-        params_scale_factor=entity(name='Loadings scale factor',
332
+        scale_factor=entity(name='Loadings scale factor',
333 333
             value=0.95,
334 334
             type='numeric',
335 335
             description='Scaling factor to apply to loadings. Default = 0.95.'
336 336
         ),
337
-        params_style=enum(name='Plot style',
337
+        style=enum(name='Plot style',
338 338
             value='points',
339 339
             type='character',
340 340
             description='Named plot styles for the biplot. [points], arrows',
341 341
             allowed=c('points','arrows')
342 342
         ),
343
-        params_label_features=entity(name='Add feature labels',
343
+        label_features=entity(name='Add feature labels',
344 344
             value=FALSE,
345 345
             type='logical',
346 346
             description='Include feature labels on the plot'
... ...
@@ -439,7 +439,7 @@ setMethod(f="chart_plot",
439 439
 #' C = pca_loadings_plot()
440 440
 pca_loadings_plot = function(...) {
441 441
     out=.pca_loadings_plot()
442
-    out=struct::.initialize_struct_class(out,...)
442
+    out=struct::new_struct(out,...)
443 443
     return(out)
444 444
 }
445 445
 
... ...
@@ -449,26 +449,26 @@ pca_loadings_plot = function(...) {
449 449
     contains='chart',
450 450
     slots=c(
451 451
         # INPUTS
452
-        params_components='entity',
453
-        params_style='enum',
454
-        params_label_features='entity'
452
+        components='entity',
453
+        style='enum',
454
+        label_features='entity'
455 455
     ),
456 456
     prototype = list(name='Feature boxplot',
457 457
         description='plots a boxplot of a chosen feature for each group of a DatasetExperiment.',
458 458
         type="boxlot",
459
-        params_components=entity(name='Components to plot',
459
+        components=entity(name='Components to plot',
460 460
             value=c(1,2),
461 461
             type='numeric',
462 462
             description='the components to be plotted e.g. c(1,2) plots component 1 on the x axis and component 2 on the y axis.',
463 463
             max_length=2
464 464
         ),
465
-        params_style=enum(name='Plot style',
465
+        style=enum(name='Plot style',
466 466
             value='points',
467 467
             type='character',
468 468
             description='Named plot styles for the biplot. [points], arrows',
469 469
             allowed=c('points','arrows')
470 470
         ),
471
-        params_label_features=entity(name='Add feature labels',
471
+        label_features=entity(name='Add feature labels',
472 472
             value=FALSE,
473 473
             type='logical',
474 474
             description='Include feature labels on the plot'
... ...
@@ -538,7 +538,7 @@ setMethod(f="chart_plot",
538 538
 #' C = pca_scree()
539 539
 pca_scree = function(...) {
540 540
     out=.pca_scree()
541
-    out=struct::.initialize_struct_class(out,...)
541
+    out=struct::new_struct(out,...)
542 542
     return(out)
543 543
 }
544 544
 
... ...
@@ -595,7 +595,7 @@ setMethod(f="chart_plot",
595 595
 #' C = PCA_dstat()
596 596
 PCA_dstat = function(...) {
597 597
     out=.PCA_dstat()
598
-    out=struct::.initialize_struct_class(out,...)
598
+    out=struct::new_struct(out,...)
599 599
     return(out)
600 600
 }
601 601
 
... ...
@@ -603,16 +603,16 @@ PCA_dstat = function(...) {
603 603
 .PCA_dstat<-setClass(
604 604
     "PCA_dstat",
605 605
     contains=c('chart'),
606
-    slots=c(params_number_components='entity',
607
-        params_alpha='entity'),
606
+    slots=c(number_components='entity',
607
+        alpha='entity'),
608 608
     prototype = list(name='d-statistic plot',
609 609
         description='a bar chart of the d-statistics for samples in the input PCA model',
610 610
         type="bar",
611
-        params_number_components=entity(value = 2,
611
+        number_components=entity(value = 2,
612 612
             name = 'number of principal components',
613 613
             description = 'number of principal components to use for the plot',
614 614
             type='numeric'),
615
-        params_alpha=entity(value=0.95,
615
+        alpha=entity(value=0.95,
616 616
             name='threshold for rejecting outliers',
617 617
             description='a confidence threshold for rejecting samples based on the d-statistic',
618 618
             type='numeric')
Browse code

change "list" to "allowed" for enums

Gavin Rhys Lloyd authored on 04/02/2020 10:21:36
Showing 1 changed files
... ...
@@ -3,7 +3,7 @@
3 3
 #' plots the correlation between features and selected components.
4 4
 #'
5 5
 #' @import struct
6
-#' @param ... slots and values for the new object 
6
+#' @param ... slots and values for the new object
7 7
 #' @return struct object
8 8
 #' @export pca_correlation_plot
9 9
 #' @include PCA_class.R
... ...
@@ -36,7 +36,7 @@ pca_correlation_plot = function(...) {
36 36
     )
37 37
 )
38 38
 
39
-#' @param ... slots and values for the new object 
39
+#' @param ... slots and values for the new object
40 40
 #' @export
41 41
 #' @template chart_plot
42 42
 setMethod(f="chart_plot",
... ...
@@ -67,7 +67,7 @@ setMethod(f="chart_plot",
67 67
 #' 2d scatter plot of princpal component scores.
68 68
 #'
69 69
 #' @import struct
70
-#' @param ... slots and values for the new object 
70
+#' @param ... slots and values for the new object
71 71
 #' @return struct object
72 72
 #' @export pca_scores_plot
73 73
 #' @include PCA_class.R
... ...
@@ -110,7 +110,7 @@ pca_scores_plot = function(...) {
110 110
             value='none',
111 111
             type='character',
112 112
             description='("none"), "all", or "outliers" will be labelled on the plot.',
113
-            list=c('none','all','outliers')
113
+            allowed=c('none','all','outliers')
114 114
         ),
115 115
         params_factor_name=entity(name='Factor name',
116 116
             value='factor',
... ...
@@ -123,18 +123,18 @@ pca_scores_plot = function(...) {
123 123
             '"group" will only plot group ellipses',
124 124
             '"none" will not plot any ellipses',
125 125
             '"sample" will plot ellipse for all samples (ignoring group)'),
126
-            list=c('all','group','none','sample'),
126
+            allowed=c('all','group','none','sample'),
127 127
             value='all'),
128 128
         params_label_filter=entity(name='Label filter',
129 129
             value=character(0),
130 130
             type='character',
131 131
             description='Only include the param.group labels included in params_label_filter. If zero length then all labels will be included.'
132 132
         ),
133
-        params_label_factor=entity(names='Factor for labels',
133
+        params_label_factor=entity(name='Factor for labels',
134 134
             description='The column name of sample_meta to use as labels. "rownames" will use the row names from sample_meta.',
135 135
             type='character',
136 136
             value='rownames'),
137
-        params_label_size=entity(names='Text size of labels',
137
+        params_label_size=entity(name='Text size of labels',
138 138
             description='The text size of labels. Note this is not in Font Units. Default 3.88.',
139 139
             type='numeric',
140 140
             value=3.88)
... ...
@@ -146,7 +146,7 @@ pca_scores_plot = function(...) {
146 146
 #' @importFrom sp point.in.polygon
147 147
 #' @import ggplot2
148 148
 #' @importFrom scales squish
149
-#' @param ... slots and values for the new object 
149
+#' @param ... slots and values for the new object
150 150
 #' @export
151 151
 #' @template chart_plot
152 152
 setMethod(f="chart_plot",
... ...
@@ -278,7 +278,7 @@ setMethod(f="chart_plot",
278 278
 #' 2d scatter plot of princpal component scores overlaid with principal component loadings.
279 279
 #'
280 280
 #' @import struct
281
-#' @param ... slots and values for the new object 
281
+#' @param ... slots and values for the new object
282 282
 #' @return struct object
283 283
 #' @export pca_biplot_plot
284 284
 #' @include PCA_class.R
... ...
@@ -313,10 +313,11 @@ pca_biplot_plot = function(...) {
313 313
             description='the components to be plotted e.g. c(1,2) plots component 1 on the x axis and component 2 on the y axis.',
314 314
             max_length=2
315 315
         ),
316
-        params_points_to_label=entity(name='points_to_label',
316
+        params_points_to_label=enum(name='points_to_label',
317 317
             value='none',
318 318
             type='character',
319
-            description='("none"), "all", or "outliers" will be labelled on the plot.'
319
+            description='("none"), "all", or "outliers" will be labelled on the plot.',
320
+            allowed=c('none','all','outliers')
320 321
         ),
321 322
         params_factor_name=entity(name='Factor name',
322 323
             value='factor',
... ...
@@ -337,7 +338,7 @@ pca_biplot_plot = function(...) {
337 338
             value='points',
338 339
             type='character',
339 340
             description='Named plot styles for the biplot. [points], arrows',
340
-            list=c('points','arrows')
341
+            allowed=c('points','arrows')
341 342
         ),
342 343
         params_label_features=entity(name='Add feature labels',
343 344
             value=FALSE,
... ...
@@ -348,7 +349,7 @@ pca_biplot_plot = function(...) {
348 349
 
349 350
 )
350 351
 
351
-#' @param ... slots and values for the new object 
352
+#' @param ... slots and values for the new object
352 353
 #' @export
353 354
 #' @template chart_plot
354 355
 setMethod(f="chart_plot",
... ...
@@ -430,7 +431,7 @@ setMethod(f="chart_plot",
430 431
 #' 2d scatter plot of princpal component loadings.
431 432
 #'
432 433
 #' @import struct
433
-#' @param ... slots and values for the new object 
434
+#' @param ... slots and values for the new object
434 435
 #' @return struct object
435 436
 #' @export pca_loadings_plot
436 437
 #' @include PCA_class.R
... ...
@@ -465,7 +466,7 @@ pca_loadings_plot = function(...) {
465 466
             value='points',
466 467
             type='character',
467 468
             description='Named plot styles for the biplot. [points], arrows',
468
-            list=c('points','arrows')
469
+            allowed=c('points','arrows')
469 470
         ),
470 471
         params_label_features=entity(name='Add feature labels',
471 472
             value=FALSE,
... ...
@@ -479,7 +480,7 @@ pca_loadings_plot = function(...) {
479 480
 
480 481
 
481 482
 
482
-#' @param ... slots and values for the new object 
483
+#' @param ... slots and values for the new object
483 484
 #' @export
484 485
 #' @template chart_plot
485 486
 setMethod(f="chart_plot",
... ...
@@ -529,7 +530,7 @@ setMethod(f="chart_plot",
529 530
 #' line plot showing percent variance and cumulative peercent variance for the computed components.
530 531
 #'
531 532
 #' @import struct
532
-#' @param ... slots and values for the new object 
533
+#' @param ... slots and values for the new object
533 534
 #' @return struct object
534 535
 #' @export pca_scree
535 536
 #' @include PCA_class.R
... ...
@@ -551,7 +552,7 @@ pca_scree = function(...) {
551 552
     )
552 553
 )
553 554
 
554
-#' @param ... slots and values for the new object 
555
+#' @param ... slots and values for the new object
555 556
 #' @export
556 557
 #' @template chart_plot
557 558
 setMethod(f="chart_plot",
... ...
@@ -586,7 +587,7 @@ setMethod(f="chart_plot",
586 587
 #' line plot showing percent variance and cumulative peercent variance for the computed components.
587 588
 #'
588 589
 #' @import struct
589
-#' @param ... slots and values for the new object 
590
+#' @param ... slots and values for the new object
590 591
 #' @return struct object
591 592
 #' @export PCA_dstat
592 593
 #' @include PCA_class.R
... ...
@@ -618,7 +619,7 @@ PCA_dstat = function(...) {
618 619
     )
619 620
 )
620 621
 
621
-#' @param ... slots and values for the new object 
622
+#' @param ... slots and values for the new object
622 623
 #' @export
623 624
 #' @template chart_plot
624 625
 setMethod(f="chart_plot",
Browse code

add @return to documentation

Gavin Rhys Lloyd authored on 19/12/2019 15:14:02
Showing 1 changed files
... ...
@@ -4,6 +4,7 @@
4 4
 #'
5 5
 #' @import struct
6 6
 #' @param ... slots and values for the new object 
7
+#' @return struct object
7 8
 #' @export pca_correlation_plot
8 9
 #' @include PCA_class.R
9 10
 #' @examples
... ...
@@ -67,6 +68,7 @@ setMethod(f="chart_plot",
67 68
 #'
68 69
 #' @import struct
69 70
 #' @param ... slots and values for the new object 
71
+#' @return struct object
70 72
 #' @export pca_scores_plot
71 73
 #' @include PCA_class.R
72 74
 #' @examples
... ...
@@ -277,6 +279,7 @@ setMethod(f="chart_plot",
277 279
 #'
278 280
 #' @import struct
279 281
 #' @param ... slots and values for the new object 
282
+#' @return struct object
280 283
 #' @export pca_biplot_plot
281 284
 #' @include PCA_class.R
282 285
 #' @examples
... ...
@@ -428,6 +431,7 @@ setMethod(f="chart_plot",
428 431
 #'
429 432
 #' @import struct
430 433
 #' @param ... slots and values for the new object 
434
+#' @return struct object
431 435
 #' @export pca_loadings_plot
432 436
 #' @include PCA_class.R
433 437
 #' @examples
... ...
@@ -526,6 +530,7 @@ setMethod(f="chart_plot",
526 530
 #'
527 531
 #' @import struct
528 532
 #' @param ... slots and values for the new object 
533
+#' @return struct object
529 534
 #' @export pca_scree
530 535
 #' @include PCA_class.R
531 536
 #' @examples
... ...
@@ -582,6 +587,7 @@ setMethod(f="chart_plot",
582 587
 #'
583 588
 #' @import struct
584 589
 #' @param ... slots and values for the new object 
590
+#' @return struct object
585 591
 #' @export PCA_dstat
586 592
 #' @include PCA_class.R
587 593
 #' @examples
Browse code

fix broken tests and...

...update some documentation

Gavin Rhys Lloyd authored on 17/12/2019 17:24:38
Showing 1 changed files
... ...
@@ -3,6 +3,7 @@
3 3
 #' plots the correlation between features and selected components.
4 4
 #'
5 5
 #' @import struct
6
+#' @param ... slots and values for the new object 
6 7
 #' @export pca_correlation_plot
7 8
 #' @include PCA_class.R
8 9
 #' @examples
... ...
@@ -34,6 +35,7 @@ pca_correlation_plot = function(...) {
34 35
     )
35 36
 )
36 37
 
38
+#' @param ... slots and values for the new object 
37 39
 #' @export
38 40
 #' @template chart_plot
39 41
 setMethod(f="chart_plot",
... ...
@@ -64,6 +66,7 @@ setMethod(f="chart_plot",
64 66
 #' 2d scatter plot of princpal component scores.
65 67
 #'
66 68
 #' @import struct
69
+#' @param ... slots and values for the new object 
67 70
 #' @export pca_scores_plot
68 71
 #' @include PCA_class.R
69 72
 #' @examples
... ...
@@ -141,6 +144,7 @@ pca_scores_plot = function(...) {
141 144
 #' @importFrom sp point.in.polygon
142 145
 #' @import ggplot2
143 146
 #' @importFrom scales squish
147
+#' @param ... slots and values for the new object 
144 148
 #' @export
145 149
 #' @template chart_plot
146 150
 setMethod(f="chart_plot",
... ...
@@ -272,6 +276,7 @@ setMethod(f="chart_plot",
272 276
 #' 2d scatter plot of princpal component scores overlaid with principal component loadings.
273 277
 #'
274 278
 #' @import struct
279
+#' @param ... slots and values for the new object 
275 280
 #' @export pca_biplot_plot
276 281
 #' @include PCA_class.R
277 282
 #' @examples
... ...
@@ -340,6 +345,7 @@ pca_biplot_plot = function(...) {
340 345
 
341 346
 )
342 347
 
348
+#' @param ... slots and values for the new object 
343 349
 #' @export
344 350
 #' @template chart_plot
345 351
 setMethod(f="chart_plot",
... ...
@@ -421,6 +427,7 @@ setMethod(f="chart_plot",
421 427
 #' 2d scatter plot of princpal component loadings.
422 428
 #'
423 429
 #' @import struct
430
+#' @param ... slots and values for the new object 
424 431
 #' @export pca_loadings_plot
425 432
 #' @include PCA_class.R
426 433
 #' @examples
... ...
@@ -468,6 +475,7 @@ pca_loadings_plot = function(...) {
468 475
 
469 476
 
470 477
 
478
+#' @param ... slots and values for the new object 
471 479
 #' @export
472 480
 #' @template chart_plot
473 481
 setMethod(f="chart_plot",
... ...
@@ -517,6 +525,7 @@ setMethod(f="chart_plot",
517 525
 #' line plot showing percent variance and cumulative peercent variance for the computed components.
518 526
 #'
519 527
 #' @import struct
528
+#' @param ... slots and values for the new object 
520 529
 #' @export pca_scree
521 530
 #' @include PCA_class.R
522 531
 #' @examples
... ...
@@ -537,6 +546,7 @@ pca_scree = function(...) {
537 546
     )
538 547
 )
539 548
 
549
+#' @param ... slots and values for the new object 
540 550
 #' @export
541 551
 #' @template chart_plot
542 552
 setMethod(f="chart_plot",
... ...
@@ -571,6 +581,7 @@ setMethod(f="chart_plot",
571 581
 #' line plot showing percent variance and cumulative peercent variance for the computed components.
572 582
 #'
573 583
 #' @import struct
584
+#' @param ... slots and values for the new object 
574 585
 #' @export PCA_dstat
575 586
 #' @include PCA_class.R
576 587
 #' @examples
... ...
@@ -601,6 +612,7 @@ PCA_dstat = function(...) {
601 612
     )
602 613
 )
603 614
 
615
+#' @param ... slots and values for the new object 
604 616
 #' @export
605 617
 #' @template chart_plot
606 618
 setMethod(f="chart_plot",
Browse code

use class contructors and...

...rename all function with dot to underscore
replace dataset with DatasetExperiment

Gavin Rhys Lloyd authored on 17/12/2019 15:48:01
Showing 1 changed files
... ...
@@ -7,17 +7,24 @@
7 7
 #' @include PCA_class.R
8 8
 #' @examples
9 9
 #' C = pca_correlation_plot()
10
-pca_correlation_plot<-setClass(
10
+pca_correlation_plot = function(...) {
11
+    out=.pca_correlation_plot()
12
+    out=struct::.initialize_struct_class(out,...)
13
+    return(out)
14
+}
15
+
16
+
17
+.pca_correlation_plot<-setClass(
11 18
     "pca_correlation_plot",
12 19
     contains='chart',
13 20
     slots=c(
14 21
         # INPUTS
15
-        params.components='entity'
22
+        params_components='entity'
16 23
     ),
17 24
     prototype = list(name='Feature boxplot',
18
-        description='plots a boxplot of a chosen feature for each group of a dataset.',
25
+        description='plots a boxplot of a chosen feature for each group of a DatasetExperiment.',
19 26
         type="boxlot",
20
-        params.components=entity(name='Components to plot',
27
+        params_components=entity(name='Components to plot',
21 28
             value=c(1,2),
22 29
             type='numeric',
23 30
             description='the components to be plotted e.g. c(1,2) plots component 1 on the x axis and component 2 on the y axis.',
... ...
@@ -29,12 +36,12 @@ pca_correlation_plot<-setClass(
29 36
 
30 37
 #' @export
31 38
 #' @template chart_plot
32
-setMethod(f="chart.plot",
39
+setMethod(f="chart_plot",
33 40
     signature=c("pca_correlation_plot",'PCA'),
34 41
     definition=function(obj,dobj)
35 42
     {
36
-        opt=param.list(obj)
37
-        A=data.frame(x=output.value(dobj,'correlation')[,opt$components[1]],y=output.value(dobj,'correlation')[,opt$components[2]])
43
+        opt=param_list(obj)
44
+        A=data.frame(x=output_value(dobj,'correlation')[,opt$components[1]],y=output_value(dobj,'correlation')[,opt$components[2]])
38 45
         dat <- circleFun(c(0,0),2,npoints = 50)
39 46
 
40 47
         out=ggplot(data=A,aes_(x=~x,y=~y)) +
... ...
@@ -61,61 +68,68 @@ setMethod(f="chart.plot",
61 68
 #' @include PCA_class.R
62 69
 #' @examples
63 70
 #' C = pca_scores_plot()
64
-pca_scores_plot<-setClass(
71
+pca_scores_plot = function(...) {
72
+    out=.pca_scores_plot()
73
+    out=struct::.initialize_struct_class(out,...)
74
+    return(out)
75
+}
76
+
77
+
78
+.pca_scores_plot<-setClass(
65 79
     "pca_scores_plot",
66 80
     contains='chart',
67 81
     slots=c(
68 82
         # INPUTS
69
-        params.components='entity',
70
-        params.points_to_label='enum',
71
-        params.factor_name='entity',
72
-        params.ellipse='enum',
73
-        params.label_filter='entity',
74
-        params.groups='ANY', # will be deprecated
75
-        params.label_factor='entity',
76
-        params.label_size='entity'
83
+        params_components='entity',
84
+        params_points_to_label='enum',
85
+        params_factor_name='entity',
86
+        params_ellipse='enum',
87
+        params_label_filter='entity',
88
+        params_groups='ANY', # will be deprecated
89
+        params_label_factor='entity',
90
+        params_label_size='entity'
77 91
     ),
78 92
 
79 93
     prototype = list(name='PCA scores plot',
80 94
         description='Plots a 2d scatter plot of the selected components',
81 95
         type="scatter",
82 96
 
83
-        params.components=entity(name='Components to plot',
97
+        params_components=entity(name='Components to plot',
84 98
             value=c(1,2),
85 99
             type='numeric',
86 100
             description='the components to be plotted e.g. c(1,2) plots component 1 on the x axis and component 2 on the y axis.',
87 101
             max_length=2
88 102
         ),
89 103
 
90
-        params.points_to_label=enum(name='points_to_label',
104
+        params_points_to_label=enum(name='points_to_label',
91 105
             value='none',
92 106
             type='character',
93 107
             description='("none"), "all", or "outliers" will be labelled on the plot.',
94 108
             list=c('none','all','outliers')
95 109
         ),
96
-        params.factor_name=entity(name='Factor name',
110
+        params_factor_name=entity(name='Factor name',
97 111
             value='factor',
98 112
             type='character',
99 113
             description='The column name of sample meta to use for plotting. A second column can be included to plot using symbols.',
100 114
             max_length=2
101 115
         ),
102
-        params.ellipse=enum(name = 'Plot ellipses',description=c(
116
+        params_ellipse=enum(name = 'Plot ellipses',description=c(
103 117
             '"all" will plot all ellipses',
104 118
             '"group" will only plot group ellipses',
105 119
             '"none" will not plot any ellipses',
106 120
             '"sample" will plot ellipse for all samples (ignoring group)'),
107 121
             list=c('all','group','none','sample'),
108 122
             value='all'),
109
-        params.label_filter=entity(name='Label filter',
123
+        params_label_filter=entity(name='Label filter',
110 124
             value=character(0),
111 125
             type='character',
112
-            description='Only include the param.group labels included in params.label_filter. If zero length then all labels will be included.'
126
+            description='Only include the param.group labels included in params_label_filter. If zero length then all labels will be included.'
113 127
         ),
114
-        params.label_factor=entity(names='Factor for labels',
128
+        params_label_factor=entity(names='Factor for labels',
115 129
             description='The column name of sample_meta to use as labels. "rownames" will use the row names from sample_meta.',
116 130
             type='character',
117 131
             value='rownames'),
118
-        params.label_size=entity(names='Text size of labels',
132
+        params_label_size=entity(names='Text size of labels',
119 133
             description='The text size of labels. Note this is not in Font Units. Default 3.88.',
120 134
             type='numeric',
121 135
             value=3.88)
... ...
@@ -129,7 +143,7 @@ pca_scores_plot<-setClass(
129 143
 #' @importFrom scales squish
130 144
 #' @export
131 145
 #' @template chart_plot
132
-setMethod(f="chart.plot",
146
+setMethod(f="chart_plot",
133 147
     signature=c("pca_scores_plot",'PCA'),
134 148
     definition=function(obj,dobj)
135 149
     {
... ...
@@ -137,9 +151,9 @@ setMethod(f="chart.plot",
137 151
         if (obj$points_to_label=='outliers' & !(obj$ellipse %in% c('all','sample'))) {
138 152
             warning('Outliers are only labelled when plotting the sample ellipse')
139 153
         }
140
-        opt=param.list(obj)
141
-        scores=output.value(dobj,'scores')$data
142
-        pvar = (colSums(scores*scores)/output.value(dobj,'ssx'))*100 # percent variance
154
+        opt=param_list(obj)
155
+        scores=output_value(dobj,'scores')$data
156
+        pvar = (colSums(scores*scores)/output_value(dobj,'ssx'))*100 # percent variance
143 157
         pvar = round(pvar,digits = 2) # round to 2 decimal places
144 158
 
145 159
         if (length(obj$factor_name)==1) {
... ...
@@ -222,7 +236,7 @@ setMethod(f="chart.plot",
222 236
             build=ggplot_build(out)$data
223 237
             points=build[[1]]
224 238
             ell=build[[length(build)]]
225
-            # outlier for dataset ellipse
239
+            # outlier for DatasetExperiment ellipse
226 240
             points$in.ell=as.logical(sp::point.in.polygon(points$x,points$y,ell$x,ell$y))
227 241
 
228 242
             # label outliers if
... ...
@@ -262,55 +276,62 @@ setMethod(f="chart.plot",
262 276
 #' @include PCA_class.R
263 277
 #' @examples
264 278
 #' C = pca_biplot_plot()
265
-pca_biplot_plot<-setClass(
279
+pca_biplot_plot = function(...) {
280
+    out=.pca_biplot_plot()
281
+    out=struct::.initialize_struct_class(out,...)
282
+    return(out)
283
+}
284
+
285
+
286
+.pca_biplot_plot<-setClass(
266 287
     "pca_biplot_plot",
267 288
     contains='chart',
268 289
     slots=c(
269 290
         # INPUTS
270
-        params.components='entity',
271
-        params.points_to_label='entity',
272
-        params.factor_name='entity',
273
-        params.groups='entity',
274
-        params.scale_factor='entity',
275
-        params.style='enum',
276
-        params.label_features='entity'
291
+        params_components='entity',
292
+        params_points_to_label='entity',
293
+        params_factor_name='entity',
294
+        params_groups='entity',
295
+        params_scale_factor='entity',
296
+        params_style='enum',
297
+        params_label_features='entity'
277 298
     ),
278 299
     prototype = list(name='Feature boxplot',
279
-        description='plots a boxplot of a chosen feature for each group of a dataset.',
300
+        description='plots a boxplot of a chosen feature for each group of a DatasetExperiment.',
280 301
         type="boxlot",
281
-        params.components=entity(name='Components to plot',
302
+        params_components=entity(name='Components to plot',
282 303
             value=c(1,2),
283 304
             type='numeric',
284 305
             description='the components to be plotted e.g. c(1,2) plots component 1 on the x axis and component 2 on the y axis.',
285 306
             max_length=2
286 307
         ),
287
-        params.points_to_label=entity(name='points_to_label',
308
+        params_points_to_label=entity(name='points_to_label',
288 309
             value='none',
289 310
             type='character',
290 311
             description='("none"), "all", or "outliers" will be labelled on the plot.'
291 312
         ),
292
-        params.factor_name=entity(name='Factor name',
313
+        params_factor_name=entity(name='Factor name',
293 314
             value='factor',
294 315
             type='character',
295 316
             description='The name of the factor to be displayed on the plot. Appears on axis and legend titles, for example. By default the column name of the meta data will be used where possible.'
296 317
         ),
297
-        params.groups=entity(name='Groups',
318
+        params_groups=entity(name='Groups',
298 319
             value=factor(),
299 320
             type='factor',
300 321
             description='The name of the factor to be displayed on the plot. Appears on axis and legend titles, for example. By default the column name of the meta data will be used where possible.'
301 322
         ),
302
-        params.scale_factor=entity(name='Loadings scale factor',
323
+        params_scale_factor=entity(name='Loadings scale factor',
303 324
             value=0.95,
304 325
             type='numeric',
305 326
             description='Scaling factor to apply to loadings. Default = 0.95.'
306 327
         ),
307
-        params.style=enum(name='Plot style',
328
+        params_style=enum(name='Plot style',
308 329
             value='points',
309 330
             type='character',
310 331
             description='Named plot styles for the biplot. [points], arrows',
311 332
             list=c('points','arrows')
312 333
         ),
313
-        params.label_features=entity(name='Add feature labels',
334
+        params_label_features=entity(name='Add feature labels',
314 335
             value=FALSE,
315 336
             type='logical',
316 337
             description='Include feature labels on the plot'
... ...
@@ -321,19 +342,19 @@ pca_biplot_plot<-setClass(
321 342
 
322 343
 #' @export
323 344
 #' @template chart_plot
324
-setMethod(f="chart.plot",
345
+setMethod(f="chart_plot",
325 346
     signature=c("pca_biplot_plot",'PCA'),
326 347
     definition=function(obj,dobj)
327 348
     {
328
-        opt=param.list(obj)
329
-        Ts=output.value(dobj,'scores')$data
330
-        pvar=(colSums(Ts*Ts)/output.value(dobj,'ssx'))*100
349
+        opt=param_list(obj)
350
+        Ts=output_value(dobj,'scores')$data
351
+        pvar=(colSums(Ts*Ts)/output_value(dobj,'ssx'))*100
331 352
         pvar=round(pvar,digits = 1)
332 353
         xlabel=paste("PC",opt$components[[1]],' (',sprintf("%.1f",pvar[opt$components[[1]]]),'%)',sep='')
333 354
         ylabel=paste("PC",opt$components[[2]],' (',sprintf("%.1f",pvar[opt$components[[2]]]),'%)',sep='')
334 355
 
335
-        P=output.value(dobj,'loadings')
336
-        Ev=output.value(dobj,'eigenvalues')
356
+        P=output_value(dobj,'loadings')
357
+        Ev=output_value(dobj,'eigenvalues')
337 358
 
338 359
         # eigenvalues were square rooted when training PCA
339 360
         Ev=Ev[,1]
... ...
@@ -355,7 +376,7 @@ setMethod(f="chart.plot",
355 376
         # plot
356 377
         A=data.frame("x"=P[,opt$components[1]]*sf*0.8,"y"=P[,opt$components[2]]*sf*0.8)
357 378
         C=pca_scores_plot(groups=obj$groups,points_to_label=obj$points_to_label,components=obj$components,factor_name=obj$factor_name)
358
-        out=chart.plot(C,dobj)
379
+        out=chart_plot(C,dobj)
359 380
 
360 381
         if (opt$style=='points')
361 382
         {
... ...
@@ -404,31 +425,38 @@ setMethod(f="chart.plot",
404 425
 #' @include PCA_class.R
405 426
 #' @examples
406 427
 #' C = pca_loadings_plot()
407
-pca_loadings_plot<-setClass(
428
+pca_loadings_plot = function(...) {
429
+    out=.pca_loadings_plot()
430
+    out=struct::.initialize_struct_class(out,...)
431
+    return(out)
432
+}
433
+
434
+
435
+.pca_loadings_plot<-setClass(
408 436
     "pca_loadings_plot",
409 437
     contains='chart',
410 438
     slots=c(
411 439
         # INPUTS
412
-        params.components='entity',
413
-        params.style='enum',
414
-        params.label_features='entity'
440
+        params_components='entity',
441
+        params_style='enum',
442
+        params_label_features='entity'
415 443
     ),
416 444
     prototype = list(name='Feature boxplot',
417
-        description='plots a boxplot of a chosen feature for each group of a dataset.',
445
+        description='plots a boxplot of a chosen feature for each group of a DatasetExperiment.',
418 446
         type="boxlot",
419
-        params.components=entity(name='Components to plot',
447
+        params_components=entity(name='Components to plot',
420 448
             value=c(1,2),
421 449
             type='numeric',
422 450
             description='the components to be plotted e.g. c(1,2) plots component 1 on the x axis and component 2 on the y axis.',
423 451
             max_length=2
424 452
         ),
425
-        params.style=enum(name='Plot style',
453
+        params_style=enum(name='Plot style',
426 454
             value='points',
427 455
             type='character',
428 456
             description='Named plot styles for the biplot. [points], arrows',
429 457
             list=c('points','arrows')
430 458
         ),
431
-        params.label_features=entity(name='Add feature labels',
459
+        params_label_features=entity(name='Add feature labels',
432 460
             value=FALSE,
433 461
             type='logical',
434 462
             description='Include feature labels on the plot'
... ...
@@ -442,13 +470,13 @@ pca_loadings_plot<-setClass(
442 470
 
443 471
 #' @export
444 472
 #' @template chart_plot
445
-setMethod(f="chart.plot",
473
+setMethod(f="chart_plot",
446 474
     signature=c("pca_loadings_plot",'PCA'),
447 475
     definition=function(obj,dobj)
448 476
     {
449
-        opt=param.list(obj)
477
+        opt=param_list(obj)
450 478
 
451
-        P=output.value(dobj,'loadings')
479
+        P=output_value(dobj,'loadings')
452 480
         # 1D plot
453 481
         if (length(opt$components)==1)
454 482
         {
... ...
@@ -489,12 +517,19 @@ setMethod(f="chart.plot",
489 517
 #' line plot showing percent variance and cumulative peercent variance for the computed components.
490 518
 #'
491 519
 #' @import struct
492
-#' @export PCA.scree
520
+#' @export pca_scree
493 521
 #' @include PCA_class.R
494 522
 #' @examples
495
-#' C = PCA.scree()
496
-PCA.scree<-setClass(
497
-    "PCA.scree",
523
+#' C = pca_scree()
524
+pca_scree = function(...) {
525
+    out=.pca_scree()
526
+    out=struct::.initialize_struct_class(out,...)
527
+    return(out)
528
+}
529
+
530
+
531
+.pca_scree<-setClass(
532
+    "pca_scree",
498 533
     contains=c('chart'),
499 534
     prototype = list(name='Scree plot',
500 535
         description='plots the percent and cumulative percent variance for the calculated components',
... ...
@@ -504,13 +539,13 @@ PCA.scree<-setClass(
504 539
 
505 540
 #' @export
506 541
 #' @template chart_plot
507
-setMethod(f="chart.plot",
508
-    signature=c("PCA.scree",'PCA'),
542
+setMethod(f="chart_plot",
543
+    signature=c("pca_scree",'PCA'),
509 544
     definition=function(obj,dobj)
510 545
     {
511 546
         ## percent variance
512
-        scores=output.value(dobj,'scores')$data
513
-        pvar=(colSums(scores*scores)/output.value(dobj,'ssx'))*100
547
+        scores=output_value(dobj,'scores')$data
548
+        pvar=(colSums(scores*scores)/output_value(dobj,'ssx'))*100
514 549
         A=data.frame("x"=1:length(pvar),"y"=c(pvar,cumsum(pvar)),"Variance"=as.factor(c(rep('Single component',length(pvar)),rep('Cumulative',length(pvar)))))
515 550
         labels=round(A$y,digits = 1)
516 551
         labels=format(labels,1)
... ...
@@ -536,23 +571,30 @@ setMethod(f="chart.plot",
536 571
 #' line plot showing percent variance and cumulative peercent variance for the computed components.
537 572
 #'
538 573
 #' @import struct
539
-#' @export PCA.dstat
574
+#' @export PCA_dstat
540 575
 #' @include PCA_class.R
541 576
 #' @examples
542
-#' C = PCA.dstat()
543
-PCA.dstat<-setClass(
544
-    "PCA.dstat",
577
+#' C = PCA_dstat()
578
+PCA_dstat = function(...) {
579
+    out=.PCA_dstat()
580
+    out=struct::.initialize_struct_class(out,...)
581
+    return(out)
582
+}
583
+
584
+
585
+.PCA_dstat<-setClass(
586
+    "PCA_dstat",
545 587
     contains=c('chart'),
546
-    slots=c(params.number_components='entity',
547
-        params.alpha='entity'),
588
+    slots=c(params_number_components='entity',
589
+        params_alpha='entity'),
548 590
     prototype = list(name='d-statistic plot',
549 591
         description='a bar chart of the d-statistics for samples in the input PCA model',
550 592
         type="bar",
551
-        params.number_components=entity(value = 2,
593
+        params_number_components=entity(value = 2,
552 594
             name = 'number of principal components',
553 595
             description = 'number of principal components to use for the plot',
554 596
             type='numeric'),
555
-        params.alpha=entity(value=0.95,
597
+        params_alpha=entity(value=0.95,
556 598
             name='threshold for rejecting outliers',
557 599
             description='a confidence threshold for rejecting samples based on the d-statistic',
558 600
             type='numeric')
... ...
@@ -561,13 +603,13 @@ PCA.dstat<-setClass(
561 603
 
562 604
 #' @export
563 605
 #' @template chart_plot
564
-setMethod(f="chart.plot",
565
-    signature=c("PCA.dstat",'PCA'),
606
+setMethod(f="chart_plot",
607
+    signature=c("PCA_dstat",'PCA'),
566 608
     definition=function(obj,dobj)
567 609
     {
568
-        opt=param.list(obj)
569
-        a=param.value(obj,'number_components')
570
-        scores=output.value(dobj,'scores')$data
610
+        opt=param_list(obj)
611
+        a=param_value(obj,'number_components')
612
+        scores=output_value(dobj,'scores')$data
571 613
         I=nrow(scores)             # number of samples
572 614
         sample_names=rownames(scores)
573 615
         scores=scores[,1:a]
Browse code

use chart_plot roxygen template

grlloyd authored on 23/09/2019 12:53:18
Showing 1 changed files
... ...
@@ -28,6 +28,7 @@ pca_correlation_plot<-setClass(
28 28
 )
29 29
 
30 30
 #' @export
31
+#' @template chart_plot
31 32
 setMethod(f="chart.plot",
32 33
     signature=c("pca_correlation_plot",'PCA'),
33 34
     definition=function(obj,dobj)
... ...
@@ -127,6 +128,7 @@ pca_scores_plot<-setClass(
127 128
 #' @import ggplot2
128 129
 #' @importFrom scales squish
129 130
 #' @export
131
+#' @template chart_plot
130 132
 setMethod(f="chart.plot",
131 133
     signature=c("pca_scores_plot",'PCA'),
132 134
     definition=function(obj,dobj)
... ...
@@ -318,6 +320,7 @@ pca_biplot_plot<-setClass(
318 320
 )
319 321
 
320 322
 #' @export
323
+#' @template chart_plot
321 324
 setMethod(f="chart.plot",
322 325
     signature=c("pca_biplot_plot",'PCA'),
323 326
     definition=function(obj,dobj)
... ...
@@ -438,6 +441,7 @@ pca_loadings_plot<-setClass(
438 441
 
439 442
 
440 443
 #' @export
444
+#' @template chart_plot
441 445
 setMethod(f="chart.plot",
442 446
     signature=c("pca_loadings_plot",'PCA'),
443 447
     definition=function(obj,dobj)
... ...
@@ -499,6 +503,7 @@ PCA.scree<-setClass(
499 503
 )
500 504
 
501 505
 #' @export
506
+#' @template chart_plot
502 507
 setMethod(f="chart.plot",
503 508
     signature=c("PCA.scree",'PCA'),
504 509
     definition=function(obj,dobj)
... ...
@@ -555,6 +560,7 @@ PCA.dstat<-setClass(
555 560
 )
556 561
 
557 562
 #' @export
563
+#' @template chart_plot
558 564
 setMethod(f="chart.plot",
559 565
     signature=c("PCA.dstat",'PCA'),
560 566
     definition=function(obj,dobj)
Browse code

add functionality to PCA scores plot

grlloyd authored on 23/09/2019 12:22:42
Showing 1 changed files
... ...
@@ -70,7 +70,9 @@ pca_scores_plot<-setClass(
70 70
         params.factor_name='entity',
71 71
         params.ellipse='enum',
72 72
         params.label_filter='entity',
73
-        params.groups='ANY' # will be deprecated
73
+        params.groups='ANY', # will be deprecated
74
+        params.label_factor='entity',
75
+        params.label_size='entity'
74 76
     ),
75 77
 
76 78
     prototype = list(name='PCA scores plot',
... ...
@@ -93,7 +95,8 @@ pca_scores_plot<-setClass(
93 95
         params.factor_name=entity(name='Factor name',
94 96
             value='factor',
95 97
             type='character',
96
-            description='The column name of sample meta to use for plotting.'
98
+            description='The column name of sample meta to use for plotting. A second column can be included to plot using symbols.',
99
+            max_length=2
97 100
         ),
98 101
         params.ellipse=enum(name = 'Plot ellipses',description=c(
99 102
             '"all" will plot all ellipses',
... ...
@@ -106,7 +109,15 @@ pca_scores_plot<-setClass(
106 109
             value=character(0),
107 110
             type='character',
108 111
             description='Only include the param.group labels included in params.label_filter. If zero length then all labels will be included.'
109
-        )
112
+        ),
113
+        params.label_factor=entity(names='Factor for labels',
114
+            description='The column name of sample_meta to use as labels. "rownames" will use the row names from sample_meta.',
115
+            type='character',
116
+            value='rownames'),
117
+        params.label_size=entity(names='Text size of labels',
118
+            description='The text size of labels. Note this is not in Font Units. Default 3.88.',
119
+            type='numeric',
120
+            value=3.88)
110 121
     )
111 122
 )
112 123
 
... ...
@@ -126,10 +137,22 @@ setMethod(f="chart.plot",
126 137
         }
127 138
         opt=param.list(obj)
128 139
         scores=output.value(dobj,'scores')$data
129
-        pvar=(colSums(scores*scores)/output.value(dobj,'ssx'))*100 # percent variance
130
-        pvar=round(pvar,digits = 2) # round to 2 decimal places
131
-        shapes <- rep(19,nrow(scores)) # filled circles for all samples
132
-        slabels <- rownames(scores)
140
+        pvar = (colSums(scores*scores)/output.value(dobj,'ssx'))*100 # percent variance
141
+        pvar = round(pvar,digits = 2) # round to 2 decimal places
142
+
143
+        if (length(obj$factor_name)==1) {
144
+            shapes = 19 # filled circles for all samples
145
+        } else {
146
+            shapes = factor(dobj$scores$sample_meta[[obj$factor_name[2]]])
147
+        }
148
+
149
+        if (obj$label_factor=='rownames') {
150
+            slabels = rownames(dobj$scores$sample_meta)
151
+        } else {
152
+            slabels = dobj$scores$sample_meta[[obj$label_factor]]
153
+        }
154
+        opt$factor_name=opt$factor_name[[1]] # only use the first factor from now on
155
+
133 156
         x=scores[,opt$components[1]]
134 157
         y=scores[,opt$components[2]]
135 158
         xlabel=paste("PC",opt$components[[1]],' (',sprintf("%.1f",pvar[opt$components[[1]]]),'%)',sep='')
... ...
@@ -158,16 +181,27 @@ setMethod(f="chart.plot",
158 181
         # build the plot
159 182
         A <- data.frame (group=opt$groups,x=x, y=y)
160 183
 
161
-        out=ggplot (data=A, aes_(x=~x,y=~y,colour=~group,label=~slabels,shapes=~shapes)) +
184
+        if (length(obj$factor_name)==2) {
185
+            out=ggplot (data=A, aes_(x=~x,y=~y,colour=~group,label=~slabels,shape=~shapes))
186
+        }   else {
187
+            out=ggplot (data=A, aes_(x=~x,y=~y,colour=~group,label=~slabels))
188
+        }
189
+        out=out+
162 190
 
163 191
             geom_point(na.rm=TRUE) +
164 192
             xlab(xlabel) +
165 193
             ylab(ylabel) +
166
-            ggtitle('PCA Scores', subtitle=NULL) +
194
+            ggtitle('PCA Scores', subtitle=NULL)
167 195
 
168
-            if (obj$ellipse %in% c('all','group')) {
169
-                stat_ellipse(type='norm') # ellipse for individual groups
170
-            }
196
+        if (length(obj$factor_name)==2) {
197
+            out=out+labs(shape=obj$factor_name[[2]],colour=obj$factor_name[[1]])
198
+        } else {
199
+            out=out+labs(shape=obj$factor_name[[1]])
200
+        }
201
+
202
+        if (obj$ellipse %in% c('all','group')) {
203
+            out = out +stat_ellipse(type='norm') # ellipse for individual groups
204
+        }
171 205
 
172 206
         if (is(opt$groups,'factor')) { # if a factor then plot by group using the colours from pmp package
173 207
             out=out+scale_colour_manual(values=plotClass$manual_colors,name=opt$factor_name)
... ...
@@ -194,10 +228,9 @@ setMethod(f="chart.plot",
194 228
             {
195 229
                 if (!all(points$in.ell))
196 230
                 {
197
-
198 231
                     temp=subset(points,!points$in.ell)
199 232
                     temp$group=opt$groups[!points$in.ell]
200
-                    out=out+geom_text(data=temp,aes_(x=~x,y=~y,label=~label,colour=~group),vjust="inward",hjust="inward")
233
+                    out=out+geom_text(data=temp,aes_(x=~x,y=~y,label=~label,colour=~group),size=obj$label_size,vjust="inward",hjust="inward")
201 234
 
202 235
                 }
203 236
             }
... ...
@@ -248,7 +281,7 @@ pca_biplot_plot<-setClass(
248 281
             type='numeric',
249 282
             description='the components to be plotted e.g. c(1,2) plots component 1 on the x axis and component 2 on the y axis.',
250 283
             max_length=2
251
-            ),
284
+        ),
252 285
         params.points_to_label=entity(name='points_to_label',
253 286
             value='none',
254 287
             type='character',
Browse code

update 'type' and matching 'value' for prototype entity objects

grlloyd authored on 09/09/2019 14:35:21
Showing 1 changed files
... ...
@@ -20,7 +20,8 @@ pca_correlation_plot<-setClass(
20 20
         params.components=entity(name='Components to plot',
21 21
             value=c(1,2),
22 22
             type='numeric',
23
-            description='the components to be plotted e.g. c(1,2) plots component 1 on the x axis and component 2 on the y axis.'
23
+            description='the components to be plotted e.g. c(1,2) plots component 1 on the x axis and component 2 on the y axis.',
24
+            max_length=2
24 25
         )
25 26
 
26 27
     )
... ...
@@ -79,7 +80,8 @@ pca_scores_plot<-setClass(
79 80
         params.components=entity(name='Components to plot',
80 81
             value=c(1,2),
81 82
             type='numeric',
82
-            description='the components to be plotted e.g. c(1,2) plots component 1 on the x axis and component 2 on the y axis.'
83
+            description='the components to be plotted e.g. c(1,2) plots component 1 on the x axis and component 2 on the y axis.',
84
+            max_length=2
83 85
         ),
84 86
 
85 87
         params.points_to_label=enum(name='points_to_label',
... ...
@@ -244,8 +246,9 @@ pca_biplot_plot<-setClass(
244 246
         params.components=entity(name='Components to plot',
245 247
             value=c(1,2),
246 248
             type='numeric',
247
-            description='the components to be plotted e.g. c(1,2) plots component 1 on the x axis and component 2 on the y axis.'
248
-        ),
249
+            description='the components to be plotted e.g. c(1,2) plots component 1 on the x axis and component 2 on the y axis.',
250
+            max_length=2
251
+            ),
249 252
         params.points_to_label=entity(name='points_to_label',
250 253
             value='none',
251 254
             type='character',
... ...
@@ -380,7 +383,8 @@ pca_loadings_plot<-setClass(
380 383
         params.components=entity(name='Components to plot',
381 384
             value=c(1,2),
382 385
             type='numeric',
383
-            description='the components to be plotted e.g. c(1,2) plots component 1 on the x axis and component 2 on the y axis.'
386
+            description='the components to be plotted e.g. c(1,2) plots component 1 on the x axis and component 2 on the y axis.',
387
+            max_length=2
384 388
         ),
385 389
         params.style=enum(name='Plot style',
386 390
             value='points',
Browse code

fix broken vigettes

groups slot for pca scores is no longer usedis deprecated

grlloyd authored on 13/08/2019 09:00:10
Showing 1 changed files
... ...
@@ -69,7 +69,7 @@ pca_scores_plot<-setClass(
69 69
         params.factor_name='entity',
70 70
         params.ellipse='enum',
71 71
         params.label_filter='entity',
72
-        params.groups='factor'
72
+        params.groups='ANY' # will be deprecated
73 73
     ),
74 74
 
75 75
     prototype = list(name='PCA scores plot',
Browse code

change PCA scores type to dataset

Makes it similar to predicted output that and allows meta data to be kept with scores for plotting, further analysis etc.

grlloyd authored on 13/08/2019 08:30:35
Showing 1 changed files
... ...
@@ -67,9 +67,9 @@ pca_scores_plot<-setClass(
67 67
         params.components='entity',
68 68
         params.points_to_label='enum',
69 69
         params.factor_name='entity',
70
-        params.groups='entity',
71 70
         params.ellipse='enum',
72
-        params.label_filter='entity'
71
+        params.label_filter='entity',
72
+        params.groups='factor'
73 73
     ),
74 74
 
75 75
     prototype = list(name='PCA scores plot',
... ...
@@ -91,12 +91,7 @@ pca_scores_plot<-setClass(
91 91
         params.factor_name=entity(name='Factor name',
92 92
             value='factor',
93 93
             type='character',
94
-            description='The name of the factor to be displayed on the plot. Appears on axis and legend titles, for example. By default the column name of the meta data will be used where possible.'
95
-        ),
96
-        params.groups=entity(name='Groups',
97
-            value=factor(),
98
-            type='factor',
99
-            description='The name of the factor to be displayed on the plot. Appears on axis and legend titles, for example. By default the column name of the meta data will be used where possible.'
94
+            description='The column name of sample meta to use for plotting.'
100 95
         ),
101 96
         params.ellipse=enum(name = 'Plot ellipses',description=c(
102 97
             '"all" will plot all ellipses',
... ...
@@ -127,11 +122,8 @@ setMethod(f="chart.plot",
127 122
         if (obj$points_to_label=='outliers' & !(obj$ellipse %in% c('all','sample'))) {
128 123
             warning('Outliers are only labelled when plotting the sample ellipse')
129 124
         }
130
-
131
-
132 125
         opt=param.list(obj)
133
-
134
-        scores=output.value(dobj,'scores')
126
+        scores=output.value(dobj,'scores')$data
135 127
         pvar=(colSums(scores*scores)/output.value(dobj,'ssx'))*100 # percent variance
136 128
         pvar=round(pvar,digits = 2) # round to 2 decimal places
137 129
         shapes <- rep(19,nrow(scores)) # filled circles for all samples
... ...
@@ -141,6 +133,9 @@ setMethod(f="chart.plot",
141 133
         xlabel=paste("PC",opt$components[[1]],' (',sprintf("%.1f",pvar[opt$components[[1]]]),'%)',sep='')
142 134
         ylabel=paste("PC",opt$components[[2]],' (',sprintf("%.1f",pvar[opt$components[[2]]]),'%)',sep='')
143 135
 
136
+        # get the factor from meta data
137
+        opt$groups=dobj$scores$sample_meta[[opt$factor_name]]
138
+
144 139
         # add a space to the front of the labels to offset them from the points, because nudge_x is in data units
145 140
         for (i in 1:length(slabels))
146 141
         {
... ...
@@ -172,12 +167,10 @@ setMethod(f="chart.plot",
172 167
                 stat_ellipse(type='norm') # ellipse for individual groups
173 168
             }
174 169
 
175
-        if (is(opt$groups,'factor')) # if a factor then plot by group using the colours from pmp package
176
-        {
170
+        if (is(opt$groups,'factor')) { # if a factor then plot by group using the colours from pmp package
177 171
             out=out+scale_colour_manual(values=plotClass$manual_colors,name=opt$factor_name)
178 172
         }
179
-        else # assume continuous and use the default colour gradient
180
-        {
173
+        else {# assume continuous and use the default colour gradient
181 174
             out=out+scale_colour_viridis_c(limits=quantile(opt$groups,c(0.05,0.95),na.rm = TRUE),oob=squish,name=opt$factor_name)
182 175
         }
183 176
         out=out+theme_Publication(base_size = 12)
... ...
@@ -294,7 +287,7 @@ setMethod(f="chart.plot",
294 287
     definition=function(obj,dobj)
295 288
     {
296 289
         opt=param.list(obj)
297
-        Ts=output.value(dobj,'scores')
290
+        Ts=output.value(dobj,'scores')$data
298 291
         pvar=(colSums(Ts*Ts)/output.value(dobj,'ssx'))*100
299 292
         pvar=round(pvar,digits = 1)
300 293
         xlabel=paste("PC",opt$components[[1]],' (',sprintf("%.1f",pvar[opt$components[[1]]]),'%)',sep='')
... ...
@@ -318,7 +311,7 @@ setMethod(f="chart.plot",
318 311
         # additionaly scale the loadings
319 312
         sf=min(max(abs(Ts[,opt$components[1]]))/max(abs(P[,opt$components[1]])),
320 313
             max(abs(Ts[,opt$components[2]]))/max(abs(P[,opt$components[2]])))
321
-        dobj$scores=as.data.frame(Ts) # nb object not returned, so only temporary scaling
314
+        dobj$scores$data=as.data.frame(Ts) # nb object not returned, so only temporary scaling
322 315
 
323 316
         # plot
324 317
         A=data.frame("x"=P[,opt$components[1]]*sf*0.8,"y"=P[,opt$components[2]]*sf*0.8)
... ...
@@ -474,7 +467,7 @@ setMethod(f="chart.plot",
474 467
     definition=function(obj,dobj)
475 468
     {
476 469
         ## percent variance
477
-        scores=output.value(dobj,'scores')
470
+        scores=output.value(dobj,'scores')$data
478 471
         pvar=(colSums(scores*scores)/output.value(dobj,'ssx'))*100
479 472
         A=data.frame("x"=1:length(pvar),"y"=c(pvar,cumsum(pvar)),"Variance"=as.factor(c(rep('Single component',length(pvar)),rep('Cumulative',length(pvar)))))
480 473
         labels=round(A$y,digits = 1)
... ...
@@ -531,7 +524,7 @@ setMethod(f="chart.plot",
531 524
     {
532 525
         opt=param.list(obj)
533 526
         a=param.value(obj,'number_components')
534
-        scores=output.value(dobj,'scores')
527
+        scores=output.value(dobj,'scores')$data
535 528
         I=nrow(scores)             # number of samples
536 529
         sample_names=rownames(scores)
537 530
         scores=scores[,1:a]
Browse code

update documentation and minor bioccheck fixes

grlloyd authored on 05/07/2019 13:06:15
Showing 1 changed files
... ...
@@ -5,6 +5,8 @@
5 5
 #' @import struct
6 6
 #' @export pca_correlation_plot
7 7
 #' @include PCA_class.R
8
+#' @examples
9
+#' C = pca_correlation_plot()
8 10
 pca_correlation_plot<-setClass(
9 11
     "pca_correlation_plot",
10 12
     contains='chart',
... ...
@@ -55,6 +57,8 @@ setMethod(f="chart.plot",
55 57
 #' @import struct
56 58
 #' @export pca_scores_plot
57 59
 #' @include PCA_class.R
60
+#' @examples
61
+#' C = pca_scores_plot()
58 62
 pca_scores_plot<-setClass(
59 63
     "pca_scores_plot",
60 64
     contains='chart',
... ...
@@ -226,6 +230,8 @@ setMethod(f="chart.plot",
226 230
 #' @import struct
227 231
 #' @export pca_biplot_plot
228 232
 #' @include PCA_class.R
233
+#' @examples
234
+#' C = pca_biplot_plot()
229 235
 pca_biplot_plot<-setClass(
230 236
     "pca_biplot_plot",
231 237
     contains='chart',
... ...
@@ -364,6 +370,8 @@ setMethod(f="chart.plot",
364 370
 #' @import struct
365 371
 #' @export pca_loadings_plot
366 372
 #' @include PCA_class.R
373
+#' @examples
374
+#' C = pca_loadings_plot()
367 375
 pca_loadings_plot<-setClass(
368 376
     "pca_loadings_plot",
369 377
     contains='chart',
... ...
@@ -449,6 +457,8 @@ setMethod(f="chart.plot",
449 457
 #' @import struct
450 458
 #' @export PCA.scree
451 459
 #' @include PCA_class.R
460
+#' @examples
461
+#' C = PCA.scree()
452 462
 PCA.scree<-setClass(
453 463
     "PCA.scree",
454 464
     contains=c('chart'),
... ...
@@ -493,6 +503,8 @@ setMethod(f="chart.plot",
493 503
 #' @import struct
494 504
 #' @export PCA.dstat
495 505
 #' @include PCA_class.R
506
+#' @examples
507
+#' C = PCA.dstat()
496 508
 PCA.dstat<-setClass(
497 509
     "PCA.dstat",
498 510
     contains=c('chart'),
Browse code

change indentation for biocCheck

grlloyd authored on 24/05/2019 13:53:08
Showing 1 changed files
... ...
@@ -6,43 +6,43 @@
6 6
 #' @export pca_correlation_plot
7 7
 #' @include PCA_class.R
8 8
 pca_correlation_plot<-setClass(
9
-  "pca_correlation_plot",
10
-  contains='chart',
11
-  slots=c(
12
-    # INPUTS
13
-    params.components='entity'
14
-  ),
15
-  prototype = list(name='Feature boxplot',
16
-    description='plots a boxplot of a chosen feature for each group of a dataset.',
17
-    type="boxlot",
18
-    params.components=entity(name='Components to plot',
19
-      value=c(1,2),
20
-      type='numeric',
21
-      description='the components to be plotted e.g. c(1,2) plots component 1 on the x axis and component 2 on the y axis.'
22
-    )
9
+    "pca_correlation_plot",
10
+    contains='chart',
11
+    slots=c(
12
+        # INPUTS
13
+        params.components='entity'
14
+    ),
15
+    prototype = list(name='Feature boxplot',
16
+        description='plots a boxplot of a chosen feature for each group of a dataset.',
17
+        type="boxlot",
18
+        params.components=entity(name='Components to plot',
19
+            value=c(1,2),
20
+            type='numeric',
21
+            description='the components to be plotted e.g. c(1,2) plots component 1 on the x axis and component 2 on the y axis.'
22
+        )
23 23
 
24
-  )
24
+    )
25 25
 )
26 26
 
27 27
 #' @export
28 28
 setMethod(f="chart.plot",
29
-  signature=c("pca_correlation_plot",'PCA'),
30
-  definition=function(obj,dobj)
31
-  {
32
-    opt=param.list(obj)
33
-    A=data.frame(x=output.value(dobj,'correlation')[,opt$components[1]],y=output.value(dobj,'correlation')[,opt$components[2]])
34
-    dat <- circleFun(c(0,0),2,npoints = 50)
35
-
36
-    out=ggplot(data=A,aes_(x=~x,y=~y)) +
37
-      geom_point() +
38
-      scale_colour_Publication() +
39
-      theme_Publication(base_size = 12)+
40
-      coord_fixed(xlim = c(-1,1),ylim=c(-1,1)) +
41
-
42
-      geom_path(data=dat,aes_(x=~x,y=~y),inherit.aes = FALSE)
43
-
44
-    return(out)
45
-  }
29
+    signature=c("pca_correlation_plot",'PCA'),
30
+    definition=function(obj,dobj)
31
+    {
32
+        opt=param.list(obj)
33
+        A=data.frame(x=output.value(dobj,'correlation')[,opt$components[1]],y=output.value(dobj,'correlation')[,opt$components[2]])
34
+        dat <- circleFun(c(0,0),2,npoints = 50)
35
+
36
+        out=ggplot(data=A,aes_(x=~x,y=~y)) +
37
+            geom_point() +
38
+            scale_colour_Publication() +
39
+            theme_Publication(base_size = 12)+
40
+            coord_fixed(xlim = c(-1,1),ylim=c(-1,1)) +
41
+
42
+            geom_path(data=dat,aes_(x=~x,y=~y),inherit.aes = FALSE)
43
+
44
+        return(out)
45
+    }
46 46
 )
47 47
 
48 48
 #################################################
... ...
@@ -56,57 +56,57 @@ setMethod(f="chart.plot",
56 56
 #' @export pca_scores_plot
57 57
 #' @include PCA_class.R
58 58
 pca_scores_plot<-setClass(
59
-  "pca_scores_plot",
60
-  contains='chart',
61
-  slots=c(
62
-    # INPUTS
63
-    params.components='entity',
64
-    params.points_to_label='enum',
65
-    params.factor_name='entity',
66
-    params.groups='entity',
67
-    params.ellipse='enum',
68
-    params.label_filter='entity'
69
-  ),
70
-
71
-  prototype = list(name='PCA scores plot',
72
-    description='Plots a 2d scatter plot of the selected components',
73
-    type="scatter",
74
-
75
-    params.components=entity(name='Components to plot',
76
-      value=c(1,2),
77
-      type='numeric',
78
-      description='the components to be plotted e.g. c(1,2) plots component 1 on the x axis and component 2 on the y axis.'
59
+    "pca_scores_plot",
60
+    contains='chart',
61
+    slots=c(
62
+        # INPUTS
63
+        params.components='entity',
64
+        params.points_to_label='enum',
65
+        params.factor_name='entity',
66
+        params.groups='entity',
67
+        params.ellipse='enum',
68
+        params.label_filter='entity'
79 69
     ),
80 70
 
81
-    params.points_to_label=enum(name='points_to_label',
82
-      value='none',
83
-      type='character',
84
-      description='("none"), "all", or "outliers" will be labelled on the plot.',
85
-      list=c('none','all','outliers')
86
-    ),
87
-    params.factor_name=entity(name='Factor name',
88
-      value='factor',
89
-      type='character',
90
-      description='The name of the factor to be displayed on the plot. Appears on axis and legend titles, for example. By default the column name of the meta data will be used where possible.'
91
-    ),
92
-    params.groups=entity(name='Groups',
93
-      value=factor(),
94
-      type='factor',
95
-      description='The name of the factor to be displayed on the plot. Appears on axis and legend titles, for example. By default the column name of the meta data will be used where possible.'
96
-    ),
97
-    params.ellipse=enum(name = 'Plot ellipses',description=c(
98
-      '"all" will plot all ellipses',
99
-      '"group" will only plot group ellipses',
100
-      '"none" will not plot any ellipses',
101
-      '"sample" will plot ellipse for all samples (ignoring group)'),
102
-      list=c('all','group','none','sample'),
103
-      value='all'),
104
-    params.label_filter=entity(name='Label filter',
105
-      value=character(0),
106
-      type='character',
107
-      description='Only include the param.group labels included in params.label_filter. If zero length then all labels will be included.'
71
+    prototype = list(name='PCA scores plot',
72
+        description='Plots a 2d scatter plot of the selected components',
73
+        type="scatter",
74
+
75
+        params.components=entity(name='Components to plot',
76
+            value=c(1,2),
77
+            type='numeric',
78
+            description='the components to be plotted e.g. c(1,2) plots component 1 on the x axis and component 2 on the y axis.'
79
+        ),
80
+
81
+        params.points_to_label=enum(name='points_to_label',
82
+            value='none',
83
+            type='character',
84
+            description='("none"), "all", or "outliers" will be labelled on the plot.',
85
+            list=c('none','all','outliers')
86
+        ),
87
+        params.factor_name=entity(name='Factor name',
88
+            value='factor',
89
+            type='character',
90
+            description='The name of the factor to be displayed on the plot. Appears on axis and legend titles, for example. By default the column name of the meta data will be used where possible.'
91
+        ),
92
+        params.groups=entity(name='Groups',
93
+            value=factor(),
94
+            type='factor',
95
+            description='The name of the factor to be displayed on the plot. Appears on axis and legend titles, for example. By default the column name of the meta data will be used where possible.'
96
+        ),
97
+        params.ellipse=enum(name = 'Plot ellipses',description=c(
98
+            '"all" will plot all ellipses',
99
+            '"group" will only plot group ellipses',
100
+            '"none" will not plot any ellipses',
101
+            '"sample" will plot ellipse for all samples (ignoring group)'),
102
+            list=c('all','group','none','sample'),
103
+            value='all'),
104
+        params.label_filter=entity(name='Label filter',
105
+            value=character(0),
106
+            type='character',
107
+            description='Only include the param.group labels included in params.label_filter. If zero length then all labels will be included.'
108
+        )
108 109
     )
109
-  )
110 110
 )
111 111
 
112 112
 
... ...
@@ -116,104 +116,104 @@ pca_scores_plot<-setClass(
116 116
 #' @importFrom scales squish
117 117
 #' @export
118 118
 setMethod(f="chart.plot",
119
-  signature=c("pca_scores_plot",'PCA'),
120
-  definition=function(obj,dobj)
121
-  {
122
-
123
-    if (obj$points_to_label=='outliers' & !(obj$ellipse %in% c('all','sample'))) {
124
-      warning('Outliers are only labelled when plotting the sample ellipse')
125
-    }
119
+    signature=c("pca_scores_plot",'PCA'),
120
+    definition=function(obj,dobj)
121
+    {
126 122
 
123
+        if (obj$points_to_label=='outliers' & !(obj$ellipse %in% c('all','sample'))) {
124
+            warning('Outliers are only labelled when plotting the sample ellipse')
125
+        }
127 126
 
128
-    opt=param.list(obj)
129 127
 
130
-    scores=output.value(dobj,'scores')
131
-    pvar=(colSums(scores*scores)/output.value(dobj,'ssx'))*100 # percent variance
132
-    pvar=round(pvar,digits = 2) # round to 2 decimal places
133
-    shapes <- rep(19,nrow(scores)) # filled circles for all samples
134
-    slabels <- rownames(scores)
135
-    x=scores[,opt$components[1]]
136
-    y=scores[,opt$components[2]]
137
-    xlabel=paste("PC",opt$components[[1]],' (',sprintf("%.1f",pvar[opt$components[[1]]]),'%)',sep='')
138
-    ylabel=paste("PC",opt$components[[2]],' (',sprintf("%.1f",pvar[opt$components[[2]]]),'%)',sep='')
128
+        opt=param.list(obj)
139 129
 
140
-    # add a space to the front of the labels to offset them from the points, because nudge_x is in data units
141
-    for (i in 1:length(slabels))
142
-    {
143
-      slabels[i]=paste0('  ',slabels[i], '  ')
144
-    }
130
+        scores=output.value(dobj,'scores')
131
+        pvar=(colSums(scores*scores)/output.value(dobj,'ssx'))*100 # percent variance
132
+        pvar=round(pvar,digits = 2) # round to 2 decimal places
133
+        shapes <- rep(19,nrow(scores)) # filled circles for all samples
134
+        slabels <- rownames(scores)
135
+        x=scores[,opt$components[1]]
136
+        y=scores[,opt$components[2]]
137
+        xlabel=paste("PC",opt$components[[1]],' (',sprintf("%.1f",pvar[opt$components[[1]]]),'%)',sep='')
138
+        ylabel=paste("PC",opt$components[[2]],' (',sprintf("%.1f",pvar[opt$components[[2]]]),'%)',sep='')
145 139
 
146
-    # filter by label_filter list if provided
147
-    if (length(obj$label_filter)>0) {
148
-      out=!(opt$groups %in% obj$label_filter)
149
-      slabels[out]=''
150
-    }
140
+        # add a space to the front of the labels to offset them from the points, because nudge_x is in data units
141
+        for (i in 1:length(slabels))
142
+        {
143
+            slabels[i]=paste0('  ',slabels[i], '  ')
144
+        }
151 145
 
152
-    if (is(opt$groups,'factor')) {
153
-      plotClass= createClassAndColors(opt$groups)
154
-      opt$groups=plotClass$class
155
-    }
146
+        # filter by label_filter list if provided
147
+        if (length(obj$label_filter)>0) {
148
+            out=!(opt$groups %in% obj$label_filter)
149
+            slabels[out]=''
150
+        }
156 151
 
157
-    # build the plot
158
-    A <- data.frame (group=opt$groups,x=x, y=y)
152
+        if (is(opt$groups,'factor')) {
153
+            plotClass= createClassAndColors(opt$groups)
154
+            opt$groups=plotClass$class
155
+        }
159 156
 
160
-    out=ggplot (data=A, aes_(x=~x,y=~y,colour=~group,label=~slabels,shapes=~shapes)) +
157
+        # build the plot
158
+        A <- data.frame (group=opt$groups,x=x, y=y)
161 159
 
162
-      geom_point(na.rm=TRUE) +
163
-      xlab(xlabel) +
164
-      ylab(ylabel) +
165
-      ggtitle('PCA Scores', subtitle=NULL) +
160
+        out=ggplot (data=A, aes_(x=~x,y=~y,colour=~group,label=~slabels,shapes=~shapes)) +
166 161
 
167
-      if (obj$ellipse %in% c('all','group')) {
168
-        stat_ellipse(type='norm') # ellipse for individual groups
169
-      }
162
+            geom_point(na.rm=TRUE) +
163
+            xlab(xlabel) +
164
+            ylab(ylabel) +
165
+            ggtitle('PCA Scores', subtitle=NULL) +
170 166
 
171
-    if (is(opt$groups,'factor')) # if a factor then plot by group using the colours from pmp package
172
-    {
173
-      out=out+scale_colour_manual(values=plotClass$manual_colors,name=opt$factor_name)
174
-    }
175
-    else # assume continuous and use the default colour gradient
176
-    {
177
-      out=out+scale_colour_viridis_c(limits=quantile(opt$groups,c(0.05,0.95),na.rm = TRUE),oob=squish,name=opt$factor_name)
178
-    }
179
-    out=out+theme_Publication(base_size = 12)
180
-    # add ellipse for all samples (ignoring group)
181
-    if (obj$ellipse %in% c('all','sample')) {
182
-      out=out+stat_ellipse(type='norm',mapping=aes(x=x,y=y),colour="#C0C0C0",linetype='dashed',data=A)
183
-    }
167
+            if (obj$ellipse %in% c('all','group')) {
168
+                stat_ellipse(type='norm') # ellipse for individual groups
169
+            }
184 170
 
185
-    if (obj$ellipse %in% c('all','sample')) { # only do this if we plotted the sample ellipse
186
-      # identify samples outside the ellipse
187
-      build=ggplot_build(out)$data
188
-      points=build[[1]]
189
-      ell=build[[length(build)]]
190
-      # outlier for dataset ellipse
191
-      points$in.ell=as.logical(sp::point.in.polygon(points$x,points$y,ell$x,ell$y))
192
-
193
-      # label outliers if
194
-      if (opt$points_to_label=='outliers')
195
-      {
196
-        if (!all(points$in.ell))
171
+        if (is(opt$groups,'factor')) # if a factor then plot by group using the colours from pmp package
172
+        {
173
+            out=out+scale_colour_manual(values=plotClass$manual_colors,name=opt$factor_name)
174
+        }
175
+        else # assume continuous and use the default colour gradient
197 176
         {
177
+            out=out+scale_colour_viridis_c(limits=quantile(opt$groups,c(0.05,0.95),na.rm = TRUE),oob=squish,name=opt$factor_name)
178
+        }
179
+        out=out+theme_Publication(base_size = 12)
180
+        # add ellipse for all samples (ignoring group)
181
+        if (obj$ellipse %in% c('all','sample')) {
182
+            out=out+stat_ellipse(type='norm',mapping=aes(x=x,y=y),colour="#C0C0C0",linetype='dashed',data=A)
183
+        }
198 184
 
199
-          temp=subset(points,!points$in.ell)
200
-          temp$group=opt$groups[!points$in.ell]
201
-          out=out+geom_text(data=temp,aes_(x=~x,y=~y,label=~label,colour=~group),vjust="inward",hjust="inward")
185
+        if (obj$ellipse %in% c('all','sample')) { # only do this if we plotted the sample ellipse
186
+            # identify samples outside the ellipse
187
+            build=ggplot_build(out)$data
188
+            points=build[[1]]
189
+            ell=build[[length(build)]]
190
+            # outlier for dataset ellipse
191
+            points$in.ell=as.logical(sp::point.in.polygon(points$x,points$y,ell$x,ell$y))
192
+
193
+            # label outliers if
194
+            if (opt$points_to_label=='outliers')
195
+            {
196
+                if (!all(points$in.ell))
197
+                {
198
+
199
+                    temp=subset(points,!points$in.ell)
200
+                    temp$group=opt$groups[!points$in.ell]
201
+                    out=out+geom_text(data=temp,aes_(x=~x,y=~y,label=~label,colour=~group),vjust="inward",hjust="inward")
202
+
203
+                }
204
+            }
205
+            # add a list of outliers to the plot object
206
+            out$outliers=trimws(slabels[!points$in.ell])
207
+        }
202 208
 
209
+        # label all points if requested
210
+        if (opt$points_to_label=='all')
211
+        {
212
+            out=out+geom_text(vjust="inward",hjust="inward")
203 213
         }
204
-      }
205
-      # add a list of outliers to the plot object
206
-      out$outliers=trimws(slabels[!points$in.ell])
207
-    }
208 214
 
209
-    # label all points if requested
210
-    if (opt$points_to_label=='all')
211
-    {
212
-      out=out+geom_text(vjust="inward",hjust="inward")
215
+        return(out)
213 216
     }
214
-
215
-    return(out)
216
-  }
217 217
 )
218 218
 
219 219
 #################################################################
... ...
@@ -227,129 +227,129 @@ setMethod(f="chart.plot",
227 227
 #' @export pca_biplot_plot
228 228
 #' @include PCA_class.R
229 229
 pca_biplot_plot<-setClass(
230
-  "pca_biplot_plot",
231
-  contains='chart',
232
-  slots=c(
233
-    # INPUTS
234
-    params.components='entity',
235
-    params.points_to_label='entity',
236
-    params.factor_name='entity',
237
-    params.groups='entity',
238
-    params.scale_factor='entity',
239
-    params.style='enum',
240
-    params.label_features='entity'
241
-  ),
242
-  prototype = list(name='Feature boxplot',
243
-    description='plots a boxplot of a chosen feature for each group of a dataset.',
244
-    type="boxlot",
245
-    params.components=entity(name='Components to plot',
246
-      value=c(1,2),
247
-      type='numeric',
248
-      description='the components to be plotted e.g. c(1,2) plots component 1 on the x axis and component 2 on the y axis.'
249
-    ),
250
-    params.points_to_label=entity(name='points_to_label',
251
-      value='none',
252
-      type='character',
253
-      description='("none"), "all", or "outliers" will be labelled on the plot.'
254
-    ),
255
-    params.factor_name=entity(name='Factor name',
256
-      value='factor',
257
-      type='character',
258
-      description='The name of the factor to be displayed on the plot. Appears on axis and legend titles, for example. By default the column name of the meta data will be used where possible.'
259
-    ),
260
-    params.groups=entity(name='Groups',
261
-      value=factor(),
262
-      type='factor',
263
-      description='The name of the factor to be displayed on the plot. Appears on axis and legend titles, for example. By default the column name of the meta data will be used where possible.'
264
-    ),
265
-    params.scale_factor=entity(name='Loadings scale factor',
266
-      value=0.95,
267
-      type='numeric',
268
-      description='Scaling factor to apply to loadings. Default = 0.95.'
230
+    "pca_biplot_plot",
231
+    contains='chart',
232
+    slots=c(
233
+        # INPUTS
234
+        params.components='entity',
235
+        params.points_to_label='entity',
236
+        params.factor_name='entity',
237
+        params.groups='entity',
238
+        params.scale_factor='entity',
239
+        params.style='enum',
240
+        params.label_features='entity'
269 241
     ),
270
-    params.style=enum(name='Plot style',
271
-      value='points',
272
-      type='character',
273
-      description='Named plot styles for the biplot. [points], arrows',
274
-      list=c('points','arrows')
275
-    ),
276
-    params.label_features=entity(name='Add feature labels',
277
-      value=FALSE,
278
-      type='logical',
279
-      description='Include feature labels on the plot'
242
+    prototype = list(name='Feature boxplot',
243
+        description='plots a boxplot of a chosen feature for each group of a dataset.',
244
+        type="boxlot",
245
+        params.components=entity(name='Components to plot',
246
+            value=c(1,2),
247
+            type='numeric',
248
+            description='the components to be plotted e.g. c(1,2) plots component 1 on the x axis and component 2 on the y axis.'
249
+        ),
250
+        params.points_to_label=entity(name='points_to_label',
251
+            value='none',
252
+            type='character',
253
+            description='("none"), "all", or "outliers" will be labelled on the plot.'
254
+        ),
255
+        params.factor_name=entity(name='Factor name',
256
+            value='factor',
257
+            type='character',
258
+            description='The name of the factor to be displayed on the plot. Appears on axis and legend titles, for example. By default the column name of the meta data will be used where possible.'
259
+        ),
260
+        params.groups=entity(name='Groups',
261
+            value=factor(),
262
+            type='factor',
263
+            description='The name of the factor to be displayed on the plot. Appears on axis and legend titles, for example. By default the column name of the meta data will be used where possible.'
264
+        ),
265
+        params.scale_factor=entity(name='Loadings scale factor',
266
+            value=0.95,
267
+            type='numeric',
268
+            description='Scaling factor to apply to loadings. Default = 0.95.'
269
+        ),
270
+        params.style=enum(name='Plot style',
271
+            value='points',
272
+            type='character',
273
+            description='Named plot styles for the biplot. [points], arrows',
274
+            list=c('points','arrows')
275
+        ),
276
+        params.label_features=entity(name='Add feature labels',
277
+            value=FALSE,
278
+            type='logical',
279
+            description='Include feature labels on the plot'
280
+        )
280 281
     )
281
-  )
282 282
 
283 283
 )
284 284
 
285 285
 #' @export
286 286
 setMethod(f="chart.plot",
287
-  signature=c("pca_biplot_plot",'PCA'),
288
-  definition=function(obj,dobj)
289
-  {
290
-    opt=param.list(obj)
291
-    Ts=output.value(dobj,'scores')
292
-    pvar=(colSums(Ts*Ts)/output.value(dobj,'ssx'))*100
293
-    pvar=round(pvar,digits = 1)
294
-    xlabel=paste("PC",opt$components[[1]],' (',sprintf("%.1f",pvar[opt$components[[1]]]),'%)',sep='')
295
-    ylabel=paste("PC",opt$components[[2]],' (',sprintf("%.1f",pvar[opt$components[[2]]]),'%)',sep='')
296
-
297
-    P=output.value(dobj,'loadings')
298
-    Ev=output.value(dobj,'eigenvalues')
299
-
300
-    # eigenvalues were square rooted when training PCA
301
-    Ev=Ev[,1]
302
-    Ev=Ev^2
303
-
304
-    ## unscale the scores
305
-    #ev are the norms of scores
306
-    Ts=as.matrix(Ts) %*% diag(1/Ev) # these are normalised scores
307
-
308
-    # scale scores and loadings by alpha
309
-    Ts=Ts %*% diag(Ev^(1-opt$scale_factor))
310
-    P=as.matrix(P) %*% diag(Ev^(opt$scale_factor))
311
-
312
-    # additionaly scale the loadings
313
-    sf=min(max(abs(Ts[,opt$components[1]]))/max(abs(P[,opt$components[1]])),
314
-      max(abs(Ts[,opt$components[2]]))/max(abs(P[,opt$components[2]])))
315
-    dobj$scores=as.data.frame(Ts) # nb object not returned, so only temporary scaling
316
-
317
-    # plot
318
-    A=data.frame("x"=P[,opt$components[1]]*sf*0.8,"y"=P[,opt$components[2]]*sf*0.8)
319
-    C=pca_scores_plot(groups=obj$groups,points_to_label=obj$points_to_label,components=obj$components,factor_name=obj$factor_name)
320
-    out=chart.plot(C,dobj)
321
-
322
-    if (opt$style=='points')
323
-    {
324
-      out=out+
325
-        geom_point(data=A,inherit.aes = FALSE,color='black',mapping = aes_(x=~x,y=~y))
326
-    }
327
-    if (opt$style=='arrows')
287
+    signature=c("pca_biplot_plot",'PCA'),
288
+    definition=function(obj,dobj)
328 289
     {
329
-      out=out+
290
+        opt=param.list(obj)
291
+        Ts=output.value(dobj,'scores')
292
+        pvar=(colSums(Ts*Ts)/output.value(dobj,'ssx'))*100
293
+        pvar=round(pvar,digits = 1)
294
+        xlabel=paste("PC",opt$components[[1]],' (',sprintf("%.1f",pvar[opt$components[[1]]]),'%)',sep='')
295
+        ylabel=paste("PC",opt$components[[2]],' (',sprintf("%.1f",pvar[opt$components[[2]]]),'%)',sep='')
296
+
297
+        P=output.value(dobj,'loadings')
298
+        Ev=output.value(dobj,'eigenvalues')
299
+
300
+        # eigenvalues were square rooted when training PCA
301
+        Ev=Ev[,1]
302
+        Ev=Ev^2
303
+
304
+        ## unscale the scores
305
+        #ev are the norms of scores
306
+        Ts=as.matrix(Ts) %*% diag(1/Ev) # these are normalised scores
307
+
308
+        # scale scores and loadings by alpha
309
+        Ts=Ts %*% diag(Ev^(1-opt$scale_factor))
310
+        P=as.matrix(P) %*% diag(Ev^(opt$scale_factor))
311
+
312
+        # additionaly scale the loadings
313
+        sf=min(max(abs(Ts[,opt$components[1]]))/max(abs(P[,opt$components[1]])),
314
+            max(abs(Ts[,opt$components[2]]))/max(abs(P[,opt$components[2]])))
315
+        dobj$scores=as.data.frame(Ts) # nb object not returned, so only temporary scaling
316
+
317
+        # plot
318
+        A=data.frame("x"=P[,opt$components[1]]*sf*0.8,"y"=P[,opt$components[2]]*sf*0.8)
319
+        C=pca_scores_plot(groups=obj$groups,points_to_label=obj$points_to_label,components=obj$components,factor_name=obj$factor_name)
320
+        out=chart.plot(C,dobj)
321
+
322
+        if (opt$style=='points')
323
+        {
324
+            out=out+
325
+                geom_point(data=A,inherit.aes = FALSE,color='black',mapping = aes_(x=~x,y=~y))
326
+        }
327
+        if (opt$style=='arrows')
328
+        {
329
+            out=out+
330 330
 
331
-        geom_segment(data=A,inherit.aes = FALSE,color='black',mapping = aes_(x=~0,y=~0,xend=~x,yend=~y),arrow=arrow(length=unit(8,'points')))
331
+                geom_segment(data=A,inherit.aes = FALSE,color='black',mapping = aes_(x=~0,y=~0,xend=~x,yend=~y),arrow=arrow(length=unit(8,'points')))
332 332
 
333
-    }
334
-    out=out+ggtitle('PCA biplot', subtitle=NULL) +
335
-      xlab(xlabel) + ylab(ylabel)
333
+        }
334
+        out=out+ggtitle('PCA biplot', subtitle=NULL) +
335
+            xlab(xlabel) + ylab(ylabel)
336 336
 
337
-    #label features if requested
338
-    if (opt$label_features)
339
-    {
340
-      vlabels=rownames(dobj$loadings)
341
-      for (i in 1:length(vlabels))
342
-      {
343
-        vlabels[i]=paste0('  ',vlabels[i], '  ')
344
-      }
345
-      A$vlabels=vlabels
346
-      out=out+
337
+        #label features if requested
338
+        if (opt$label_features)
339
+        {
340
+            vlabels=rownames(dobj$loadings)
341
+            for (i in 1:length(vlabels))
342
+            {
343
+                vlabels[i]=paste0('  ',vlabels[i], '  ')
344
+            }
345
+            A$vlabels=vlabels
346
+            out=out+
347 347
 
348
-        geom_text(data=A,aes_(x=~x,y=~y,label=~vlabels),vjust="inward",hjust="inward",inherit.aes = FALSE)
348
+                geom_text(data=A,aes_(x=~x,y=~y,label=~vlabels),vjust="inward",hjust="inward",inherit.aes = FALSE)
349 349
 
350
+        }
351
+        return(out)
350 352
     }
351
-    return(out)
352
-  }
353 353
 )
354 354
 
355 355
 
... ...
@@ -365,34 +365,34 @@ setMethod(f="chart.plot",
365 365
 #' @export pca_loadings_plot
366 366
 #' @include PCA_class.R
367 367
 pca_loadings_plot<-setClass(
368
-  "pca_loadings_plot",
369
-  contains='chart',
370
-  slots=c(
371
-    # INPUTS
372
-    params.components='entity',
373
-    params.style='enum',
374
-    params.label_features='entity'
375
-  ),
376
-  prototype = list(name='Feature boxplot',
377
-    description='plots a boxplot of a chosen feature for each group of a dataset.',
378
-    type="boxlot",
379
-    params.components=entity(name='Components to plot',
380
-      value=c(1,2),
381
-      type='numeric',
382
-      description='the components to be plotted e.g. c(1,2) plots component 1 on the x axis and component 2 on the y axis.'
368
+    "pca_loadings_plot",
369
+    contains='chart',
370
+    slots=c(
371
+        # INPUTS
372
+        params.components='entity',
373
+        params.style='enum',
374
+        params.label_features='entity'
383 375
     ),
384
-    params.style=enum(name='Plot style',
385
-      value='points',
386
-      type='character',
387
-      description='Named plot styles for the biplot. [points], arrows',
388
-      list=c('points','arrows')
389
-    ),
390
-    params.label_features=entity(name='Add feature labels',
391
-      value=FALSE,
392
-      type='logical',
393
-      description='Include feature labels on the plot'
376
+    prototype = list(name='Feature boxplot',
377
+        description='plots a boxplot of a chosen feature for each group of a dataset.',
378
+        type="boxlot",
379
+        params.components=entity(name='Components to plot',
380
+            value=c(1,2),
381
+            type='numeric',
382
+            description='the components to be plotted e.g. c(1,2) plots component 1 on the x axis and component 2 on the y axis.'
383
+        ),
384
+        params.style=enum(name='Plot style',
385
+            value='points',
386
+            type='character',
387
+            description='Named plot styles for the biplot. [points], arrows',
388
+            list=c('points','arrows')
389
+        ),
390
+        params.label_features=entity(name='Add feature labels',
391
+            value=FALSE,
392
+            type='logical',
393
+            description='Include feature labels on the plot'
394
+        )
394 395
     )
395
-  )
396 396
 
397 397
 )
398 398
 
... ...
@@ -401,44 +401,44 @@ pca_loadings_plot<-setClass(
401 401
 
402 402
 #' @export
403 403
 setMethod(f="chart.plot",
404
-  signature=c("pca_loadings_plot",'PCA'),
405
-  definition=function(obj,dobj)
406
-  {
407
-    opt=param.list(obj)
408
-
409
-    P=output.value(dobj,'loadings')
410
-    # 1D plot
411
-    if (length(opt$components)==1)
412
-    {
413
-      A=data.frame("x"=1:nrow(P),"y"=P[,opt$components[1]])
414
-      out=ggplot(data=A,aes_(x=~x,y=~y)) +
415
-        geom_line() +
416
-        ggtitle('PCA Loadings', subtitle=NULL) +
417
-        xlab('Feature') +
418
-        ylab('Loading') +
419
-        scale_colour_Publication() +
420
-        theme_Publication(base_size = 12)
421
-      return(out)
422
-    }
423
-    # 2D plot
424
-    if (length(opt$components)==2)
425
-    {
426
-      A=data.frame("x"=P[,opt$components[1]],"y"=P[,opt$components[2]])
427
-      out=ggplot(data=A,aes_(x=~x,y=~y)) +
428
-        geom_point() +
429
-        ggtitle('PCA Loadings', subtitle=NULL) +
430
-        xlab(paste0('Component ',opt$components[1])) +
431
-        ylab(paste0('Component ',opt$components[2])) +
432
-        scale_colour_Publication() +
433
-        theme_Publication(base_size = 12)
434
-      return(out)
435
-    }
436
-    if (length(opt$components)>2)
404
+    signature=c("pca_loadings_plot",'PCA'),
405
+    definition=function(obj,dobj)
437 406
     {
438
-      stop('can only plot loadings for 1 or 2 components at a time')
439
-    }
407
+        opt=param.list(obj)
408
+
409
+        P=output.value(dobj,'loadings')
410
+        # 1D plot
411
+        if (length(opt$components)==1)
412
+        {
413
+            A=data.frame("x"=1:nrow(P),"y"=P[,opt$components[1]])
414
+            out=ggplot(data=A,aes_(x=~x,y=~y)) +
415
+                geom_line() +
416
+                ggtitle('PCA Loadings', subtitle=NULL) +
417
+                xlab('Feature') +
418
+                ylab('Loading') +
419
+                scale_colour_Publication() +
420
+                theme_Publication(base_size = 12)
421
+            return(out)
422
+        }
423
+        # 2D plot
424
+        if (length(opt$components)==2)
425
+        {
426
+            A=data.frame("x"=P[,opt$components[1]],"y"=P[,opt$components[2]])
427
+            out=ggplot(data=A,aes_(x=~x,y=~y)) +
428
+                geom_point() +
429
+                ggtitle('PCA Loadings', subtitle=NULL) +
430
+                xlab(paste0('Component ',opt$components[1])) +
431
+                ylab(paste0('Component ',opt$components[2])) +
432
+                scale_colour_Publication() +
433
+                theme_Publication(base_size = 12)
434
+            return(out)
435
+        }
436
+        if (length(opt$components)>2)
437
+        {
438
+            stop('can only plot loadings for 1 or 2 components at a time')
439
+        }
440 440
 
441
-  }
441
+    }
442 442
 )
443 443
 
444 444
 
... ...
@@ -450,40 +450,40 @@ setMethod(f="chart.plot",
450 450
 #' @export PCA.scree
451 451
 #' @include PCA_class.R
452 452
 PCA.scree<-setClass(
453
-  "PCA.scree",
454
-  contains=c('chart'),
455
-  prototype = list(name='Scree plot',
456
-    description='plots the percent and cumulative percent variance for the calculated components',
457
-    type="line"
458
-  )
453
+    "PCA.scree",
454
+    contains=c('chart'),
455
+    prototype = list(name='Scree plot',
456
+        description='plots the percent and cumulative percent variance for the calculated components',
457
+        type="line"
458
+    )
459 459
 )
460 460
 
461 461
 #' @export
462 462
 setMethod(f="chart.plot",
463
-  signature=c("PCA.scree",'PCA'),
464
-  definition=function(obj,dobj)
465
-  {
466
-    ## percent variance
467
-    scores=output.value(dobj,'scores')
468
-    pvar=(colSums(scores*scores)/output.value(dobj,'ssx'))*100
469
-    A=data.frame("x"=1:length(pvar),"y"=c(pvar,cumsum(pvar)),"Variance"=as.factor(c(rep('Single component',length(pvar)),rep('Cumulative',length(pvar)))))
470
-    labels=round(A$y,digits = 1)
471
-    labels=format(labels,1)
472
-
473
-    out=ggplot(data=A, aes_(x=~x,y=~y,color=~Variance)) +
474
-      geom_line() +
475
-      geom_point() +
476
-      geom_text(aes_(label=~labels),color='black',vjust=0,nudge_y = 5) +
477
-
478
-      ggtitle('Scree Plot', subtitle=NULL) +
479
-      xlab('Component') +
480
-      ylab('Variance (%)') +
481
-      scale_colour_Publication() +
482
-      scale_x_continuous(breaks=0:length(pvar)) +
483
-      guides(color=guide_legend("")) +
484
-      theme_Publication(base_size = 12)
485
-    return(out)
486
-  }
463
+    signature=c("PCA.scree",'PCA'),
464
+    definition=function(obj,dobj)
465
+    {
466
+        ## percent variance
467
+        scores=output.value(dobj,'scores')
468
+        pvar=(colSums(scores*scores)/output.value(dobj,'ssx'))*100
469
+        A=data.frame("x"=1:length(pvar),"y"=c(pvar,cumsum(pvar)),"Variance"=as.factor(c(rep('Single component',length(pvar)),rep('Cumulative',length(pvar)))))
470
+        labels=round(A$y,digits = 1)
471
+        labels=format(labels,1)
472
+
473
+        out=ggplot(data=A, aes_(x=~x,y=~y,color=~Variance)) +
474
+            geom_line() +
475
+            geom_point() +
476
+            geom_text(aes_(label=~labels),color='black',vjust=0,nudge_y = 5) +
477
+
478
+            ggtitle('Scree Plot', subtitle=NULL) +
479
+            xlab('Component') +
480
+            ylab('Variance (%)') +
481
+            scale_colour_Publication() +
482
+            scale_x_continuous(breaks=0:length(pvar)) +
483
+            guides(color=guide_legend("")) +
484
+            theme_Publication(base_size = 12)
485
+        return(out)
486
+    }
487 487
 )
488 488
 
489 489
 #' pca_dstat_plot class
... ...
@@ -494,73 +494,73 @@ setMethod(f="chart.plot",
494 494
 #' @export PCA.dstat
495 495
 #' @include PCA_class.R
496 496
 PCA.dstat<-setClass(
497
-  "PCA.dstat",
498
-  contains=c('chart'),
499
-  slots=c(params.number_components='entity',
500
-    params.alpha='entity'),
501
-  prototype = list(name='d-statistic plot',
502
-    description='a bar chart of the d-statistics for samples in the input PCA model',
503
-    type="bar",
504
-    params.number_components=entity(value = 2,
505
-      name = 'number of principal components',
506
-      description = 'number of principal components to use for the plot',
507
-      type='numeric'),
508
-    params.alpha=entity(value=0.95,
509
-      name='threshold for rejecting outliers',
510
-      description='a confidence threshold for rejecting samples based on the d-statistic',
511
-      type='numeric')
512
-  )
497
+    "PCA.dstat",
498
+    contains=c('chart'),
499
+    slots=c(params.number_components='entity',
500
+        params.alpha='entity'),
501
+    prototype = list(name='d-statistic plot',
502
+        description='a bar chart of the d-statistics for samples in the input PCA model',
503
+        type="bar",
504
+        params.number_components=entity(value = 2,
505
+            name = 'number of principal components',
506
+            description = 'number of principal components to use for the plot',
507
+            type='numeric'),
508
+        params.alpha=entity(value=0.95,
509
+            name='threshold for rejecting outliers',
510
+            description='a confidence threshold for rejecting samples based on the d-statistic',
511
+            type='numeric')
512
+    )
513 513
 )
514 514
 
515 515
 #' @export
516 516
 setMethod(f="chart.plot",
517
-  signature=c("PCA.dstat",'PCA'),
518
-  definition=function(obj,dobj)
519
-  {
520
-    opt=param.list(obj)
521
-    a=param.value(obj,'number_components')
522
-    scores=output.value(dobj,'scores')
523
-    I=nrow(scores)             # number of samples
524
-    sample_names=rownames(scores)
525
-    scores=scores[,1:a]
526
-    scores=as.matrix(scores)
527
-    covT=t(scores) %*% scores  # covariance matrix
528
-    covT=solve(covT/(I-1)) # inverse
529
-    H=numeric(length = I)
530
-    for (i in 1:I)
517
+    signature=c("PCA.dstat",'PCA'),
518
+    definition=function(obj,dobj)
531 519
     {
532
-      H[i]=scores[i,,drop=FALSE]%*%covT%*%t(scores[i,,drop=FALSE])
533
-    } #leverage value
534
-
535
-    # threshold at alpha
536
-    F=qf(p = opt$alpha,df1 = a,df2=I-a)
537
-    sf=(a*(I-1)*(I+1))/(I*(I-a))
538
-    threshold=sf*F
539
-    # ggplot
540
-    df=data.frame(x=sample_names,y=H)
541
-
542
-    out=ggplot(data=df, aes_(x=~x,y=~y)) +
543
-
544
-      geom_bar(stat="identity") +
545
-      geom_hline(yintercept=threshold, linetype='dashed', color='grey') +
546
-      ggtitle('d-statistic', subtitle=paste0('Number of components = ',a)) +
547
-      xlab('Sample') +
548
-      ylab('d-statistic') +
549
-      scale_x_discrete (limits = sample_names,breaks=NULL) +
550
-      scale_colour_Publication() +
551
-      theme_Publication(base_size = 12)
552
-    return(out)
553
-  }
520
+        opt=param.list(obj)
521
+        a=param.value(obj,'number_components')
522
+        scores=output.value(dobj,'scores')
523
+        I=nrow(scores)             # number of samples
524
+        sample_names=rownames(scores)
525
+        scores=scores[,1:a]
526
+        scores=as.matrix(scores)
527
+        covT=t(scores) %*% scores  # covariance matrix
528
+        covT=solve(covT/(I-1)) # inverse
529
+        H=numeric(length = I)
530
+        for (i in 1:I)
531
+        {
532
+            H[i]=scores[i,,drop=FALSE]%*%covT%*%t(scores[i,,drop=FALSE])
533
+        } #leverage value
534
+
535
+        # threshold at alpha
536
+        F=qf(p = opt$alpha,df1 = a,df2=I-a)
537
+        sf=(a*(I-1)*(I+1))/(I*(I-a))
538
+        threshold=sf*F
539
+        # ggplot
540
+        df=data.frame(x=sample_names,y=H)
541
+
542
+        out=ggplot(data=df, aes_(x=~x,y=~y)) +
543
+
544
+            geom_bar(stat="identity") +
545
+            geom_hline(yintercept=threshold, linetype='dashed', color='grey') +
546
+            ggtitle('d-statistic', subtitle=paste0('Number of components = ',a)) +
547
+            xlab('Sample') +
548
+            ylab('d-statistic') +
549
+            scale_x_discrete (limits = sample_names,breaks=NULL) +
550
+            scale_colour_Publication() +
551
+            theme_Publication(base_size = 12)
552
+        return(out)
553
+    }
554 554
 )
555 555
 
556 556
 
557 557
 
558 558
 
559 559
 circleFun <- function(center = c(0,0),diameter = 1, npoints = 100){
560
-  r = diameter / 2
561
-  tt <- seq(0,2*pi,length.out = npoints)
562
-  xx <- center[1] + r * cos(tt)
563
-  yy <- center[2] + r * sin(tt)
564
-  return(data.frame(x = xx, y = yy))
560
+    r = diameter / 2
561
+    tt <- seq(0,2*pi,length.out = npoints)
562
+    xx <- center[1] + r * cos(tt)
563
+    yy <- center[2] + r * sin(tt)
564
+    return(data.frame(x = xx, y = yy))
565 565
 }
566 566
 
Browse code

changes due to some functions being removed from PMP dependency

grlloyd authored on 02/04/2019 16:26:09
Showing 1 changed files
... ...
@@ -150,7 +150,7 @@ setMethod(f="chart.plot",
150 150
     }
151 151
 
152 152
     if (is(opt$groups,'factor')) {
153
-      plotClass=pmp::createClassAndColors(opt$groups)
153
+      plotClass= createClassAndColors(opt$groups)
154 154
       opt$groups=plotClass$class
155 155
     }
156 156
 
... ...
@@ -563,3 +563,4 @@ circleFun <- function(center = c(0,0),diameter = 1, npoints = 100){
563 563
   yy <- center[2] + r * sin(tt)
564 564
   return(data.frame(x = xx, y = yy))
565 565
 }
566
+
Browse code

fix extra comma in dstat plot

grlloyd authored on 01/04/2019 14:55:51
Showing 1 changed files
... ...
@@ -508,7 +508,7 @@ PCA.dstat<-setClass(
508 508
     params.alpha=entity(value=0.95,
509 509
       name='threshold for rejecting outliers',
510 510
       description='a confidence threshold for rejecting samples based on the d-statistic',
511
-      type='numeric'),
511
+      type='numeric')
512 512
   )
513 513
 )
514 514
 
Browse code

remove params slot

no longer needed by struct

grlloyd authored on 01/04/2019 14:54:24
Showing 1 changed files
... ...
@@ -376,7 +376,6 @@ pca_loadings_plot<-setClass(
376 376
   prototype = list(name='Feature boxplot',
377 377
     description='plots a boxplot of a chosen feature for each group of a dataset.',
378 378
     type="boxlot",
379
-    params=c('components','points_to_label','factor_name','groups'),
380 379
     params.components=entity(name='Components to plot',
381 380
       value=c(1,2),
382 381
       type='numeric',
... ...
@@ -510,7 +509,6 @@ PCA.dstat<-setClass(
510 509
       name='threshold for rejecting outliers',
511 510
       description='a confidence threshold for rejecting samples based on the d-statistic',
512 511
       type='numeric'),
513
-    params=c('number_components','alpha')
514 512
   )
515 513
 )
516 514
 
Browse code

fix pca_loadings_plot and add tests

function hadnt been converted to class def

grlloyd authored on 01/04/2019 14:11:15
Showing 1 changed files
... ...
@@ -15,7 +15,6 @@ pca_correlation_plot<-setClass(
15 15
   prototype = list(name='Feature boxplot',
16 16
     description='plots a boxplot of a chosen feature for each group of a dataset.',
17 17
     type="boxlot",
18
-    params=c('components'),
19 18
     params.components=entity(name='Components to plot',
20 19
       value=c(1,2),
21 20
       type='numeric',
... ...
@@ -72,7 +71,6 @@ pca_scores_plot<-setClass(
72 71
   prototype = list(name='PCA scores plot',
73 72
     description='Plots a 2d scatter plot of the selected components',
74 73
     type="scatter",
75
-    params=c('components','points_to_label','factor_name','groups'),
76 74
 
77 75
     params.components=entity(name='Components to plot',
78 76
       value=c(1,2),
... ...
@@ -244,7 +242,6 @@ pca_biplot_plot<-setClass(
244 242
   prototype = list(name='Feature boxplot',
245 243
     description='plots a boxplot of a chosen feature for each group of a dataset.',
246 244
     type="boxlot",
247
-    params=c('components','points_to_label','factor_name','groups'),
248 245
     params.components=entity(name='Components to plot',
249 246
       value=c(1,2),
250 247
       type='numeric',
... ...
@@ -357,47 +354,93 @@ setMethod(f="chart.plot",
357 354
 
358 355
 
359 356
 
357
+##################################################################
358
+##################################################################
359
+
360
+#' pca_loadings_plot class
361
+#'
362
+#' 2d scatter plot of princpal component loadings.
363
+#'
364
+#' @import struct
365
+#' @export pca_loadings_plot
366
+#' @include PCA_class.R
367
+pca_loadings_plot<-setClass(
368
+  "pca_loadings_plot",
369
+  contains='chart',
370
+  slots=c(
371
+    # INPUTS
372
+    params.components='entity',
373
+    params.style='enum',
374
+    params.label_features='entity'
375
+  ),
376
+  prototype = list(name='Feature boxplot',
377
+    description='plots a boxplot of a chosen feature for each group of a dataset.',
378
+    type="boxlot",
379
+    params=c('components','points_to_label','factor_name','groups'),
380
+    params.components=entity(name='Components to plot',
381
+      value=c(1,2),
382
+      type='numeric',
383
+      description='the components to be plotted e.g. c(1,2) plots component 1 on the x axis and component 2 on the y axis.'
384
+    ),
385
+    params.style=enum(name='Plot style',
386
+      value='points',
387
+      type='character',
388
+      description='Named plot styles for the biplot. [points], arrows',
389
+      list=c('points','arrows')
390
+    ),
391
+    params.label_features=entity(name='Add feature labels',
392
+      value=FALSE,
393
+      type='logical',
394
+      description='Include feature labels on the plot'
395
+    )
396
+  )
360 397
 
398
+)
361 399
 
362 400
 
363 401
 
364 402
 
365
-# loadings plot
366
-pca_loadings_plot=function(obj,opt)
367
-{
368
-  P=output.value(obj,'loadings')
369
-  # 1D plot
370
-  if (length(opt$components)==1)
371
-  {
372
-    A=data.frame("x"=1:nrow(P),"y"=P[,opt$components[1]])
373
-    out=ggplot(data=A,aes_(x=~x,y=~y)) +
374
-      geom_line() +
375
-      ggtitle('PCA Loadings', subtitle=NULL) +
376
-      xlab('Feature') +
377
-      ylab('Loading') +
378
-      scale_colour_Publication() +
379
-      theme_Publication(base_size = 12)
380
-    return(out)
381
-  }
382
-  # 2D plot
383
-  if (length(opt$components)==2)
384
-  {
385
-    A=data.frame("x"=P[,opt$components[1]],"y"=P[,opt$components[2]])
386
-    out=ggplot(data=A,aes_(x=~x,y=~y)) +
387
-      geom_point() +
388
-      ggtitle('PCA Loadings', subtitle=NULL) +
389
-      xlab(paste0('Component ',opt$components[1])) +
390
-      ylab(paste0('Component ',opt$components[2])) +
391
-      scale_colour_Publication() +
392
-      theme_Publication(base_size = 12)
393
-    return(out)
394
-  }
395
-  if (length(opt$components)>2)
403
+#' @export
404
+setMethod(f="chart.plot",
405
+  signature=c("pca_loadings_plot",'PCA'),
406
+  definition=function(obj,dobj)
396 407
   {
397
-    stop('can only plot loadings for 1 or 2 components at a time')
398
-  }
408
+    opt=param.list(obj)
399 409
 
400
-}
410
+    P=output.value(dobj,'loadings')
411
+    # 1D plot
412
+    if (length(opt$components)==1)
413
+    {
414
+      A=data.frame("x"=1:nrow(P),"y"=P[,opt$components[1]])
415
+      out=ggplot(data=A,aes_(x=~x,y=~y)) +
416
+        geom_line() +
417
+        ggtitle('PCA Loadings', subtitle=NULL) +
418
+        xlab('Feature') +
419
+        ylab('Loading') +
420
+        scale_colour_Publication() +
421
+        theme_Publication(base_size = 12)
422
+      return(out)
423
+    }
424
+    # 2D plot
425
+    if (length(opt$components)==2)
426
+    {
427
+      A=data.frame("x"=P[,opt$components[1]],"y"=P[,opt$components[2]])
428
+      out=ggplot(data=A,aes_(x=~x,y=~y)) +
429
+        geom_point() +
430
+        ggtitle('PCA Loadings', subtitle=NULL) +
431
+        xlab(paste0('Component ',opt$components[1])) +
432
+        ylab(paste0('Component ',opt$components[2])) +
433
+        scale_colour_Publication() +
434
+        theme_Publication(base_size = 12)
435
+      return(out)
436
+    }
437
+    if (length(opt$components)>2)
438
+    {
439
+      stop('can only plot loadings for 1 or 2 components at a time')
440
+    }
441
+
442
+  }
443
+)
401 444
 
402 445
 
403 446
 #' pca_scree_plot class
Browse code

update tests for PCA and related charts

grlloyd authored on 01/04/2019 13:04:40
Showing 1 changed files
... ...
@@ -238,7 +238,8 @@ pca_biplot_plot<-setClass(
238 238
     params.factor_name='entity',
239 239
     params.groups='entity',
240 240
     params.scale_factor='entity',
241
-    params.style='entity'
241
+    params.style='enum',
242
+    params.label_features='entity'
242 243
   ),
243 244
   prototype = list(name='Feature boxplot',
244 245
     description='plots a boxplot of a chosen feature for each group of a dataset.',
... ...
@@ -269,10 +270,16 @@ pca_biplot_plot<-setClass(
269 270
       type='numeric',
270 271
       description='Scaling factor to apply to loadings. Default = 0.95.'
271 272
     ),
272
-    params.style=entity(name='Plot style',
273
+    params.style=enum(name='Plot style',
273 274
       value='points',
274 275
       type='character',
275
-      description='Named plot styles for the biplot. [points]'
276
+      description='Named plot styles for the biplot. [points], arrows',
277
+      list=c('points','arrows')
278
+    ),
279
+    params.label_features=entity(name='Add feature labels',
280
+      value=FALSE,
281
+      type='logical',
282
+      description='Include feature labels on the plot'
276 283
     )
277 284
   )
278 285
 
... ...
@@ -308,19 +315,19 @@ setMethod(f="chart.plot",
308 315
     # additionaly scale the loadings
309 316
     sf=min(max(abs(Ts[,opt$components[1]]))/max(abs(P[,opt$components[1]])),
310 317
       max(abs(Ts[,opt$components[2]]))/max(abs(P[,opt$components[2]])))
311
-    output.value(obj,'scores')=as.data.frame(Ts) # nb object not returned, so only temporary scaling
318
+    dobj$scores=as.data.frame(Ts) # nb object not returned, so only temporary scaling
312 319
 
313 320
     # plot
314 321
     A=data.frame("x"=P[,opt$components[1]]*sf*0.8,"y"=P[,opt$components[2]]*sf*0.8)
315
-    out=pca_scores_plot()
316
-
322
+    C=pca_scores_plot(groups=obj$groups,points_to_label=obj$points_to_label,components=obj$components,factor_name=obj$factor_name)
323
+    out=chart.plot(C,dobj)
317 324
 
318 325
     if (opt$style=='points')
319 326
     {
320 327
       out=out+
321 328
         geom_point(data=A,inherit.aes = FALSE,color='black',mapping = aes_(x=~x,y=~y))
322 329
     }
323
-    if (opt$style=='arrow')
330
+    if (opt$style=='arrows')
324 331
     {
325 332
       out=out+
326 333
 
... ...
@@ -333,10 +340,10 @@ setMethod(f="chart.plot",
333 340
     #label features if requested
334 341
     if (opt$label_features)
335 342
     {
336
-      vlabels=opt$feature_labels
337
-      for (i in 1:length(opt$feature_labels))
343
+      vlabels=rownames(dobj$loadings)
344
+      for (i in 1:length(vlabels))
338 345
       {
339
-        vlabels[i]=paste0('  ',opt$feature_labels[i], '  ')
346
+        vlabels[i]=paste0('  ',vlabels[i], '  ')
340 347
       }
341 348
       A$vlabels=vlabels
342 349
       out=out+
Browse code

add tests for PCA

Using testthat package

grlloyd authored on 01/04/2019 10:08:03
Showing 1 changed files
... ...
@@ -237,8 +237,8 @@ pca_biplot_plot<-setClass(
237 237
     params.points_to_label='entity',
238 238
     params.factor_name='entity',
239 239
     params.groups='entity',
240
-    params.sf='entity'
241
-
240
+    params.scale_factor='entity',
241
+    params.style='entity'
242 242
   ),
243 243
   prototype = list(name='Feature boxplot',
244 244
     description='plots a boxplot of a chosen feature for each group of a dataset.',
... ...
@@ -268,6 +268,11 @@ pca_biplot_plot<-setClass(
268 268
       value=0.95,
269 269
       type='numeric',
270 270
       description='Scaling factor to apply to loadings. Default = 0.95.'
271
+    ),
272
+    params.style=entity(name='Plot style',
273
+      value='points',
274
+      type='character',
275
+      description='Named plot styles for the biplot. [points]'
271 276
     )
272 277
   )
273 278
 
... ...
@@ -307,7 +312,7 @@ setMethod(f="chart.plot",
307 312
 
308 313
     # plot
309 314
     A=data.frame("x"=P[,opt$components[1]]*sf*0.8,"y"=P[,opt$components[2]]*sf*0.8)
310
-    SP=pca_scores_plot()
315
+    out=pca_scores_plot()
311 316
 
312 317
 
313 318
     if (opt$style=='points')
Browse code

add label_filter param to PCA scores plot

allows only labelling of specific classes, and works in conjunction with params_to_label. e.g. params_to_label = 'outliers', label_filter='QC' only labels QC samples also flagged as an outlier.

grlloyd authored on 28/03/2019 10:28:57
Showing 1 changed files
... ...
@@ -65,7 +65,8 @@ pca_scores_plot<-setClass(
65 65
     params.points_to_label='enum',
66 66
     params.factor_name='entity',
67 67
     params.groups='entity',
68
-    params.ellipse='enum'
68
+    params.ellipse='enum',
69
+    params.label_filter='entity'
69 70
   ),
70 71
 
71 72
   prototype = list(name='PCA scores plot',
... ...
@@ -82,8 +83,8 @@ pca_scores_plot<-setClass(
82 83
     params.points_to_label=enum(name='points_to_label',
83 84
       value='none',
84 85
       type='character',
85
-      description='("none"), "all", or "outliers" will be labelled on the plot.'
86
-
86
+      description='("none"), "all", or "outliers" will be labelled on the plot.',
87
+      list=c('none','all','outliers')
87 88
     ),
88 89
     params.factor_name=entity(name='Factor name',
89 90
       value='factor',
... ...
@@ -101,7 +102,12 @@ pca_scores_plot<-setClass(
101 102
       '"none" will not plot any ellipses',
102 103
       '"sample" will plot ellipse for all samples (ignoring group)'),
103 104
       list=c('all','group','none','sample'),
104
-      value='all')
105
+      value='all'),
106
+    params.label_filter=entity(name='Label filter',
107
+      value=character(0),
108
+      type='character',
109
+      description='Only include the param.group labels included in params.label_filter. If zero length then all labels will be included.'
110
+    )
105 111
   )
106 112
 )
107 113
 
... ...
@@ -139,6 +145,12 @@ setMethod(f="chart.plot",
139 145
       slabels[i]=paste0('  ',slabels[i], '  ')
140 146
     }
141 147
 
148
+    # filter by label_filter list if provided
149
+    if (length(obj$label_filter)>0) {
150
+      out=!(opt$groups %in% obj$label_filter)
151
+      slabels[out]=''
152
+    }
153
+
142 154
     if (is(opt$groups,'factor')) {
143 155
       plotClass=pmp::createClassAndColors(opt$groups)
144 156
       opt$groups=plotClass$class
Browse code

initial commit

Gavin Rhys Lloyd authored on 20/03/2019 15:54:08
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,500 @@
1
+#' pca_correlation_plot class
2
+#'
3
+#' plots the correlation between features and selected components.
4
+#'
5
+#' @import struct
6
+#' @export pca_correlation_plot
7
+#' @include PCA_class.R
8
+pca_correlation_plot<-setClass(
9
+  "pca_correlation_plot",
10
+  contains='chart',
11
+  slots=c(
12
+    # INPUTS
13
+    params.components='entity'
14
+  ),
15
+  prototype = list(name='Feature boxplot',
16
+    description='plots a boxplot of a chosen feature for each group of a dataset.',
17
+    type="boxlot",
18
+    params=c('components'),
19
+    params.components=entity(name='Components to plot',
20
+      value=c(1,2),
21
+      type='numeric',
22
+      description='the components to be plotted e.g. c(1,2) plots component 1 on the x axis and component 2 on the y axis.'
23
+    )
24
+
25
+  )
26
+)
27
+
28
+#' @export
29
+setMethod(f="chart.plot",
30
+  signature=c("pca_correlation_plot",'PCA'),
31
+  definition=function(obj,dobj)
32
+  {
33
+    opt=param.list(obj)
34
+    A=data.frame(x=output.value(dobj,'correlation')[,opt$components[1]],y=output.value(dobj,'correlation')[,opt$components[2]])
35
+    dat <- circleFun(c(0,0),2,npoints = 50)
36
+
37
+    out=ggplot(data=A,aes_(x=~x,y=~y)) +
38
+      geom_point() +
39
+      scale_colour_Publication() +
40
+      theme_Publication(base_size = 12)+
41
+      coord_fixed(xlim = c(-1,1),ylim=c(-1,1)) +
42
+
43
+      geom_path(data=dat,aes_(x=~x,y=~y),inherit.aes = FALSE)
44
+
45
+    return(out)
46
+  }
47
+)
48
+
49
+#################################################
50
+#################################################
51
+
52
+#' pca_scores_plot class
53
+#'
54
+#' 2d scatter plot of princpal component scores.
55
+#'
56
+#' @import struct
57
+#' @export pca_scores_plot
58
+#' @include PCA_class.R
59
+pca_scores_plot<-setClass(
60
+  "pca_scores_plot",
61
+  contains='chart',
62
+  slots=c(
63
+    # INPUTS
64
+    params.components='entity',
65
+    params.points_to_label='enum',
66
+    params.factor_name='entity',
67
+    params.groups='entity',
68
+    params.ellipse='enum'
69
+  ),
70
+
71
+  prototype = list(name='PCA scores plot',
72
+    description='Plots a 2d scatter plot of the selected components',
73
+    type="scatter",
74
+    params=c('components','points_to_label','factor_name','groups'),
75
+
76
+    params.components=entity(name='Components to plot',
77
+      value=c(1,2),
78
+      type='numeric',
79
+      description='the components to be plotted e.g. c(1,2) plots component 1 on the x axis and component 2 on the y axis.'
80
+    ),
81
+
82
+    params.points_to_label=enum(name='points_to_label',
83
+      value='none',
84
+      type='character',
85
+      description='("none"), "all", or "outliers" will be labelled on the plot.'
86
+
87
+    ),
88
+    params.factor_name=entity(name='Factor name',
89
+      value='factor',
90
+      type='character',
91
+      description='The name of the factor to be displayed on the plot. Appears on axis and legend titles, for example. By default the column name of the meta data will be used where possible.'
92
+    ),
93
+    params.groups=entity(name='Groups',
94
+      value=factor(),
95
+      type='factor',
96
+      description='The name of the factor to be displayed on the plot. Appears on axis and legend titles, for example. By default the column name of the meta data will be used where possible.'
97
+    ),
98
+    params.ellipse=enum(name = 'Plot ellipses',description=c(
99
+      '"all" will plot all ellipses',
100
+      '"group" will only plot group ellipses',
101
+      '"none" will not plot any ellipses',
102
+      '"sample" will plot ellipse for all samples (ignoring group)'),
103
+      list=c('all','group','none','sample'),
104
+      value='all')
105
+  )
106
+)
107
+
108
+
109
+
110
+#' @importFrom sp point.in.polygon
111
+#' @import ggplot2
112
+#' @importFrom scales squish
113
+#' @export
114
+setMethod(f="chart.plot",
115
+  signature=c("pca_scores_plot",'PCA'),
116
+  definition=function(obj,dobj)
117
+  {
118
+
119
+    if (obj$points_to_label=='outliers' & !(obj$ellipse %in% c('all','sample'))) {
120
+      warning('Outliers are only labelled when plotting the sample ellipse')
121
+    }
122
+
123
+
124
+    opt=param.list(obj)
125
+
126
+    scores=output.value(dobj,'scores')
127
+    pvar=(colSums(scores*scores)/output.value(dobj,'ssx'))*100 # percent variance
128
+    pvar=round(pvar,digits = 2) # round to 2 decimal places
129
+    shapes <- rep(19,nrow(scores)) # filled circles for all samples
130
+    slabels <- rownames(scores)
131
+    x=scores[,opt$components[1]]
132
+    y=scores[,opt$components[2]]
133
+    xlabel=paste("PC",opt$components[[1]],' (',sprintf("%.1f",pvar[opt$components[[1]]]),'%)',sep='')
134
+    ylabel=paste("PC",opt$components[[2]],' (',sprintf("%.1f",pvar[opt$components[[2]]]),'%)',sep='')
135
+
136
+    # add a space to the front of the labels to offset them from the points, because nudge_x is in data units
137
+    for (i in 1:length(slabels))
138
+    {
139
+      slabels[i]=paste0('  ',slabels[i], '  ')
140
+    }
141
+
142
+    if (is(opt$groups,'factor')) {
143
+      plotClass=pmp::createClassAndColors(opt$groups)
144
+      opt$groups=plotClass$class
145
+    }
146
+
147
+    # build the plot
148
+    A <- data.frame (group=opt$groups,x=x, y=y)
149
+
150
+    out=ggplot (data=A, aes_(x=~x,y=~y,colour=~group,label=~slabels,shapes=~shapes)) +
151
+
152
+      geom_point(na.rm=TRUE) +
153
+      xlab(xlabel) +
154
+      ylab(ylabel) +
155
+      ggtitle('PCA Scores', subtitle=NULL) +
156
+
157
+      if (obj$ellipse %in% c('all','group')) {
158
+        stat_ellipse(type='norm') # ellipse for individual groups
159
+      }
160
+
161
+    if (is(opt$groups,'factor')) # if a factor then plot by group using the colours from pmp package
162
+    {
163
+      out=out+scale_colour_manual(values=plotClass$manual_colors,name=opt$factor_name)
164
+    }
165
+    else # assume continuous and use the default colour gradient
166
+    {
167
+      out=out+scale_colour_viridis_c(limits=quantile(opt$groups,c(0.05,0.95),na.rm = TRUE),oob=squish,name=opt$factor_name)
168
+    }
169
+    out=out+theme_Publication(base_size = 12)
170
+    # add ellipse for all samples (ignoring group)
171
+    if (obj$ellipse %in% c('all','sample')) {
172
+      out=out+stat_ellipse(type='norm',mapping=aes(x=x,y=y),colour="#C0C0C0",linetype='dashed',data=A)
173
+    }
174
+
175
+    if (obj$ellipse %in% c('all','sample')) { # only do this if we plotted the sample ellipse
176
+      # identify samples outside the ellipse
177
+      build=ggplot_build(out)$data
178
+      points=build[[1]]
179
+      ell=build[[length(build)]]
180
+      # outlier for dataset ellipse
181
+      points$in.ell=as.logical(sp::point.in.polygon(points$x,points$y,ell$x,ell$y))
182
+
183
+      # label outliers if
184
+      if (opt$points_to_label=='outliers')
185
+      {
186
+        if (!all(points$in.ell))
187
+        {
188
+
189
+          temp=subset(points,!points$in.ell)
190
+          temp$group=opt$groups[!points$in.ell]
191
+          out=out+geom_text(data=temp,aes_(x=~x,y=~y,label=~label,colour=~group),vjust="inward",hjust="inward")
192
+
193
+        }
194
+      }
195
+      # add a list of outliers to the plot object
196
+      out$outliers=trimws(slabels[!points$in.ell])
197
+    }
198
+
199
+    # label all points if requested
200
+    if (opt$points_to_label=='all')
201
+    {
202
+      out=out+geom_text(vjust="inward",hjust="inward")
203
+    }
204
+
205
+    return(out)
206
+  }
207
+)
208
+
209
+#################################################################
210
+#################################################################
211
+
212
+#' pca_biplot_plot class
213
+#'
214
+#' 2d scatter plot of princpal component scores overlaid with principal component loadings.
215
+#'
216
+#' @import struct
217
+#' @export pca_biplot_plot
218
+#' @include PCA_class.R
219
+pca_biplot_plot<-setClass(
220
+  "pca_biplot_plot",
221
+  contains='chart',
222
+  slots=c(
223
+    # INPUTS
224
+    params.components='entity',
225
+    params.points_to_label='entity',
226
+    params.factor_name='entity',
227
+    params.groups='entity',
228
+    params.sf='entity'
229
+
230
+  ),
231
+  prototype = list(name='Feature boxplot',
232
+    description='plots a boxplot of a chosen feature for each group of a dataset.',
233
+    type="boxlot",
234
+    params=c('components','points_to_label','factor_name','groups'),
235
+    params.components=entity(name='Components to plot',
236
+      value=c(1,2),
237
+      type='numeric',
238
+      description='the components to be plotted e.g. c(1,2) plots component 1 on the x axis and component 2 on the y axis.'
239
+    ),
240
+    params.points_to_label=entity(name='points_to_label',
241
+      value='none',
242
+      type='character',
243
+      description='("none"), "all", or "outliers" will be labelled on the plot.'
244
+    ),
245
+    params.factor_name=entity(name='Factor name',
246
+      value='factor',
247
+      type='character',
248
+      description='The name of the factor to be displayed on the plot. Appears on axis and legend titles, for example. By default the column name of the meta data will be used where possible.'
249
+    ),
250
+    params.groups=entity(name='Groups',
251
+      value=factor(),
252
+      type='factor',
253
+      description='The name of the factor to be displayed on the plot. Appears on axis and legend titles, for example. By default the column name of the meta data will be used where possible.'
254
+    ),
255
+    params.scale_factor=entity(name='Loadings scale factor',
256
+      value=0.95,
257
+      type='numeric',
258
+      description='Scaling factor to apply to loadings. Default = 0.95.'
259
+    )
260
+  )
261
+
262
+)
263
+
264
+#' @export
265
+setMethod(f="chart.plot",
266
+  signature=c("pca_biplot_plot",'PCA'),
267
+  definition=function(obj,dobj)
268
+  {
269
+    opt=param.list(obj)
270
+    Ts=output.value(dobj,'scores')
271
+    pvar=(colSums(Ts*Ts)/output.value(dobj,'ssx'))*100
272
+    pvar=round(pvar,digits = 1)
273
+    xlabel=paste("PC",opt$components[[1]],' (',sprintf("%.1f",pvar[opt$components[[1]]]),'%)',sep='')
274
+    ylabel=paste("PC",opt$components[[2]],' (',sprintf("%.1f",pvar[opt$components[[2]]]),'%)',sep='')
275
+
276
+    P=output.value(dobj,'loadings')
277
+    Ev=output.value(dobj,'eigenvalues')
278
+
279
+    # eigenvalues were square rooted when training PCA
280
+    Ev=Ev[,1]
281
+    Ev=Ev^2
282
+
283
+    ## unscale the scores
284
+    #ev are the norms of scores
285
+    Ts=as.matrix(Ts) %*% diag(1/Ev) # these are normalised scores
286
+
287
+    # scale scores and loadings by alpha
288
+    Ts=Ts %*% diag(Ev^(1-opt$scale_factor))
289
+    P=as.matrix(P) %*% diag(Ev^(opt$scale_factor))
290
+
291
+    # additionaly scale the loadings
292
+    sf=min(max(abs(Ts[,opt$components[1]]))/max(abs(P[,opt$components[1]])),
293
+      max(abs(Ts[,opt$components[2]]))/max(abs(P[,opt$components[2]])))
294
+    output.value(obj,'scores')=as.data.frame(Ts) # nb object not returned, so only temporary scaling
295
+
296
+    # plot
297
+    A=data.frame("x"=P[,opt$components[1]]*sf*0.8,"y"=P[,opt$components[2]]*sf*0.8)
298
+    SP=pca_scores_plot()
299
+
300
+
301
+    if (opt$style=='points')
302
+    {
303
+      out=out+
304
+        geom_point(data=A,inherit.aes = FALSE,color='black',mapping = aes_(x=~x,y=~y))
305
+    }
306
+    if (opt$style=='arrow')
307
+    {
308
+      out=out+
309
+
310
+        geom_segment(data=A,inherit.aes = FALSE,color='black',mapping = aes_(x=~0,y=~0,xend=~x,yend=~y),arrow=arrow(length=unit(8,'points')))
311
+
312
+    }
313
+    out=out+ggtitle('PCA biplot', subtitle=NULL) +
314
+      xlab(xlabel) + ylab(ylabel)
315
+
316
+    #label features if requested
317
+    if (opt$label_features)
318
+    {
319
+      vlabels=opt$feature_labels
320
+      for (i in 1:length(opt$feature_labels))
321
+      {
322
+        vlabels[i]=paste0('  ',opt$feature_labels[i], '  ')
323
+      }
324
+      A$vlabels=vlabels
325
+      out=out+
326
+
327
+        geom_text(data=A,aes_(x=~x,y=~y,label=~vlabels),vjust="inward",hjust="inward",inherit.aes = FALSE)
328
+
329
+    }
330
+    return(out)
331
+  }
332
+)
333
+
334
+
335
+
336
+
337
+
338
+
339
+
340
+
341
+# loadings plot
342
+pca_loadings_plot=function(obj,opt)
343
+{
344
+  P=output.value(obj,'loadings')
345
+  # 1D plot
346
+  if (length(opt$components)==1)
347
+  {
348
+    A=data.frame("x"=1:nrow(P),"y"=P[,opt$components[1]])
349
+    out=ggplot(data=A,aes_(x=~x,y=~y)) +
350
+      geom_line() +
351
+      ggtitle('PCA Loadings', subtitle=NULL) +
352
+      xlab('Feature') +
353
+      ylab('Loading') +
354
+      scale_colour_Publication() +
355
+      theme_Publication(base_size = 12)
356
+    return(out)
357
+  }
358
+  # 2D plot
359
+  if (length(opt$components)==2)
360
+  {
361
+    A=data.frame("x"=P[,opt$components[1]],"y"=P[,opt$components[2]])
362
+    out=ggplot(data=A,aes_(x=~x,y=~y)) +
363
+      geom_point() +
364
+      ggtitle('PCA Loadings', subtitle=NULL) +
365
+      xlab(paste0('Component ',opt$components[1])) +
366
+      ylab(paste0('Component ',opt$components[2])) +
367
+      scale_colour_Publication() +
368
+      theme_Publication(base_size = 12)
369
+    return(out)
370
+  }
371
+  if (length(opt$components)>2)
372
+  {
373
+    stop('can only plot loadings for 1 or 2 components at a time')
374
+  }
375
+
376
+}
377
+
378
+
379
+#' pca_scree_plot class
380
+#'
381
+#' line plot showing percent variance and cumulative peercent variance for the computed components.
382
+#'
383
+#' @import struct
384
+#' @export PCA.scree
385
+#' @include PCA_class.R
386
+PCA.scree<-setClass(
387
+  "PCA.scree",
388
+  contains=c('chart'),
389
+  prototype = list(name='Scree plot',
390
+    description='plots the percent and cumulative percent variance for the calculated components',
391
+    type="line"
392
+  )
393
+)
394
+
395
+#' @export
396
+setMethod(f="chart.plot",
397
+  signature=c("PCA.scree",'PCA'),
398
+  definition=function(obj,dobj)
399
+  {
400
+    ## percent variance
401
+    scores=output.value(dobj,'scores')
402
+    pvar=(colSums(scores*scores)/output.value(dobj,'ssx'))*100
403
+    A=data.frame("x"=1:length(pvar),"y"=c(pvar,cumsum(pvar)),"Variance"=as.factor(c(rep('Single component',length(pvar)),rep('Cumulative',length(pvar)))))
404
+    labels=round(A$y,digits = 1)
405
+    labels=format(labels,1)
406
+
407
+    out=ggplot(data=A, aes_(x=~x,y=~y,color=~Variance)) +
408
+      geom_line() +
409
+      geom_point() +
410
+      geom_text(aes_(label=~labels),color='black',vjust=0,nudge_y = 5) +
411
+
412
+      ggtitle('Scree Plot', subtitle=NULL) +
413
+      xlab('Component') +
414
+      ylab('Variance (%)') +
415
+      scale_colour_Publication() +
416
+      scale_x_continuous(breaks=0:length(pvar)) +
417
+      guides(color=guide_legend("")) +
418
+      theme_Publication(base_size = 12)
419
+    return(out)
420
+  }
421
+)
422
+
423
+#' pca_dstat_plot class
424
+#'
425
+#' line plot showing percent variance and cumulative peercent variance for the computed components.
426
+#'
427
+#' @import struct
428
+#' @export PCA.dstat
429
+#' @include PCA_class.R
430
+PCA.dstat<-setClass(
431
+  "PCA.dstat",
432
+  contains=c('chart'),
433
+  slots=c(params.number_components='entity',
434
+    params.alpha='entity'),
435
+  prototype = list(name='d-statistic plot',
436
+    description='a bar chart of the d-statistics for samples in the input PCA model',
437
+    type="bar",
438
+    params.number_components=entity(value = 2,
439
+      name = 'number of principal components',
440
+      description = 'number of principal components to use for the plot',
441
+      type='numeric'),
442
+    params.alpha=entity(value=0.95,
443
+      name='threshold for rejecting outliers',
444
+      description='a confidence threshold for rejecting samples based on the d-statistic',
445
+      type='numeric'),
446
+    params=c('number_components','alpha')
447
+  )
448
+)
449
+
450
+#' @export
451
+setMethod(f="chart.plot",
452
+  signature=c("PCA.dstat",'PCA'),
453
+  definition=function(obj,dobj)
454
+  {
455
+    opt=param.list(obj)
456
+    a=param.value(obj,'number_components')
457
+    scores=output.value(dobj,'scores')
458
+    I=nrow(scores)             # number of samples
459
+    sample_names=rownames(scores)
460
+    scores=scores[,1:a]
461
+    scores=as.matrix(scores)
462
+    covT=t(scores) %*% scores  # covariance matrix
463
+    covT=solve(covT/(I-1)) # inverse
464
+    H=numeric(length = I)
465
+    for (i in 1:I)
466
+    {
467
+      H[i]=scores[i,,drop=FALSE]%*%covT%*%t(scores[i,,drop=FALSE])
468
+    } #leverage value
469
+
470
+    # threshold at alpha
471
+    F=qf(p = opt$alpha,df1 = a,df2=I-a)
472
+    sf=(a*(I-1)*(I+1))/(I*(I-a))
473
+    threshold=sf*F
474
+    # ggplot
475
+    df=data.frame(x=sample_names,y=H)
476
+
477
+    out=ggplot(data=df, aes_(x=~x,y=~y)) +
478
+
479
+      geom_bar(stat="identity") +
480
+      geom_hline(yintercept=threshold, linetype='dashed', color='grey') +
481
+      ggtitle('d-statistic', subtitle=paste0('Number of components = ',a)) +
482
+      xlab('Sample') +
483
+      ylab('d-statistic') +
484
+      scale_x_discrete (limits = sample_names,breaks=NULL) +
485
+      scale_colour_Publication() +
486
+      theme_Publication(base_size = 12)
487
+    return(out)
488
+  }
489
+)
490
+
491
+
492
+
493
+
494
+circleFun <- function(center = c(0,0),diameter = 1, npoints = 100){
495
+  r = diameter / 2
496
+  tt <- seq(0,2*pi,length.out = npoints)
497
+  xx <- center[1] + r * cos(tt)
498
+  yy <- center[2] + r * sin(tt)
499
+  return(data.frame(x = xx, y = yy))
500
+}