@@ -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 )
@@ -70,7 +71,9 @@ pca_scores_plot<-setClass(
70
71
params.factor_name = ' entity' ,
71
72
params.ellipse = ' enum' ,
72
73
params.label_filter = ' entity' ,
73
- params.groups = ' ANY' # will be deprecated
74
+ params.groups = ' ANY' , # will be deprecated
75
+ params.label_factor = ' entity' ,
76
+ params.label_size = ' entity'
74
77
),
75
78
76
79
prototype = list (name = ' PCA scores plot' ,
@@ -93,7 +96,8 @@ pca_scores_plot<-setClass(
93
96
params.factor_name = entity(name = ' Factor name' ,
94
97
value = ' factor' ,
95
98
type = ' character' ,
96
- description = ' The column name of sample meta to use for plotting.'
99
+ description = ' The column name of sample meta to use for plotting. A second column can be included to plot using symbols.' ,
100
+ max_length = 2
97
101
),
98
102
params.ellipse = enum(name = ' Plot ellipses' ,description = c(
99
103
' "all" will plot all ellipses' ,
@@ -106,7 +110,15 @@ pca_scores_plot<-setClass(
106
110
value = character (0 ),
107
111
type = ' character' ,
108
112
description = ' Only include the param.group labels included in params.label_filter. If zero length then all labels will be included.'
109
- )
113
+ ),
114
+ params.label_factor = entity(names = ' Factor for labels' ,
115
+ description = ' The column name of sample_meta to use as labels. "rownames" will use the row names from sample_meta.' ,
116
+ type = ' character' ,
117
+ value = ' rownames' ),
118
+ params.label_size = entity(names = ' Text size of labels' ,
119
+ description = ' The text size of labels. Note this is not in Font Units. Default 3.88.' ,
120
+ type = ' numeric' ,
121
+ value = 3.88 )
110
122
)
111
123
)
112
124
@@ -116,6 +128,7 @@ pca_scores_plot<-setClass(
116
128
# ' @import ggplot2
117
129
# ' @importFrom scales squish
118
130
# ' @export
131
+ # ' @template chart_plot
119
132
setMethod (f="chart.plot ",
120
133
signature = c(" pca_scores_plot" ,' PCA' ),
121
134
definition = function (obj ,dobj )
@@ -126,10 +139,22 @@ setMethod(f="chart.plot",
126
139
}
127
140
opt = param.list(obj )
128
141
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 )
142
+ pvar = (colSums(scores * scores )/ output.value(dobj ,' ssx' ))* 100 # percent variance
143
+ pvar = round(pvar ,digits = 2 ) # round to 2 decimal places
144
+
145
+ if (length(obj $ factor_name )== 1 ) {
146
+ shapes = 19 # filled circles for all samples
147
+ } else {
148
+ shapes = factor (dobj $ scores $ sample_meta [[obj $ factor_name [2 ]]])
149
+ }
150
+
151
+ if (obj $ label_factor == ' rownames' ) {
152
+ slabels = rownames(dobj $ scores $ sample_meta )
153
+ } else {
154
+ slabels = dobj $ scores $ sample_meta [[obj $ label_factor ]]
155
+ }
156
+ opt $ factor_name = opt $ factor_name [[1 ]] # only use the first factor from now on
157
+
133
158
x = scores [,opt $ components [1 ]]
134
159
y = scores [,opt $ components [2 ]]
135
160
xlabel = paste(" PC" ,opt $ components [[1 ]],' (' ,sprintf(" %.1f" ,pvar [opt $ components [[1 ]]]),' %)' ,sep = ' ' )
@@ -158,16 +183,27 @@ setMethod(f="chart.plot",
158
183
# build the plot
159
184
A <- data.frame (group = opt $ groups ,x = x , y = y )
160
185
161
- out = ggplot (data = A , aes_(x = ~ x ,y = ~ y ,colour = ~ group ,label = ~ slabels ,shapes = ~ shapes )) +
186
+ if (length(obj $ factor_name )== 2 ) {
187
+ out = ggplot (data = A , aes_(x = ~ x ,y = ~ y ,colour = ~ group ,label = ~ slabels ,shape = ~ shapes ))
188
+ } else {
189
+ out = ggplot (data = A , aes_(x = ~ x ,y = ~ y ,colour = ~ group ,label = ~ slabels ))
190
+ }
191
+ out = out +
162
192
163
193
geom_point(na.rm = TRUE ) +
164
194
xlab(xlabel ) +
165
195
ylab(ylabel ) +
166
- ggtitle(' PCA Scores' , subtitle = NULL ) +
196
+ ggtitle(' PCA Scores' , subtitle = NULL )
167
197
168
- if (obj $ ellipse %in% c(' all' ,' group' )) {
169
- stat_ellipse(type = ' norm' ) # ellipse for individual groups
170
- }
198
+ if (length(obj $ factor_name )== 2 ) {
199
+ out = out + labs(shape = obj $ factor_name [[2 ]],colour = obj $ factor_name [[1 ]])
200
+ } else {
201
+ out = out + labs(shape = obj $ factor_name [[1 ]])
202
+ }
203
+
204
+ if (obj $ ellipse %in% c(' all' ,' group' )) {
205
+ out = out + stat_ellipse(type = ' norm' ) # ellipse for individual groups
206
+ }
171
207
172
208
if (is(opt $ groups ,' factor' )) { # if a factor then plot by group using the colours from pmp package
173
209
out = out + scale_colour_manual(values = plotClass $ manual_colors ,name = opt $ factor_name )
@@ -194,10 +230,9 @@ setMethod(f="chart.plot",
194
230
{
195
231
if (! all(points $ in .ell ))
196
232
{
197
-
198
233
temp = subset(points ,! points $ in .ell )
199
234
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" )
235
+ out = out + geom_text(data = temp ,aes_(x = ~ x ,y = ~ y ,label = ~ label ,colour = ~ group ),size = obj $ label_size , vjust = " inward" ,hjust = " inward" )
201
236
202
237
}
203
238
}
@@ -248,7 +283,7 @@ pca_biplot_plot<-setClass(
248
283
type = ' numeric' ,
249
284
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
285
max_length = 2
251
- ),
286
+ ),
252
287
params.points_to_label = entity(name = ' points_to_label' ,
253
288
value = ' none' ,
254
289
type = ' character' ,
@@ -285,6 +320,7 @@ pca_biplot_plot<-setClass(
285
320
)
286
321
287
322
# ' @export
323
+ # ' @template chart_plot
288
324
setMethod (f="chart.plot ",
289
325
signature = c(" pca_biplot_plot" ,' PCA' ),
290
326
definition = function (obj ,dobj )
@@ -405,6 +441,7 @@ pca_loadings_plot<-setClass(
405
441
406
442
407
443
# ' @export
444
+ # ' @template chart_plot
408
445
setMethod (f="chart.plot ",
409
446
signature = c(" pca_loadings_plot" ,' PCA' ),
410
447
definition = function (obj ,dobj )
@@ -466,6 +503,7 @@ PCA.scree<-setClass(
466
503
)
467
504
468
505
# ' @export
506
+ # ' @template chart_plot
469
507
setMethod (f="chart.plot ",
470
508
signature = c(" PCA.scree" ,' PCA' ),
471
509
definition = function (obj ,dobj )
@@ -522,6 +560,7 @@ PCA.dstat<-setClass(
522
560
)
523
561
524
562
# ' @export
563
+ # ' @template chart_plot
525
564
setMethod (f="chart.plot ",
526
565
signature = c(" PCA.dstat" ,' PCA' ),
527
566
definition = function (obj ,dobj )
0 commit comments