Skip to content

Commit e1fb836

Browse files
committed
fix labelling when NA present and add plotting options
1 parent b4e61a2 commit e1fb836

File tree

2 files changed

+77
-15
lines changed

2 files changed

+77
-15
lines changed

R/dataset_chart_classes.R

Lines changed: 68 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@
55
#' chart_plot(C,D)
66
#' @export feature_boxplot
77
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,...) {
99
out=struct::new_struct('feature_boxplot',
1010
label_outliers=label_outliers,
1111
feature_to_plot=feature_to_plot,
1212
factor_name=factor_name,
1313
show_counts=show_counts,
14+
style=style,
15+
jitter=jitter,
16+
fill=fill,
1417
...)
1518
return(out)
1619
}
@@ -24,13 +27,16 @@ feature_boxplot = function(label_outliers=TRUE,feature_to_plot,
2427
label_outliers='entity',
2528
feature_to_plot='entity',
2629
factor_name='entity',
27-
show_counts='entity'
30+
show_counts='entity',
31+
style='enum',
32+
jitter='entity',
33+
fill='entity'
2834
),
2935
prototype = list(name='Feature boxplot',
3036
description='A boxplot to visualise the distribution of values within a feature.',
3137
type="boxlot",
3238
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'),
3440

3541
label_outliers=entity(name='Label outliers',
3642
value=TRUE,
@@ -46,7 +52,32 @@ feature_boxplot = function(label_outliers=TRUE,feature_to_plot,
4652
description='The column name of the plotted feature.'
4753
),
4854
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+
)
5081
)
5182
)
5283

@@ -71,31 +102,53 @@ setMethod(f="chart_plot",
71102
# meta data
72103
SM=dobj$sample_meta
73104
SM=SM[[obj$factor_name]]
105+
names(SM)=rownames(dobj)
74106

75107
# remove NA
76108
SM=SM[!is.na(Xt)]
77109
Xt=Xt[!is.na(Xt)]
78110

111+
# get color pallete using pmp
112+
clrs= createClassAndColors(class = SM)
113+
SM=clrs$class
114+
79115
# count number of values
80116
L=levels(SM)
81117
count=numeric(length(L))
82118
for (i in 1:length(L)) {
83119
count[i]=sum(SM==L[i])
84120
}
85121

86-
# get color pallete using pmp
87-
clrs= createClassAndColors(class = SM)
88-
SM=clrs$class
89-
90122
#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) +
95145
ylab('') +
96146
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) +
99152
theme(legend.position="none")
100153

101154
if (opt$show_counts) {
@@ -112,7 +165,7 @@ setMethod(f="chart_plot",
112165
outliers=c(outliers,IN[which( Xt[IN]<(quantile(Xt[IN], 0.25) - 1.5*IQR(Xt[IN]))) ] )
113166
}
114167
outlier_df=temp[outliers,]
115-
outlier_df$out_label=paste0(' ',rownames(dobj$data))[outliers]
168+
outlier_df$out_label=paste0(' ',rownames(temp))[outliers]
116169
p=p+geom_text(data=outlier_df,aes_(group=~x,color=~x,label=~out_label),hjust='left')
117170
}
118171

man/feature_boxplot.Rd

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)