5
5
# ' chart_plot(C,D)
6
6
# ' @export feature_boxplot
7
7
feature_boxplot = function (label_outliers = TRUE ,feature_to_plot ,
8
- factor_name ,show_counts = TRUE ,... ) {
8
+ factor_name ,show_counts = TRUE ,style = ' boxplot ' , jitter = FALSE , fill = FALSE , ... ) {
9
9
out = struct :: new_struct(' feature_boxplot' ,
10
10
label_outliers = label_outliers ,
11
11
feature_to_plot = feature_to_plot ,
12
12
factor_name = factor_name ,
13
13
show_counts = show_counts ,
14
+ style = style ,
15
+ jitter = jitter ,
16
+ fill = fill ,
14
17
... )
15
18
return (out )
16
19
}
@@ -24,13 +27,16 @@ feature_boxplot = function(label_outliers=TRUE,feature_to_plot,
24
27
label_outliers = ' entity' ,
25
28
feature_to_plot = ' entity' ,
26
29
factor_name = ' entity' ,
27
- show_counts = ' entity'
30
+ show_counts = ' entity' ,
31
+ style = ' enum' ,
32
+ jitter = ' entity' ,
33
+ fill = ' entity'
28
34
),
29
35
prototype = list (name = ' Feature boxplot' ,
30
36
description = ' A boxplot to visualise the distribution of values within a feature.' ,
31
37
type = " boxlot" ,
32
38
ontology = ' STATO:0000243' ,
33
- .params = c(' label_outliers' ,' feature_to_plot' ,' factor_name' ,' show_counts' ),
39
+ .params = c(' label_outliers' ,' feature_to_plot' ,' factor_name' ,' show_counts' , ' style ' , ' jitter ' , ' fill ' ),
34
40
35
41
label_outliers = entity(name = ' Label outliers' ,
36
42
value = TRUE ,
@@ -46,7 +52,32 @@ feature_boxplot = function(label_outliers=TRUE,feature_to_plot,
46
52
description = ' The column name of the plotted feature.'
47
53
),
48
54
factor_name = ents $ factor_name ,
49
- show_counts = ents $ show_counts
55
+ show_counts = ents $ show_counts ,
56
+ style = enum(
57
+ name = ' Plot style' ,
58
+ description = c(
59
+ ' boxplot' = ' boxplot style' ,
60
+ ' violin' = ' violon plot style'
61
+ ),
62
+ allowed = c(' boxplot' ,' violin' ),
63
+ value = ' boxplot' ,
64
+ max_length = 1 ,
65
+ type = ' character'
66
+ ),
67
+ jitter = entity(
68
+ name = ' Jitter' ,
69
+ description = ' Include points plotted with added jitter.' ,
70
+ value = FALSE ,
71
+ type = ' logical' ,
72
+ max_length = 1
73
+ ),
74
+ fill = entity(
75
+ name = ' Fill' ,
76
+ description = ' Block fill the boxes or violins with the group colour.' ,
77
+ value = FALSE ,
78
+ type = ' logical' ,
79
+ max_length = 1
80
+ )
50
81
)
51
82
)
52
83
@@ -71,31 +102,53 @@ setMethod(f="chart_plot",
71
102
# meta data
72
103
SM = dobj $ sample_meta
73
104
SM = SM [[obj $ factor_name ]]
105
+ names(SM )= rownames(dobj )
74
106
75
107
# remove NA
76
108
SM = SM [! is.na(Xt )]
77
109
Xt = Xt [! is.na(Xt )]
78
110
111
+ # get color pallete using pmp
112
+ clrs = createClassAndColors(class = SM )
113
+ SM = clrs $ class
114
+
79
115
# count number of values
80
116
L = levels(SM )
81
117
count = numeric (length(L ))
82
118
for (i in 1 : length(L )) {
83
119
count [i ]= sum(SM == L [i ])
84
120
}
85
121
86
- # get color pallete using pmp
87
- clrs = createClassAndColors(class = SM )
88
- SM = clrs $ class
89
-
90
122
# prep the plot
91
- temp = data.frame (x = SM ,y = Xt )
92
- p <- ggplot(temp , aes_(x = ~ x ,y = ~ y ,color = ~ x )) +
93
- geom_boxplot() +
94
- xlab(opt $ factor ) +
123
+ temp = data.frame (x = SM ,y = Xt ,row.names = names(SM ))
124
+
125
+ if (obj $ fill ) {
126
+ A = aes_string(x = ' x' ,y = ' y' ,color = ' x' ,fill = ' x' )
127
+ } else {
128
+ A = aes_string(x = ' x' ,y = ' y' ,color = ' x' )
129
+ }
130
+
131
+
132
+ p <- ggplot(temp , A )
133
+
134
+ if (obj $ style == ' boxplot' ) {
135
+ p = p + geom_boxplot(alpha = 0.3 )
136
+ } else {
137
+ p = p + geom_violin(alpha = 0.3 , draw_quantiles = c(0.25 , 0.5 , 0.75 ),
138
+ scale = " width" , adjust = .5 )
139
+ }
140
+
141
+ if (obj $ jitter ) {
142
+ p = p + geom_jitter(show.legend = F , width = 0.1 )
143
+ }
144
+ p = p + xlab(opt $ factor ) +
95
145
ylab(' ' ) +
96
146
ggtitle(varn ) +
97
- scale_colour_manual(values = clrs $ manual_colors ,name = opt $ factor_name ) +
98
- theme_Publication(base_size = 12 ) +
147
+ scale_colour_manual(values = clrs $ manual_colors ,name = opt $ factor_name )
148
+
149
+ p = p + scale_fill_manual(values = clrs $ manual_colors ,name = opt $ factor_name )
150
+
151
+ p = p + theme_Publication(base_size = 12 ) +
99
152
theme(legend.position = " none" )
100
153
101
154
if (opt $ show_counts ) {
@@ -112,7 +165,7 @@ setMethod(f="chart_plot",
112
165
outliers = c(outliers ,IN [which( Xt [IN ]< (quantile(Xt [IN ], 0.25 ) - 1.5 * IQR(Xt [IN ]))) ] )
113
166
}
114
167
outlier_df = temp [outliers ,]
115
- outlier_df $ out_label = paste0(' ' ,rownames(dobj $ data ))[outliers ]
168
+ outlier_df $ out_label = paste0(' ' ,rownames(temp ))[outliers ]
116
169
p = p + geom_text(data = outlier_df ,aes_(group = ~ x ,color = ~ x ,label = ~ out_label ),hjust = ' left' )
117
170
}
118
171
0 commit comments