| 1 | package Chart::GGPlot::Stat::Bin; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | # ABSTRACT: Statistic method that gets histogram of data | ||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | 7 | ▶ | use Chart::GGPlot::Class qw(:pdl); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 7 | ▶ | use namespace::autoclean -except => 'stat'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 7 | ▶ | use MooseX::Singleton; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | 7 | our $VERSION = '0.002003'; # VERSION | |||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | 7 | ▶ | use PDL::Primitive qw(which); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 7 | ▶ | use POSIX qw(floor); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | 7 | ▶ | use Chart::GGPlot::Aes::Functions qw(aes); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | 7 | ▶ | use Chart::GGPlot::Bins; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | 7 | ▶ | use Chart::GGPlot::Layer; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | 7 | ▶ | use Chart::GGPlot::Util qw(call_if_coderef seq_n stat); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | 7 | ▶ | use Chart::GGPlot::Util::Pod qw(layer_func_pod); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | 7 | with qw( | |||||||||||||||||||||||||||||||||||||||||
| 21 | Chart::GGPlot::Stat | ||||||||||||||||||||||||||||||||||||||||||
| 22 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | has '+default_aes' => ( | ||||||||||||||||||||||||||||||||||||||||||
| 25 | 1 | ▶ | default => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 26 | 1 | aes( | |||||||||||||||||||||||||||||||||||||||||
| 27 | y => q{stat($count)}, | ||||||||||||||||||||||||||||||||||||||||||
| 28 | weight => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 29 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||||||||||||||||
| 31 | 7 | ); | |||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||
| 33 | 1 | ▶ | classmethod required_aes () { ['x'] } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
required_aes: called CC=3 SCAR=11.2
Pod
undocumented
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | 1 | ▶ | classmethod _parameters () { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_parameters: called CC=3 SCAR=11.3
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | [ | ||||||||||||||||||||||||||||||||||||||||||
| 37 | 1 | qw( | |||||||||||||||||||||||||||||||||||||||||
| 38 | na_rm | ||||||||||||||||||||||||||||||||||||||||||
| 39 | bins binwidth boundary breaks center pad | ||||||||||||||||||||||||||||||||||||||||||
| 40 | ) | ||||||||||||||||||||||||||||||||||||||||||
| 41 | ] | ||||||||||||||||||||||||||||||||||||||||||
| 42 | 7 | } | |||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | 7 | my $stat_bin_pod = layer_func_pod(<<'EOT'); | |||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | stat_bin(:$mapping=undef, :$data=undef, | ||||||||||||||||||||||||||||||||||||||||||
| 47 | :$geom="bar", :$position="stack", | ||||||||||||||||||||||||||||||||||||||||||
| 48 | :$binwidth=undef, :$bins=undef, | ||||||||||||||||||||||||||||||||||||||||||
| 49 | :$center=undef, :$boundary=undef, :$breaks=undef, | ||||||||||||||||||||||||||||||||||||||||||
| 50 | :$pad=false, | ||||||||||||||||||||||||||||||||||||||||||
| 51 | :$na_rm=false, :$show_legend='auto', :$inherit_aes=true, | ||||||||||||||||||||||||||||||||||||||||||
| 52 | %rest) | ||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | Arguments: | ||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||
| 58 | %TMPL_COMMON_ARGS% | ||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | =item * $binwidth | ||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | The width of the bins. | ||||||||||||||||||||||||||||||||||||||||||
| 63 | Can be specified as a numeric value, or a function that calculates width | ||||||||||||||||||||||||||||||||||||||||||
| 64 | from x. The default is to use C<$bins> bins that cover the range of the | ||||||||||||||||||||||||||||||||||||||||||
| 65 | data. | ||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||
| 67 | =item * $bins | ||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | Number of bins. Overridden by C<$binwidth>. Defaults to 30. | ||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | You should always override this C<$bins> or C<$binwidth>, exploring | ||||||||||||||||||||||||||||||||||||||||||
| 72 | multiple widths to find the best to illustrate the stories in your data. | ||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||
| 76 | EOT | ||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | 0 | ▶ | my $stat_bin_code = fun ( | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=9 SCAR=45.0
Branch: 1 branch
Condition0%0/3
@_ > 0 and @_ % 2
MC/DC0%0/2
@_ > 0 and @_ % 2
@_ > 0
@_ % 2
Truth table0%0/3
A and B
A= @_ > 0B= @_ % 2
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | :$mapping=undef, :$data=undef, | ||||||||||||||||||||||||||||||||||||||||||
| 80 | :$geom="bar", :$position="stack", | ||||||||||||||||||||||||||||||||||||||||||
| 81 | :$binwidth=undef, :$bins=undef, | ||||||||||||||||||||||||||||||||||||||||||
| 82 | :$center=undef, :$boundary=undef, :$breaks=undef, | ||||||||||||||||||||||||||||||||||||||||||
| 83 | :$pad=false, | ||||||||||||||||||||||||||||||||||||||||||
| 84 | :$na_rm=false, | ||||||||||||||||||||||||||||||||||||||||||
| 85 | :$show_legend='auto', :$inherit_aes=true, | ||||||||||||||||||||||||||||||||||||||||||
| 86 | %rest ) | ||||||||||||||||||||||||||||||||||||||||||
| 87 | 0 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 6 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 88 | 0 | return Chart::GGPlot::Layer->new( | |||||||||||||||||||||||||||||||||||||||||
| 89 | data => $data, | ||||||||||||||||||||||||||||||||||||||||||
| 90 | mapping => $mapping, | ||||||||||||||||||||||||||||||||||||||||||
| 91 | stat => 'bin', | ||||||||||||||||||||||||||||||||||||||||||
| 92 | geom => $geom, | ||||||||||||||||||||||||||||||||||||||||||
| 93 | position => $position, | ||||||||||||||||||||||||||||||||||||||||||
| 94 | show_legend => $show_legend, | ||||||||||||||||||||||||||||||||||||||||||
| 95 | inherit_aes => $inherit_aes, | ||||||||||||||||||||||||||||||||||||||||||
| 96 | params => { | ||||||||||||||||||||||||||||||||||||||||||
| 97 | binwidth => $binwidth, | ||||||||||||||||||||||||||||||||||||||||||
| 98 | bins => $bins, | ||||||||||||||||||||||||||||||||||||||||||
| 99 | center => $center, | ||||||||||||||||||||||||||||||||||||||||||
| 100 | boundary => $boundary, | ||||||||||||||||||||||||||||||||||||||||||
| 101 | breaks => $breaks, | ||||||||||||||||||||||||||||||||||||||||||
| 102 | pad => $pad, | ||||||||||||||||||||||||||||||||||||||||||
| 103 | na_rm => $na_rm, | ||||||||||||||||||||||||||||||||||||||||||
| 104 | %rest | ||||||||||||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||||||||||||
| 106 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 107 | 7 | }; | |||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||
| 109 | 7 | ▶ | classmethod ggplot_functions () { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
ggplot_functions: called CC=3 SCAR=11.3
Pod
undocumented
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 110 | return [ | ||||||||||||||||||||||||||||||||||||||||||
| 111 | { | ||||||||||||||||||||||||||||||||||||||||||
| 112 | 7 | name => 'stat_bin', | |||||||||||||||||||||||||||||||||||||||||
| 113 | code => $stat_bin_code, | ||||||||||||||||||||||||||||||||||||||||||
| 114 | pod => $stat_bin_pod, | ||||||||||||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||||||||||||
| 116 | ]; | ||||||||||||||||||||||||||||||||||||||||||
| 117 | 7 | } | |||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||
| 119 | 0 | ▶ | method setup_params ($data, $params) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
setup_params: not called CC=7 SCAR=40.3
Pod
undocumented
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 120 | 0 | ▶ | if ( $data->exists('y') ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 121 | 0 | die "stat_count() must not be used with a y aesthetic."; | |||||||||||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||||||||||||
| 123 | 0 | ▶ | if ( !$params->at('breaks') | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$params->at("breaks") or $params->at("binwidth") or $params->at("bins")
$params->at("breaks") or $params->at("binwidth")
MC/DC0%0/3
$params->at("breaks") or $params->at("binwidth") or $params->at("bins")
$params->at("breaks")
$params->at("binwidth")
$params->at("bins")
Truth table0%0/4
A or B or C
A= $params->at("breaks")B= $params->at("binwidth")C= $params->at("bins")
| |||||||||||||||||||||||||||||||||||||||||||
| 124 | and !$params->at('binwidth') | ||||||||||||||||||||||||||||||||||||||||||
| 125 | and !$params->at('bins') ) | ||||||||||||||||||||||||||||||||||||||||||
| 126 | { | ||||||||||||||||||||||||||||||||||||||||||
| 127 | 0 | $params->set( bins => 30 ); | |||||||||||||||||||||||||||||||||||||||||
| 128 | 0 | warn | |||||||||||||||||||||||||||||||||||||||||
| 129 | "'stat_bin()' using 'bins = 30'. Pick better value with 'binwidth'."; | ||||||||||||||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||||||||||||
| 131 | 0 | return $params; | |||||||||||||||||||||||||||||||||||||||||
| 132 | 7 | } | |||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||
| 134 | 0 | ▶ | method compute_group ($data, $scales, $params) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
compute_group: not called CC=7 SCAR=40.3
Pod
undocumented
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 135 | my ( $binwidth, $bins, $center, $boundary, $breaks, $pad ) = | ||||||||||||||||||||||||||||||||||||||||||
| 136 | 0 | map { $params->at($_) } | |||||||||||||||||||||||||||||||||||||||||
| 137 | qw( | ||||||||||||||||||||||||||||||||||||||||||
| 138 | binwidth bins center boundary breaks pad | ||||||||||||||||||||||||||||||||||||||||||
| 139 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||
| 141 | 0 | ▶ | if ( defined $breaks ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 142 | 0 | my $scale_x = $scales->at('x'); | |||||||||||||||||||||||||||||||||||||||||
| 143 | 0 | ▶ | if ( $scale_x->$_DOES('Chart::GGPlot::Scales::Discrete') ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 144 | 0 | $breaks = $scale_x->transform->($breaks); | |||||||||||||||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||||||||||||||||
| 146 | 0 | $bins = Chart::GGPlot::Bins->bin_breaks($breaks); | |||||||||||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||||||||||
| 148 | elsif ( defined $binwidth ) { | ||||||||||||||||||||||||||||||||||||||||||
| 149 | 0 | $binwidth = call_if_coderef( $binwidth, $data->at('x') ); | |||||||||||||||||||||||||||||||||||||||||
| 150 | 0 | $bins = Chart::GGPlot::Bins->bin_breaks_width( | |||||||||||||||||||||||||||||||||||||||||
| 151 | $scales->at('x')->dimension(), | ||||||||||||||||||||||||||||||||||||||||||
| 152 | $binwidth, | ||||||||||||||||||||||||||||||||||||||||||
| 153 | center => $center, | ||||||||||||||||||||||||||||||||||||||||||
| 154 | boundary => $boundary | ||||||||||||||||||||||||||||||||||||||||||
| 155 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||||||||||||||||
| 157 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 158 | 0 | $bins = Chart::GGPlot::Bins->bin_breaks_bins( | |||||||||||||||||||||||||||||||||||||||||
| 159 | $scales->at('x')->dimension(), | ||||||||||||||||||||||||||||||||||||||||||
| 160 | $bins, | ||||||||||||||||||||||||||||||||||||||||||
| 161 | center => $center, | ||||||||||||||||||||||||||||||||||||||||||
| 162 | boundary => $boundary | ||||||||||||||||||||||||||||||||||||||||||
| 163 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||||||
| 166 | 0 | ▶ | return $bins->bin_vector( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 167 | $data->at('x'), | ||||||||||||||||||||||||||||||||||||||||||
| 168 | ( $data->exists('weight') ? ( weight => $data->at('weight') ) : () ), | ||||||||||||||||||||||||||||||||||||||||||
| 169 | maybe pad => $pad | ||||||||||||||||||||||||||||||||||||||||||
| 170 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 171 | 7 | } | |||||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||||||
| 173 | 7 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||||||||
| 175 | 7 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||||||||
| 177 | __END__ | ||||||||||||||||||||||||||||||||||||||||||