Chart/GGPlot/Geom/Boxplot.pm

filter: Statementstmt 90.4% 95 / 105 Branchbran 51.9% 27 / 52 Conditioncond 22.2% 4 / 18 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 100% 16 / 16 Podpod 0.0% 0 / 4 totaltotal 68.9% 142 / 206 CC 33 SCAR 27.0
CC
33
Cov
72%
__ANON__
32.4
setup_data
14.9
extra_params
3.1
CRAP 14.8
« Chart/GGPlot/Stat/Identity.pm ↑ Summary Chart/GGPlot/Position/Dodge.pm »
1 package Chart::GGPlot::Geom::Boxplot;
2  
3 # ABSTRACT: Class for boxplot geom
4  
5 5 ▶use Chart::GGPlot::Class qw(:pdl);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use MooseX::Singleton;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 5 extends qw(Chart::GGPlot::Geom::Rect);
10  
11 5 our $VERSION = '0.002003'; # VERSION
12  
13 5 ▶use Chart::GGPlot::Aes;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use Chart::GGPlot::Layer;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 5 ▶use Chart::GGPlot::Position::Functions qw(position_dodge2);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 5 ▶use Chart::GGPlot::Util qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 5 ▶use Chart::GGPlot::Util::Pod qw(layer_func_pod);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 has '+default_aes' => (
20 1 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
21 1 Chart::GGPlot::Aes->new(
22 weight => pdl(1),
23 color => PDL::SV->new( ['grey20'] ),
24 fill => PDL::SV->new( ['white'] ),
25 size => pdl(0.5),
26 alpha => NA(),
27 shape => pdl(19),
28 linetype => PDL::SV->new( ['solid'] ),
29 );
30 }
31 5 );
32  
33 4 ▶classmethod required_aes() { [qw(x lower upper middle ymin ymax)] }
Subroutine required_aes: called CC=3 SCAR=11.2
Pod undocumented
Branch: 2 branches
BranchTrueFalseTotal
@_ < 1 and Function::Parameters::_croak('Too few arguments for classmethod required_aes (expected 1, got ' . @_ . ')') 0 4 4
@_ > 1 and Function::Parameters::_croak('Too many arguments for classmethod required_aes (expected 1, got ' . @_ . ')') 0 4 4
34 1 ▶classmethod extra_params () {
Subroutine extra_params: called CC=3 SCAR=11.3
Pod undocumented
Branch: 2 branches
BranchTrueFalseTotal
@_ < 1 and Function::Parameters::_croak('Too few arguments for classmethod extra_params (expected 1, got ' . @_ . ')') 0 1 1
@_ > 1 and Function::Parameters::_croak('Too many arguments for classmethod extra_params (expected 1, got ' . @_ . ')') 0 1 1
35 [
36 1 qw(
37 fatten
38 outlier_color outlier_fill outlier_shape outlier_size
39 outlier_stroke outlier_alpha
40 notch notchwidth varwidth na_rm width
41 )
42 ]
43 5 }
44  
45 5 my $geom_boxplot_pod = layer_func_pod(<<'EOT');
46  
47 geom_boxplot(:$mapping=undef, :$data=undef,
48 :$stat='boxplot', :$position='dodge2',
49 :$outlier_color=undef, :$outlier_colour=undef,
50 :$outlier_fill=undef, :$outlier_shape=undef,
51 :$outlier_size=1.5, :$outlier_stroke=undef,
52 :$outlier_alpha=undef,
53 :$notch=false, :$notchwidth=0.25,
54 :$varwidth=false, :$na_rm=false,
55 :$show_legend=undef, :$inherit_aes=true,
56 %rest)
57  
58 The boxplot compactly displays the distribution of a continuous
59 variable. It visualises five summary statistics (the median, two hinges
60 and two whiskers), and all "outlying" points individually.
61  
62 Arguments:
63  
64 =over 4
65  
66 %TMPL_COMMON_ARGS%
67  
68 =item * $outlier_color, $outlier_fill, $outlier_size, $outlier_stroke,
69 $outlier_alpha
70  
71 Default aesthetics for outliers. Set to C<undef> to inherit from the
72 aesthetics used for the box.
73  
74 Sometimes it can be useful to hide the outliers, for example when
75 overlaying the raw data points on top of the boxplot. Hiding the
76 outliers can be achieved by setting C<outlier_shape =E<gt> ''>.
77 Importantly, this does not remove the outliers, it only hides them, so
78 the range calculated for the y-axis will be the same with outliers
79 shown and outliers hidden.
80  
81 =item * $notch
82  
83 If false (default) make a standard box plot. If true, make a notched
84 box plot. Notches are used to compare groups; if the notches of two
85 boxes do not overlap, this suggests that the medians are significantly
86 different.
87  
88 =item * $notchwidth
89  
90 For a notched box plot, width of the notch relative to the body.
91  
92 =back
93  
94 See also L<Chart::GGPlot::Stat::Functions/stat_boxplot>.
95  
96 EOT
97  
98 1 ▶my $geom_boxplot_code = fun (
Subroutine __ANON__: called CC=16 SCAR=34.8
Branch: 1 branch
BranchTrueFalseTotal
@_ > 0 and @_ % 2 and Function::Parameters::_croak("Odd number of paired arguments for fun (anon)") 0 1 1
Condition33%1/3
@_ > 0 and @_ % 2
!ll&&!rl&&r
100
MC/DC0%0/2
@_ > 0 and @_ % 2
@_ > 0 @_ % 2
Truth table33%1/3
A and B
A= @_ > 0B= @_ % 2
ABresult
0X0
100
111
99 :$data=undef, :$mapping=undef,
100 :$stat='boxplot', :$position='dodge2',
101 :$outlier_color=undef, :$outlier_colour=undef,
102 :$outlier_fill=undef, :$outlier_shape=undef,
103 :$outlier_size=1.5, :$outlier_stroke=undef,
104 :$outlier_alpha=undef,
105 :$notch=false, :$notchwidth=0.25,
106 :$varwidth=false, :$na_rm=false,
107 :$show_legend=undef, :$inherit_aes=true,
108 %rest )
109 1 ▶{
Branch: 8 branches
BranchTrueFalseTotal
exists $rest{'stat'} ? : 0 1 1
exists $rest{'position'} ? : 0 1 1
exists $rest{'outlier_size'} ? : 0 1 1
exists $rest{'notch'} ? : 0 1 1
exists $rest{'notchwidth'} ? : 0 1 1
exists $rest{'varwidth'} ? : 0 1 1
exists $rest{'na_rm'} ? : 0 1 1
exists $rest{'inherit_aes'} ? : 0 1 1
110 1 ▶ if ( not Ref::Util::is_ref($position) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not Ref::Util::XS::is_ref($position)) { } 1 0 1
111 1 ▶ if ($varwidth) {
Branch: 1 branch
BranchTrueFalseTotal
if ($varwidth) 0 1 1
112 0 $position = position_dodge2( preserve => 'single' );
113 }
114 }
115 else {
116 0 ▶ if ( $position->preserve eq 'total' and $varwidth ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($position->preserve eq "total" and $varwidth) 0 0 0
Condition0%0/3
$position->preserve eq "total" and $varwidth
!ll&&!rl&&r
000
MC/DC0%0/2
$position->preserve eq "total" and $varwidth
$position->preserve eq "total" $varwidth
Truth table0%0/3
A and B
A= $position->preserve eq "total"B= $varwidth
ABresult
0X0
100
111
117 0 warn "Can't preserve total widths when varwidth is true.";
118 0 $position->preserve('single');
119 }
120 }
121  
122 1 ▶ return Chart::GGPlot::Layer->new(
Condition33%1/3
$outlier_color // $outlier_colour
l!l&&r!l&&!r
001
MC/DC0%0/2
$outlier_color // $outlier_colour
$outlier_color $outlier_colour
Truth table33%1/3
A // B
A= $outlier_colorB= $outlier_colour
ABresult
1X1
011
000
123 data => $data,
124 mapping => $mapping,
125 stat => $stat,
126 geom => 'boxplot',
127 position => $position,
128 show_legend => $show_legend,
129 inherit_aes => $inherit_aes,
130 params => {
131 outlier_color => ( $outlier_color // $outlier_colour ),
132 outlier_fill => $outlier_fill,
133 outlier_shape => $outlier_shape,
134 outlier_size => $outlier_size,
135 outlier_stroke => $outlier_stroke,
136 outlier_alpha => $outlier_alpha,
137 notch => $notch,
138 notchwidth => $notchwidth,
139 varwidth => $varwidth,
140 na_rm => $na_rm,
141 %rest
142 },
143 );
144 5 };
145  
146 5 ▶classmethod ggplot_functions() {
Subroutine ggplot_functions: called CC=3 SCAR=11.3
Pod undocumented
Branch: 2 branches
BranchTrueFalseTotal
@_ < 1 and Function::Parameters::_croak('Too few arguments for classmethod ggplot_functions (expected 1, got ' . @_ . ')') 0 5 5
@_ > 1 and Function::Parameters::_croak('Too many arguments for classmethod ggplot_functions (expected 1, got ' . @_ . ')') 0 5 5
147 return [
148 {
149 5 name => 'geom_boxplot',
150 code => $geom_boxplot_code,
151 pod => $geom_boxplot_pod,
152 }
153 ];
154 5 }
155  
156 1 ▶method setup_data ($data, $params) {
Subroutine setup_data: called CC=10 SCAR=27.0
Pod undocumented
Branch: 2 branches
BranchTrueFalseTotal
@_ < 3 and Function::Parameters::_croak('Too few arguments for method setup_data (expected 3, got ' . @_ . ')') 0 1 1
@_ > 3 and Function::Parameters::_croak('Too many arguments for method setup_data (expected 3, got ' . @_ . ')') 0 1 1
157 1 ▶ unless ( $data->exists('width') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($data->exists("width")) 0 1 1
158 0 ▶ $data->set( 'width',
Condition0%0/3
$params->at("width") // pdl(resolution($data->at("x"), &false()) * "0.9")
l!l&&r!l&&!r
000
MC/DC0%0/2
$params->at("width") // pdl(resolution($data->at("x"), &false()) * "0.9")
$params->at("width") pdl(resolution($data->at("x"), &false()) * "0.9")
Truth table0%0/3
A // B
A= $params->at("width")B= pdl(resolution($data->at("x"), &false()) * "0.9")
ABresult
1X1
011
000
159 $params->at('width')
160 // pdl( resolution( $data->at('x'), false ) * 0.9 ) );
161 }
162  
163 1 ▶ if ( $data->exists('outliers') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->exists("outliers")) 1 0 1
164 1 my $outliers = $data->at('outliers')->unpdl;
165 2 my ( $out_min, $out_max ) = map { pdl( $outliers->map($_) ) } (
166 7 ▶ sub { $_->isempty ? "inf" : $_->min },
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_->isempty ? : 2 5 7
167 7 ▶ sub { $_->isempty ? "-inf" : $_->max }
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_->isempty ? : 2 5 7
168 1 );
169 1 $data->set( 'ymin_final', pmin( $out_min, $data->at('ymin') ) );
170 1 $data->set( 'ymax_final', pmax( $out_max, $data->at('ymax') ) );
171 }
172  
173 # if 'varwidth' not requested or not available, don't use it
174 1 ▶ if ( $params->length == 0
Branch: 1 branch
BranchTrueFalseTotal
if ($params->length == 0 or not $params->at('varwidth') or not $params->at('relvarwidth')) { } 1 0 1
Condition33%2/6
$params->length == 0 or not $params->at("varwidth") or not $params->at("relvarwidth")
l!l&&r!l&&!r
100
$params->length == 0 or not $params->at("varwidth")
l!l&&r!l&&!r
010
MC/DC0%0/3
$params->length == 0 or not $params->at("varwidth") or not $params->at("relvarwidth")
$params->length == 0 not $params->at("varwidth") not $params->at("relvarwidth")
Truth table25%1/4
A or not(B) or not(C)
A= $params->length == 0B= not $params->at("varwidth")C= not $params->at("relvarwidth")
ABCresult
1XX1
01X1
0011
0000
175 or !$params->at('varwidth')
176 or !$params->at('relvarwidth') )
177 {
178 1 $data->set( 'xmin', $data->at('x') - $data->at('width') / 2 );
179 1 $data->set( 'xmax', $data->at('x') + $data->at('width') / 2 );
180 }
181 else {
182 # make 'relvarwidth' relative to the size of the largest group
183 0 my $relvarwidth = $data->at('relvarwidth');
184 0 my $width = $data->at('width');
185 0 $data->set( 'relvarwidth', $relvarwidth / $relvarwidth->max );
186 0 $data->set( 'xmin', $data->at('x') - $relvarwidth * $width / 2 );
187 0 $data->set( 'xmax', $data->at('x') + $relvarwidth * $width / 2 );
188 }
189  
190 1 $data->delete('width');
191 1 ▶ if ($data->exists('relvarwidth')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->exists("relvarwidth")) 1 0 1
192 1 $data->delete('relvarwidth');
193 }
194  
195 1 return $data;
196 5 }
197  
198 5 __PACKAGE__->meta->make_immutable(inline_constructor => 0);
199  
200 5 1;
201  
202 __END__
« Chart/GGPlot/Stat/Identity.pm ↑ Summary Chart/GGPlot/Position/Dodge.pm »