| 1 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | 5 | ▶ | use feature 'say'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 5 | ▶ | use warnings FATAL => 'all'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | package Matplotlib::Simple::Ggplot2; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | # The ggplot2 engine: it writes plt's figure as an R script that draws it with | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | # ggplot2, and runs that script with Rscript. Matplotlib::Simple checks the arguments and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | # refuses, by name, every plot type and keyword not listed below; render() is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | # given what is left. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | # The figure is meant to be the one matplotlib draws from the same arguments, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | # so the semantics of each type -- the shapes of "data", the order of the keys, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | # the defaults -- are those of the matching *_helper in Matplotlib::Simple::Matplotlib, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | # and the binning, the box and whisker statistics, the pie's wedges and the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | # summaries of "wide" are worked out here, in Perl, as numpy and matplotlib | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | # work them out, rather than left to ggplot2's own stats, which differ: a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | # ggplot2 histogram bins every set over one shared range, where matplotlib bins | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | # each set over its own; geom_violin's default bandwidth is R's bw.nrd0, where | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | # matplotlib uses Scott's rule. Every position is numeric, at the place | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | # matplotlib puts it -- bars at 0 .. n-1, boxes and violins at 1 .. n -- so that | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | # an "add" graph lands where it would under matplotlib. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | # That is "style => 'matplotlib'", the default. Under "style => 'native'" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | # (%Matplotlib::Simple::styles) ggplot2 decides instead: series given no color take its | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | # default hue scale and a colormap not asked for its default gradient; hist, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | # boxplot, violin and hist2d are drawn by its own stats -- stat_bin, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | # stat_boxplot, stat_ydensity, stat_bin_2d -- with their defaults; bars, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | # boxes and violins stand on a discrete axis of their keys; and the theme is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | # its default, theme_grey. wide and pie have no stat of ggplot2's to compute | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | # them, so their summaries and wedges are still worked out here. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | # Only ggplot2 and base R (grid, grDevices, stats) are used: the data is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | # written into the script as R literals, and the subplots of a grid are placed | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | # with grid's own layouts, not patchwork or cowplot, which ggplot2 does not | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | # depend on. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 5 | require 5.010; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 5 | our $VERSION = 0.3345; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 5 | ▶ | use Scalar::Util 'looks_like_number'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 5 | ▶ | use List::Util qw(max min sum); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 5 | ▶ | BEGIN { require Matplotlib::Simple } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 5 | ▶ | BEGIN { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 5 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 5 | *{$_} = \&{"Matplotlib::Simple::$_"} foreach qw(as_double axis_limits axis_scale bar_series bin_counts bool_option | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | cb_limits check_key_order check_numbers check_scatter_lengths check_scatter_values | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | check_wide_runs color_hex colormap_colors creator_note decoded_text double_literal finite_values | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | is_finite is_nan linspace logscale_axes logspace nonfinite_name plain_text program_from_env | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | run_script script_handle svg_creator_metadata table_cells); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | # The plot types drawn, each with the keywords of Matplotlib::Simple's %opt that are | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | # read for it. hexbin is not here because geom_hex needs the hexbin package, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | # which ggplot2 does not depend on; venn_proportional_area is not here because | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | # ggplot2 has no area-proportional Venn diagram. Neither is any keyword whose | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | # meaning is matplotlib's own: "set_options" and "legend" are Python argument | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | # lists, "tooltips" are drawn into the SVG matplotlib writes, "twinx" is a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | # second y axis, which ggplot2 has only as a transform of the first. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 5 | our %types = ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | bar => [qw(color edgecolor key_order label log logscale stacked width xerr yerr)], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | barh => [qw(color edgecolor key_order label log logscale stacked width xerr yerr)], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | boxplot => [qw(color colors key_order logscale notch orientation showcaps showfliers showmeans)], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | colored_table => [qw(cb_logscale cb_max cb_min cblabel cmap col_labels colorbar_on default_undefined mirror row_labels show_numbers undef_color)], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | hist => [qw(alpha bins color key_order logscale orientation show_legend)], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | hist2d => [qw(cb_logscale cb_max cb_min cblabel cmap cmax cmin colorbar_on density key_order logscale show_colorbar vmax vmin xbins xmax xmin ybins ymax ymin)], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | imshow => [qw(cb_logscale cb_max cb_min cblabel cmap colorbar_on stringmap vmax vmin)], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | pie => [qw(autopct key_order labeldistance pctdistance)], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | plot => [qw(key_order logscale show_legend)], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | scatter => [qw(cb_logscale cb_max cb_min cblabel cmap color_key colorbar_on key_order keys logscale show_legend)], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | violin => [qw(color colors edgecolor key_order logscale medians orientation whiskers)], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | violinplot => [qw(color colors edgecolor key_order logscale medians orientation whiskers)], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | wide => [qw(color show_legend)], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 5 | our @plot_keywords = qw(set_title set_xlabel set_xlim set_xscale set_ylabel set_ylim | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | set_yscale title xlabel xscale ylabel ylim yscale); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | # No "show": Rscript runs the script and exits, so a window it opened would | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | # close as soon as it was drawn. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 5 | our @figure_keywords = qw(scale scalex scaley suptitle); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 5 | my $engine = 'ggplot2'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | # The oldest ggplot2 the script runs with: 3.5.0 brought the "transform" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | # argument of the position scales (before it, "trans") and geom_boxplot's | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | # "staplewidth", which draws the caps matplotlib draws on every whisker. The | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | # script checks it before it draws, and t/18.ggplot2.t gates its render checks | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | # on it. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 5 | our $min_ggplot2 = '3.5.0'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | # matplotlib's default property cycle, "C0" to "C9", from Matplotlib::Simple::Colors: an | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | # artist given no color takes the next of these, per subplot. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 5 | my @cycle = map { color_hex( "C$_", 'the color cycle', $engine, 'Matplotlib::Simple::Ggplot2' ) } 0 .. 9; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | # matplotlib's default sizes, in ggplot2's units: lines 1.5 pt wide, and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | # markers 6 pt across, as ggplot2's "size" (3, measured against matplotlib's | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | # markers in PNGs of the same data at the same size) and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | # "linewidth" (a millimetre per unit, so 1.5 pt / 2.845 pt per mm). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 5 | my $line_mm = 0.53; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 5 | my $marker_mm = 3; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | # The figure, as matplotlib sizes it: 6.4 x 4.8 inches, at 100 dots per inch | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | # for a PNG (matplotlib's rcParams figure.figsize and figure.dpi). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 5 | my ( $fig_w, $fig_h, $png_dpi ) = ( 6.4, 4.8, 100 ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 4917 | ▶ | sub r_str { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
r_str: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | # A Perl string as an R string literal, in ASCII: every character that is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | # not printable ASCII is an escape, so the script reads the same in | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | # whatever encoding Rscript takes it to be in -- on Windows that is not | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | # always UTF-8. R has no way to write a NUL in a string, so one is refused. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 4917 | my ($str) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 4917 | ▶ | $str = decoded_text( $str // '' ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$str // ""
MC/DC0%0/1
$str // ""
$str
Truth table50%1/2
A
A= $str
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 4917 | ▶ | die "Matplotlib::Simple: the $engine engine cannot write text holding a NUL character into R" if $str =~ m/\x00/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 4917 | $str =~ s/\\/\\\\/g; # first, or the escapes below would be doubled | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 4917 | $str =~ s/"/\\"/g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 4917 | $str =~ s/\n/\\n/g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 4917 | $str =~ s/\r/\\r/g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 4917 | $str =~ s/\t/\\t/g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 4917 | ▶ | $str =~ s/([^\x20-\x7e])/ord($1) > 0xffff ? sprintf( '\\U{%x}', ord $1 ) : sprintf( '\\u{%x}', ord $1 )/ge; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 4917 | return qq{"$str"}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 21579 | ▶ | sub r_num { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
r_num: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | # A number as an R numeric literal: NA for undef, NaN, Inf and -Inf for | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | # the values Perl and numpy call nan and inf, and a finite value as | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | # double_literal writes it, as gp_num does for gnuplot. A value already | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | # written as R would write it was passed through as it stood, but Perl | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | # stringifies a computed number to 15 digits, so 1/3 lost its last two. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | 21579 | my ($n) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 21579 | ▶ | return 'NA' unless defined $n; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 21573 | my $nonfinite = nonfinite_name($n); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | 21573 | ▶ | return { nan => 'NaN', inf => 'Inf', '-inf' => '-Inf' }->{$nonfinite} if defined $nonfinite; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | 21572 | return double_literal($n); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | 907 | ▶ | sub r_nums { return 'c(' . join( ', ', map { r_num($_) } @_ ) . ')' } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
r_nums: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 423 | ▶ | sub r_strs { return 'c(' . join( ', ', map { r_str($_) } @_ ) . ')' } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
r_strs: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 311 | ▶ | sub r_frame { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
r_frame: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | # A data.frame literal from ( name => R vector, ... ), in that order. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 311 | my @cols = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | 311 | my @parts; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | 311 | ▶ | while ( my ( $name, $vector ) = splice @cols, 0, 2 ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | 896 | push @parts, "$name = $vector"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | 311 | return 'data.frame(' . join( ', ', @parts, 'stringsAsFactors = FALSE' ) . ')'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | 120 | ▶ | sub next_color { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
next_color: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | # The next color of matplotlib's cycle for this subplot, for an artist of | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | # the kind given: 'line' (plot, violinplot) or 'patch' (bar, hist, pie, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | # scatter). matplotlib keeps one cycle of each per axes -- _get_lines and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | # _get_patches_for_fill -- so a scatter over a line both start at C0. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | # Under "native" there is no cycle, and undef leaves the color to ggplot2. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | 120 | my ( $s, $kind ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | 120 | ▶ | return undef if $s->{native}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | 91 | return $cycle[ $s->{cycle}{$kind}++ % scalar @cycle ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | 50 | ▶ | sub color_of { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
color_of: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | # color_hex, naming the option, the key and the plot in what it refuses. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | 50 | my ( $color, $what, $sub ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | 50 | return color_hex( $color, $what, $engine, $sub ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | 44 | ▶ | sub percentile { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
percentile: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | # numpy.percentile's default, "linear", of a sorted list: the value at | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | # rank (n - 1) * p, interpolated. R's quantile() type 7, which | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | # geom_boxplot uses, is the same rule. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | 44 | my ( $sorted, $p ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | 44 | my $h = ( scalar @{$sorted} - 1 ) * $p; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | 44 | my $lo = int $h; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | 44 | ▶ | return $sorted->[$lo] if $lo >= $#{$sorted}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | 44 | return $sorted->[$lo] + ( $h - $lo ) * ( $sorted->[ $lo + 1 ] - $sorted->[$lo] ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | 32 | ▶ | sub with_commas { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
with_commas: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | # A whole number with a comma every three digits, as format_commas in | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | # Matplotlib::Simple::Matplotlib writes the counts under a box or a violin. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | 32 | my ($n) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | 32 | ▶ | 1 while $n =~ s/^(\d+)(\d{3})/$1,$2/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | 32 | return $n; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | 11 | ▶ | sub colormap_at { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
colormap_at: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | # A colormap's color at a point from 0 to 1, interpolated between the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | # stops Matplotlib::Simple::Colors keeps, as "#rrggbb". | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | 11 | my ( $stops, $t ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | 11 | my $pos = $t * ( scalar @{$stops} - 1 ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | 11 | my $i = min( int $pos, $#{$stops} - 1 ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | 11 | my $f = $pos - $i; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | 11 | my @a = map { hex } substr( $stops->[$i], 1 ) =~ m/(..)(..)(..)/; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | 11 | my @b = map { hex } substr( $stops->[ $i + 1 ], 1 ) =~ m/(..)(..)(..)/; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | 11 | return '#' . join '', map { sprintf '%02x', int( $a[$_] + $f * ( $b[$_] - $a[$_] ) + 0.5 ) } 0 .. 2; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | 262 | ▶ | sub bin_index { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
bin_index: called CC=6 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | # The bin of $v among ascending $edges, numpy.histogram's way: each bin | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | # holds its left edge, the last holds its right edge too, and a value | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | # outside them all is in none (undef). The edges are doubles, and $v is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | # rounded to one before it is compared with them; see as_double. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | 262 | my ( $edges, $v ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | 262 | $v = as_double($v); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | 262 | ▶ | return undef if ( $v < $edges->[0] ) || ( $v > $edges->[-1] ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$v < $edges->[0] or $v > $edges->[-1]
MC/DC50%1/2
$v < $edges->[0] or $v > $edges->[-1]
$v < $edges->[0]
$v > $edges->[-1]
Truth table66%2/3
A or B
A= $v < $edges->[0]B= $v > $edges->[-1]
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | 261 | ▶ | return $#{$edges} - 1 if $v == $edges->[-1]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | 237 | my ( $lo, $hi ) = ( 0, $#{$edges} ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | 237 | ▶ | while ( $hi - $lo > 1 ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | 632 | my $mid = int( ( $lo + $hi ) / 2 ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | 632 | ▶ | if ( $v >= $edges->[$mid] ) { $lo = $mid } else { $hi = $mid } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | 237 | return $lo; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | 201 | ▶ | sub new_panel { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new_panel: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | # One subplot as it is built up: its layers, in the order they are drawn, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | # and what its scales, legend and labels need to say. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | return { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | 201 | ▶ | layers => [], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | colour_d => [], # [ legend name, color ] of lines and points | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | fill_d => [], # [ legend name, color ] of areas | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | shape_d => [], # [ legend name, R point shape ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | gradient => undef, # the one continuous color scale, if any | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | axis => { x => {}, y => {} }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | default_label => {}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | cycle => { line => 0, patch => 0 }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | kind => 'cartesian', # 'cartesian', 'pie' or 'table' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | native => $_[0] ? 1 : 0, # 1 = style "native", 0 = style "matplotlib" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | 94 | ▶ | sub add_legend { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add_legend: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | # Add a series to a legend, once: a name already there keeps its color. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | # Under "native" a color may be undef, for ggplot2's hue scale to fill in. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | 94 | my ( $list, $name, $value ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | 94 | ▶ | push @{$list}, [ $name, $value ] unless grep { $_->[0] eq $name } @{$list}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | 38 | ▶ | sub set_gradient { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
set_gradient: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | # The subplot's continuous color scale. ggplot2 has one per aesthetic, so | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | # a second -- an "add" graph that colors by value over a plot that already | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | # does -- is refused rather than drawn against the first one's colors. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | 38 | my ( $s, $gradient, $where, $sub ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | 38 | ▶ | if ( defined $s->{gradient} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | 1 | die "$sub: the $engine engine draws one color scale per plot, and $gradient->{type}$where would be a second one beside $s->{gradient}{type}'s"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | 37 | $s->{gradient} = $gradient; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | 155 | ▶ | sub key_order_of { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
key_order_of: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | # The keys in the order drawn: "key_order", or sorted. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | 155 | my ( $plot, $data, $option, $sub ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | 155 | ▶ | return check_key_order({ keys => $plot->{$option}, data => $data, option => $option, sub => $sub }) if defined $plot->{$option}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | 150 | return sort keys %{$data}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | 30 | ▶ | sub value_axis_log { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
value_axis_log: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | # The axes "logscale" names, refusing the one that holds the keys of a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | # boxplot or violin, which are categories, not a scale. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | 30 | my ( $plot, $key_axis, $sub ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | 30 | my @axes = logscale_axes( $plot->{logscale}, $sub ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | 30 | ▶ | if ( grep { $_ eq $key_axis } @axes ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | 1 | die "$sub: \"logscale\" asks for a log $key_axis axis, but the $key_axis axis of this $plot->{plot_type} holds its keys; the $engine engine puts only the axis of values on a log scale"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | 29 | return @axes; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | 31 | ▶ | sub line_width { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
line_width: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | # The width of a line, as an argument: matplotlib's 1.5 pt, or nothing, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | # for ggplot2's own 0.5 mm, under "native". | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | 31 | my ($s) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | 31 | ▶ | return $s->{native} ? '' : ", linewidth = $line_mm"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | 25 | ▶ | sub marker_size { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
marker_size: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | # The size of a marker, as line_width gives a line's. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | 25 | my ($s) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | 25 | ▶ | return $s->{native} ? '' : ", size = $marker_mm"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | 55 | ▶ | sub category_ticks { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
category_ticks: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | # Tick positions and labels for the axis that holds the keys. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | 55 | my ( $s, $axis, $positions, $labels ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | 55 | $s->{axis}{$axis}{breaks} = $positions; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | 55 | $s->{axis}{$axis}{labels} = $labels; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | # One drawer per plot type, each adding its layers to the subplot $s. Each is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | # barplot_helper, boxplot_helper and the rest of Matplotlib::Simple::Matplotlib over | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | # again, with the same checks, which die before anything is written. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | 61 | ▶ | sub draw_bar { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
draw_bar: called CC=66 SCAR=47.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | 61 | my ( $plot, $s, $where, $sub ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | 61 | ▶ | my $horizontal = ( $plot->{plot_type} eq 'barh' ) ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | 61 | ▶ | my ( $pos_axis, $val_axis ) = $horizontal ? ( 'y', 'x' ) : ( 'x', 'y' ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | 61 | my $data = $plot->{data}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | 61 | my %ref_counts; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | 61 | $ref_counts{ ref $data->{$_} }++ foreach keys %{$data}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | 61 | ▶ | if ( scalar keys %ref_counts > 1 ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | 0 | die "$sub: different kinds of data were entered$where, which should be a simple hash, a hash of arrays or a hash of hashes"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | 61 | my ( $form, @series_names, %series_color ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | 61 | my %values; # key => array of one value per series | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | 61 | ▶ | if ( defined $ref_counts{''} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | 35 | $form = 'simple'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | 35 | $values{$_} = [ $data->{$_} ] foreach keys %{$data}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | } elsif ( defined $ref_counts{ARRAY} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | 5 | $form = 'grouped'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | 5 | $values{$_} = $data->{$_} foreach keys %{$data}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | 5 | ▶ | if ( defined $plot->{label} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | 1 | ▶ | die "$sub: \"label\"$where is an array of one label per series" unless ref $plot->{label} eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | 1 | @series_names = @{ $plot->{label} }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | } elsif ( defined $ref_counts{HASH} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | 21 | $form = 'grouped'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | 21 | @series_names = bar_series({ plot => $plot, sub => $sub . $where }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | 17 | foreach my $k1 ( keys %{$data} ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | 24 | $values{$k1} = [ @{ $data->{$k1} }{@series_names} ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | 17 | ▶ | if ( ref $plot->{color} eq 'HASH' ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | 0 | my @uncolored = grep { not defined $plot->{color}{$_} } @series_names; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | 0 | ▶ | die "$sub: the series " . join( ', ', map { "\"$_\"" } @uncolored ) . " have no color in \"color\"$where" if scalar @uncolored > 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | 0 | %series_color = map { $_ => $plot->{color}{$_} } @series_names; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | 0 | die "$sub: \"data\"$where holds " . join( ', ', sort keys %ref_counts ) . ' references, which bar and barh do not draw'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | 57 | my $stacked = bool_option( $plot->{stacked}, 'stacked', $sub ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | 57 | ▶ | if ( defined $plot->{width} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | 4 | ▶ | die "$sub: \"width\" is one number for every bar, not a " . ref( $plot->{width} ) . ' reference' if ref $plot->{width}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 304 | 4 | check_numbers( [ $plot->{width} ], '"width"', $sub ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 305 | 4 | ▶ | die "$sub: \"width\" must be a finite number, not \"$plot->{width}\"" unless is_finite( $plot->{width} ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 306 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 307 | 57 | ▶ | if ( ( $form eq 'grouped' ) && ( defined $plot->{width} ) && ( not $stacked ) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$form eq "grouped" and defined $plot->{"width"} and not $stacked
$form eq "grouped" and defined $plot->{"width"}
MC/DC66%2/3
$form eq "grouped" and defined $plot->{"width"} and not $stacked
$form eq "grouped"
defined $plot->{"width"}
not $stacked
Truth table75%3/4
A and B and not(C)
A= $form eq "grouped"B= defined $plot->{"width"}C= not $stacked
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 308 | # Left out, as the matplotlib engine leaves it; reset only here, the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 309 | # native style's geom_col and a log axis's bars still drew it. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 310 | 1 | say STDERR 'grouped, non-stacked barplots ignore width settings'; # as matplotlib's engine says | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 311 | 1 | delete $plot->{width}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 312 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 313 | 57 | ▶ | my $width = $plot->{width} // 0.8; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$plot->{"width"} // "0.8"
MC/DC100%1/1
$plot->{"width"} // "0.8"
$plot->{"width"}
Truth table100%2/2
A
A= $plot->{"width"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 314 | 57 | my @keys = key_order_of( $plot, $data, 'key_order', $sub ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 315 | 57 | ▶ | if ( ( $form eq 'grouped' ) && ( ref $plot->{color} eq 'HASH' ) && ( not %series_color ) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
$form eq "grouped" and ref $plot->{"color"} eq "HASH" and not %series_color
$form eq "grouped" and ref $plot->{"color"} eq "HASH"
MC/DC0%0/3
$form eq "grouped" and ref $plot->{"color"} eq "HASH" and not %series_color
$form eq "grouped"
ref $plot->{"color"} eq "HASH"
not %series_color
Truth table50%2/4
A and B and not(C)
A= $form eq "grouped"B= ref $plot->{"color"} eq "HASH"C= not %series_color
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 316 | 0 | die "$sub: \"color\" is a HASH, but this data is a hash of arrays, whose series have no names to key it by: give one color per series as an array, e.g. color => ['red','blue']"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 317 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 318 | 57 | foreach my $key (@keys) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 319 | 94 | ▶ | my @bad = grep { ( defined $_ ) && ( not looks_like_number($_) ) } @{ $values{$key} }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
defined $_ && !&looks_like_number($_)
MC/DC0%0/2
defined $_ && !&looks_like_number($_)
defined $_
!&looks_like_number($_)
Truth table66%2/3
A && not(B)
A= defined $_B= !&looks_like_number($_)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 320 | 94 | ▶ | die "$sub: \"$key\"$where has values that are not numbers: \"" . join( '", "', @bad ) . '"' if scalar @bad > 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 321 | 94 | ▶ | die "$sub: \"$key\" has an undefined value, which $plot->{plot_type} cannot draw" if ( $form eq 'simple' ) && ( not defined $values{$key}[0] ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$form eq "simple" and not defined $values{$key}[0]
MC/DC0%0/2
$form eq "simple" and not defined $values{$key}[0]
$form eq "simple"
not defined $values{$key}[0]
Truth table66%2/3
A and not(B)
A= $form eq "simple"B= not defined $values{$key}[0]
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 322 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 323 | 57 | my $n_series = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 324 | 57 | ▶ | if ( $form eq 'grouped' ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 325 | 22 | my %lengths = map { scalar @{ $values{$_} } => 1 } @keys; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 326 | 22 | ▶ | die "$sub: every key of a grouped $plot->{plot_type} needs the same number of values$where" if scalar keys %lengths > 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 327 | 22 | ($n_series) = keys %lengths; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 328 | 22 | ▶ | die "$sub: every key of \"data\" holds an empty array, so $plot->{plot_type} has no bars to draw" if $n_series == 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 329 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 330 | 57 | ▶ | my $log = bool_option( $plot->{log}, 'log', $sub ) || $plot->{logscale}; # "logscale" takes Perl truth, as documented | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
bool_option($plot->{'log'}, 'log', $sub) || $plot->{'logscale'}
MC/DC50%1/2
bool_option($plot->{'log'}, 'log', $sub) || $plot->{'logscale'}
bool_option($plot->{'log'}, 'log', $sub)
$plot->{'logscale'}
Truth table66%2/3
A || B
A= bool_option($plot->{'log'}, 'log', $sub)B= $plot->{'logscale'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 331 | 57 | ▶ | $s->{axis}{$val_axis}{log} = 1 if $log; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 332 | 57 | my $n = scalar @keys; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 333 | # Edge colors: one for all, an array cycled over the bars, or a hash by key | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 334 | # (a simple hash alone has a bar per key to key it by). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 335 | 57 | ▶ | my $edge_of = sub { 'NA' }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 336 | 57 | ▶ | if ( defined $plot->{edgecolor} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 337 | 3 | my $edge = $plot->{edgecolor}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 338 | 3 | ▶ | if ( ref $edge eq 'ARRAY' ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 339 | 0 | ▶ | die "$sub: \"edgecolor\"$where is an empty array" if scalar @{$edge} == 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 340 | 0 | my @hex = map { r_str( color_of( $_, '"edgecolor"', $sub ) ) } @{$edge}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 341 | 0 | ▶ | $edge_of = sub { $hex[ $_[0] % scalar @hex ] }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 342 | } elsif ( ref $edge eq 'HASH' ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 343 | 0 | ▶ | die "$sub: \"edgecolor\" is a HASH, which is keyed by bar and so only fits a simple hash of one bar per key: give a grouped plot one color, or an array of one per key" if $form eq 'grouped'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 344 | 0 | my @uncolored = grep { not defined $edge->{$_} } @keys; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 345 | 0 | ▶ | die "$sub: the keys " . join( ', ', map { "\"$_\"" } @uncolored ) . " have no color in \"edgecolor\"" if scalar @uncolored > 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 346 | 0 | my @hex = map { r_str( color_of( $edge->{$_}, "\"edgecolor\" of \"$_\"", $sub ) ) } @keys; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 347 | 0 | ▶ | $edge_of = sub { $hex[ $_[0] ] }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 348 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 349 | 3 | my $hex = r_str( color_of( $edge, '"edgecolor"', $sub ) ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 350 | 3 | ▶ | $edge_of = sub { $hex }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 351 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 352 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 353 | 57 | ▶ | if ( ( $form eq 'grouped' ) && ( grep { defined $plot->{$_} } ( 'xerr', 'yerr' ) ) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$form eq "grouped" and grep {defined $plot->{$_};} "xerr", "yerr"
MC/DC100%2/2
$form eq "grouped" and grep {defined $plot->{$_};} "xerr", "yerr"
$form eq "grouped"
grep {defined $plot->{$_};} "xerr", "yerr"
Truth table100%3/3
A and B
A= $form eq "grouped"B= grep {defined $plot->{$_};} "xerr", "yerr"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 354 | 1 | my ($err) = grep { defined $plot->{$_} } ( 'xerr', 'yerr' ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 355 | 1 | die "$sub: \"$err\"$where: the $engine engine draws error bars on a bar chart of one bar per key, not on a grouped or stacked one"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 356 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 357 | 56 | ▶ | if ( $s->{native} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 358 | 30 | draw_bar_native({ plot => $plot, s => $s, keys => \@keys, values => \%values, form => $form, n_series => $n_series, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 359 | series_names => \@series_names, series_color => \%series_color, stacked => $stacked, edge_of => $edge_of, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 360 | horizontal => $horizontal, where => $where, sub => $sub }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 361 | 30 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 362 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 363 | 26 | my ( @rects, @fills, @edges, @legend_names ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 364 | 26 | ▶ | if ( $form eq 'simple' ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 365 | 16 | my @colors = simple_bar_colors( $plot, \@keys, $where, $sub ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 366 | 15 | ▶ | @colors = (next_color( $s, 'patch' )) x $n if scalar @colors == 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 367 | 15 | foreach my $i ( 0 .. $n - 1 ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 368 | 29 | push @rects, [ $i - $width / 2, $i + $width / 2, 0, $values{ $keys[$i] }[0] ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 369 | 29 | push @fills, $colors[$i]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 370 | 29 | push @edges, $edge_of->($i); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 371 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 372 | 15 | draw_bar_errors( $plot, $s, \@keys, $horizontal, $where, $sub ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 373 | 14 | category_ticks( $s, $pos_axis, [ 0 .. $n - 1 ], \@keys ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 374 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 375 | 10 | ▶ | my $bar_w = $stacked ? $width : $width / ( $n_series + 1 ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 376 | 10 | my @bottom = (0) x $n; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 377 | 10 | my @tick = (0) x $n; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 378 | 10 | foreach my $i ( 0 .. $n_series - 1 ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 379 | 20 | my $color; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 380 | 20 | ▶ | if (%series_color) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 381 | 0 | $color = color_of( $series_color{ $series_names[$i] }, "\"color\" of \"$series_names[$i]\"", $sub ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 382 | ▶ | } elsif ( ( ref $plot->{color} eq 'ARRAY' ) && ( defined $plot->{color}[$i] ) ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
ref $plot->{"color"} eq "ARRAY" and defined $plot->{"color"}[$i]
MC/DC50%1/2
ref $plot->{"color"} eq "ARRAY" and defined $plot->{"color"}[$i]
ref $plot->{"color"} eq "ARRAY"
defined $plot->{"color"}[$i]
Truth table66%2/3
A and B
A= ref $plot->{"color"} eq "ARRAY"B= defined $plot->{"color"}[$i]
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 383 | 2 | $color = color_of( $plot->{color}[$i], '"color"', $sub ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 384 | ▶ | } elsif ( ( defined $plot->{color} ) && ( not ref $plot->{color} ) ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
defined $plot->{"color"} and not ref $plot->{"color"}
MC/DC0%0/2
defined $plot->{"color"} and not ref $plot->{"color"}
defined $plot->{"color"}
not ref $plot->{"color"}
Truth table33%1/3
A and not(B)
A= defined $plot->{"color"}B= not ref $plot->{"color"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 385 | 0 | $color = color_of( $plot->{color}, '"color"', $sub ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 386 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 387 | 18 | $color = next_color( $s, 'patch' ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 388 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 389 | 20 | my $label = $series_names[$i]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 390 | 20 | ▶ | add_legend( $s->{fill_d}, $label, $color ) if defined $label; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 391 | 20 | foreach my $k ( 0 .. $n - 1 ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 392 | 30 | ▶ | my $v = $values{ $keys[$k] }[$i] // 0; # every series has a bar at every key | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$values{$keys[$k]}[$i] // 0
MC/DC100%1/1
$values{$keys[$k]}[$i] // 0
$values{$keys[$k]}[$i]
Truth table100%2/2
A
A= $values{$keys[$k]}[$i]
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 393 | 30 | ▶ | my $centre = $stacked ? $k : $k + $i * $bar_w; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 394 | 30 | $tick[$k] += $centre; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 395 | 30 | push @rects, [ $centre - $bar_w / 2, $centre + $bar_w / 2, $bottom[$k], $bottom[$k] + $v ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 396 | 30 | ▶ | $bottom[$k] += $v if $stacked; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 397 | 30 | push @fills, $color; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 398 | 30 | push @edges, $edge_of->($k); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 399 | 30 | push @legend_names, $label; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 400 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 401 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 402 | 10 | category_ticks( $s, $pos_axis, [ map { $_ / $n_series } @tick ], \@keys ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 403 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 404 | 24 | ▶ | if ($log) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 405 | # A bar stands on 0, which a log axis cannot place: log10(0) is -Inf, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 406 | # which ggplot2 leaves out of the axis's range, so a bar of 1 had no | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 407 | # height above an axis that started at 1. matplotlib's axis starts its | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 408 | # 5% margin below the lowest bar top, in log units, with each bar drawn | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 409 | # up from the bottom edge; so each bar here stands on that point, and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 410 | # the axis is given no margin below it. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 411 | 2 | my @tops = grep { $_ > 0 } map { $_->[3] } @rects; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 412 | 2 | ▶ | if (@tops) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 413 | 2 | my ( $lo, $hi ) = ( log10( min(@tops) ), log10( max(@tops) ) ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 414 | 2 | ▶ | my $floor = ( $hi > $lo ) ? 10**( $lo - 0.05 * ( $hi - $lo ) ) : 10**( $lo - 1 ); # a decade below, for bars all of one height | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 415 | 2 | $_->[2] = $floor foreach grep { $_->[2] <= 0 } @rects; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 416 | 2 | $s->{axis}{$val_axis}{floor} = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 417 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 418 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 419 | 24 | ▶ | my ( $p_lo, $p_hi, $v_lo, $v_hi ) = $horizontal ? ( 'ymin', 'ymax', 'xmin', 'xmax' ) : ( 'xmin', 'xmax', 'ymin', 'ymax' ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 420 | my @cols = ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 421 | 58 | $p_lo => r_nums( map { $_->[0] } @rects ), | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 422 | 58 | $p_hi => r_nums( map { $_->[1] } @rects ), | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 423 | 58 | $v_lo => r_nums( map { $_->[2] } @rects ), | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 424 | 24 | $v_hi => r_nums( map { $_->[3] } @rects ), | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 425 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 426 | 24 | my $aes = 'xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 427 | 24 | my $fill; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 428 | 24 | ▶ | if ( grep { defined } @legend_names ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 429 | # The bars of a labelled series are mapped to the legend; matplotlib | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 430 | # leaves a series out of it when "label" has no name for it. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 431 | 9 | my @mapped = grep { defined $legend_names[$_] } 0 .. $#rects; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 432 | 9 | my @plain = grep { not defined $legend_names[$_] } 0 .. $#rects; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 433 | 0 | ▶ | push @{ $s->{layers} }, rect_layer( \@rects, [ @fills[@plain] ], [ @edges[@plain] ], \@plain, $horizontal ) if @plain; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 434 | 9 | push @{ $s->{layers} }, 'ggplot2::geom_rect(data = ' . r_frame( map_rects( \@rects, \@mapped, $horizontal ), s => r_strs( @legend_names[@mapped] ) ) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 435 | . ", ggplot2::aes($aes, fill = s), colour = c(" . join( ', ', @edges[@mapped] ) . '), na.rm = TRUE)'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 436 | 9 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 437 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 438 | 15 | push @{ $s->{layers} }, rect_layer( \@rects, \@fills, \@edges, [ 0 .. $#rects ], $horizontal ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 439 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 440 | 35 | ▶ | sub simple_bar_colors { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
simple_bar_colors: called CC=6 SCAR=20.7
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 441 | # The fill of each bar of a bar chart of one bar per key, from "color": a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 442 | # hash by key, an array cycled over the bars, or one for all. An empty list | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 443 | # where "color" is not given, for the caller's default. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 444 | 35 | my ( $plot, $keys, $where, $sub ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 445 | 35 | my $color = $plot->{color}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 446 | 35 | my $n = scalar @{$keys}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 447 | 35 | ▶ | if ( ref $color eq 'HASH' ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 448 | 1 | my @uncolored = grep { not defined $color->{$_} } @{$keys}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 449 | 0 | ▶ | die "$sub: the keys " . join( ', ', map { "\"$_\"" } @uncolored ) . ' were not defined in the colors hash' if scalar @uncolored > 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 450 | 1 | return map { color_of( $color->{$_}, "\"color\" of \"$_\"", $sub ) } @{$keys}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 451 | } elsif ( ref $color eq 'ARRAY' ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 452 | 0 | ▶ | die "$sub: \"color\"$where is an empty array" if scalar @{$color} == 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 453 | 0 | my @hex = map { color_of( $_, '"color"', $sub ) } @{$color}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 454 | 0 | return map { $hex[ $_ % scalar @hex ] } 0 .. $n - 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 455 | } elsif ( defined $color ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 456 | 2 | return (color_of( $color, '"color"', $sub )) x $n; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 457 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 458 | 32 | return (); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 459 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 460 | 23 | ▶ | sub r_factor { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
r_factor: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 461 | # An R factor of the values given, its levels the distinct values in the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 462 | # order first given, so that a discrete axis or legend keeps that order | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 463 | # rather than R's alphabetical one. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 464 | 23 | my @values = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 465 | 23 | my %seen; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 466 | 23 | my @levels = grep { not $seen{$_}++ } @values; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 467 | 23 | return 'factor(' . r_strs(@values) . ', levels = ' . r_strs(@levels) . ')'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 468 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 469 | 30 | ▶ | sub draw_bar_native { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
draw_bar_native: called CC=26 SCAR=33.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 470 | # A bar chart as ggplot2 draws one: geom_col on a discrete axis of the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 471 | # keys, at its own width and fill unless "width" or "color" is given, a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 472 | # grouped chart dodged and a stacked one stacked, its series in the hue | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 473 | # scale. The checks are draw_bar's, already made. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 474 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 475 | # A stack is reversed from ggplot2's own order, which puts the first | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 476 | # series on top: the series are in the caller's order ("label", for a hash | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 477 | # of hashes), and the first is at the bottom, as matplotlib draws it and as | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 478 | # log_bar_layer draws it here. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 479 | 30 | my ($a) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 480 | 30 | my ( $plot, $s, $keys, $values, $where, $sub ) = @{$a}{qw(plot s keys values where sub)}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 481 | 30 | my $horizontal = $a->{horizontal}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 482 | 30 | ▶ | my $pos_axis = $horizontal ? 'y' : 'x'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 483 | 30 | $s->{discrete}{$pos_axis} = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 484 | 30 | ▶ | my $aes = $horizontal ? 'x = v, y = k' : 'x = k, y = v'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 485 | 30 | ▶ | my $extra = ( defined $plot->{width} ? ', width = ' . r_num( $plot->{width} ) : '' ) . ( $horizontal ? ', orientation = "y"' : '' ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 486 | 30 | my $n = scalar @{$keys}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 487 | 30 | my $edges = defined $plot->{edgecolor}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 488 | # On a log value axis geom_col stands its bars on 0 of the transformed | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 489 | # scale, which is 1, so a bar of 1 had no height at all, and one below 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 490 | # hung down from it. There the bars are rectangles from a tenth of the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 491 | # least value -- the decade below it, where the gnuplot engine's native | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 492 | # bars start -- laid out as geom_col would lay them (log_bar_layer). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 493 | 30 | ▶ | my $log = $s->{axis}{ $horizontal ? 'x' : 'y' }{log}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 494 | 30 | ▶ | if ( $a->{form} eq 'simple' ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 495 | 19 | my @colors = simple_bar_colors( $plot, $keys, $where, $sub ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 496 | 19 | ▶ | if ($log) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 497 | # "grey35" is geom_col's own fill, which a rectangle does not have | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 498 | 1 | ▶ | log_bar_layer( $a, [ map { [ $_, 0, $values->{ $keys->[$_] }[0] ] } 0 .. $n - 1 ], 1, [ @colors ? @colors : ('grey35') ], [ map { $a->{edge_of}->($_) } 0 .. $n - 1 ] ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 499 | 1 | draw_bar_errors( $plot, $s, $keys, $horizontal, $where, $sub ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 500 | 1 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 501 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 502 | 18 | ▶ | $extra .= ', fill = ' . r_strs(@colors) if @colors; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 503 | 18 | ▶ | $extra .= ', colour = c(' . join( ', ', map { $a->{edge_of}->($_) } 0 .. $n - 1 ) . ')' if $edges; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 504 | 18 | push @{ $s->{layers} }, 'ggplot2::geom_col(data = ' . r_frame( k => r_factor( @{$keys} ), v => r_nums( map { $values->{$_}[0] } @{$keys} ) ) . ", ggplot2::aes($aes)$extra, na.rm = TRUE)"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 505 | 18 | draw_bar_errors( $plot, $s, $keys, $horizontal, $where, $sub ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 506 | 18 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 507 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 508 | # Series with no name -- a hash of arrays without "label" -- are still told | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 509 | # apart by color, by their place, with no legend to name them. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 510 | 11 | ▶ | my @names = map { $a->{series_names}[$_] // ( $_ + 1 ) } 0 .. $a->{n_series} - 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$a->{"series_names"}[$_] // $_ + 1
MC/DC100%1/1
$a->{"series_names"}[$_] // $_ + 1
$a->{"series_names"}[$_]
Truth table100%2/2
A
A= $a->{"series_names"}[$_]
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 511 | 11 | ▶ | $s->{guide_none}{fill} = 1 unless grep { defined } @{ $a->{series_names} }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 512 | 11 | foreach my $i ( 0 .. $#names ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 513 | 21 | my $color; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 514 | 21 | ▶ | if ( %{ $a->{series_color} } ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 515 | 0 | $color = color_of( $a->{series_color}{ $names[$i] }, "\"color\" of \"$names[$i]\"", $sub ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 516 | ▶ | } elsif ( ( ref $plot->{color} eq 'ARRAY' ) && ( defined $plot->{color}[$i] ) ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
ref $plot->{"color"} eq "ARRAY" and defined $plot->{"color"}[$i]
MC/DC0%0/2
ref $plot->{"color"} eq "ARRAY" and defined $plot->{"color"}[$i]
ref $plot->{"color"} eq "ARRAY"
defined $plot->{"color"}[$i]
Truth table33%1/3
A and B
A= ref $plot->{"color"} eq "ARRAY"B= defined $plot->{"color"}[$i]
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 517 | 0 | $color = color_of( $plot->{color}[$i], '"color"', $sub ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 518 | ▶ | } elsif ( ( defined $plot->{color} ) && ( not ref $plot->{color} ) ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
defined $plot->{"color"} and not ref $plot->{"color"}
MC/DC0%0/2
defined $plot->{"color"} and not ref $plot->{"color"}
defined $plot->{"color"}
not ref $plot->{"color"}
Truth table33%1/3
A and not(B)
A= defined $plot->{"color"}B= not ref $plot->{"color"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 519 | 0 | $color = color_of( $plot->{color}, '"color"', $sub ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 520 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 521 | 21 | add_legend( $s->{fill_d}, $names[$i], $color ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 522 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 523 | 11 | my ( @k, @series, @v, @edge ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 524 | 11 | foreach my $i ( 0 .. $#names ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 525 | 21 | foreach my $j ( 0 .. $n - 1 ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 526 | 32 | push @k, $keys->[$j]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 527 | 32 | push @series, $names[$i]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 528 | 32 | ▶ | push @v, $values->{ $keys->[$j] }[$i] // 0; # every series has a bar at every key | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$$values{$keys->[$j]}[$i] // 0
MC/DC0%0/1
$$values{$keys->[$j]}[$i] // 0
$$values{$keys->[$j]}[$i]
Truth table50%1/2
A
A= $$values{$keys->[$j]}[$i]
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 529 | 32 | push @edge, $a->{edge_of}->($j); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 530 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 531 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 532 | 11 | ▶ | if ($log) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 533 | 1 | ▶ | my @rows = map { my $i = $_; map { [ $_, $i, $values->{ $keys->[$_] }[$i] // 0 ] } 0 .. $n - 1 } 0 .. $#names; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$$values{$keys->[$_]}[$i] // 0
MC/DC0%0/1
$$values{$keys->[$_]}[$i] // 0
$$values{$keys->[$_]}[$i]
Truth table50%1/2
A
A= $$values{$keys->[$_]}[$i]
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 534 | 1 | log_bar_layer( $a, \@rows, scalar @names, undef, \@edge, \@names ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 535 | 1 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 536 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 537 | 10 | ▶ | $extra .= ', colour = c(' . join( ', ', @edge ) . ')' if $edges; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 538 | 10 | ▶ | push @{ $s->{layers} }, 'ggplot2::geom_col(data = ' . r_frame( k => 'factor(' . r_strs(@k) . ', levels = ' . r_strs( @{$keys} ) . ')', s => 'factor(' . r_strs(@series) . ', levels = ' . r_strs(@names) . ')', v => r_nums(@v) ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 539 | 10 | . ", ggplot2::aes($aes, fill = s), position = " . ( $a->{stacked} ? 'ggplot2::position_stack(reverse = TRUE)' : '"dodge"' ) . "$extra, na.rm = TRUE)"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 540 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 541 | 2 | ▶ | sub log_bar_layer { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
log_bar_layer: called CC=17 SCAR=28.7
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 542 | # Native bars on a log value axis, as rectangles laid out the way geom_col | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 543 | # lays out its bars -- 0.9 of a category wide, or "width", dodged side by | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 544 | # side or stacked -- but standing on a tenth of the least value rather than | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 545 | # on 1 (see draw_bar_native). The categories are still a discrete axis, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 546 | # which a geom_blank of the keys sets out in their order. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 547 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 548 | # rows: [ key index, series index, value ], one per bar | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 549 | # n: the number of series, which share each category when dodged | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 550 | # fills: one fill per bar, or undef to map the series to the fill scale | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 551 | # edges: one R color per bar, NA for none | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 552 | # names: the series' names, for the fill scale, when fills is undef | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 553 | 2 | my ( $a, $rows, $n, $fills, $edges, $names ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 554 | 2 | my ( $s, $keys, $horizontal ) = @{$a}{qw(s keys horizontal)}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 555 | 2 | ▶ | my $width = $a->{plot}{width} // 0.9; # geom_col's own width | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$a->{"plot"}{"width"} // "0.9"
MC/DC100%1/1
$a->{"plot"}{"width"} // "0.9"
$a->{"plot"}{"width"}
Truth table100%2/2
A
A= $a->{"plot"}{"width"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 556 | 2 | ▶ | my @drawn = grep { looks_like_number( $rows->[$_][2] ) && is_finite( $rows->[$_][2] ) && ( $rows->[$_][2] > 0 ) } 0 .. $#{$rows}; # what a log axis can show | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%2/6
&looks_like_number($rows->[$_][2]) && is_finite($rows->[$_][2]) && $rows->[$_][2] > 0
&looks_like_number($rows->[$_][2]) and is_finite($rows->[$_][2])
MC/DC0%0/3
&looks_like_number($rows->[$_][2]) && is_finite($rows->[$_][2]) && $rows->[$_][2] > 0
&looks_like_number($rows->[$_][2])
is_finite($rows->[$_][2])
$rows->[$_][2] > 0
Truth table25%1/4
A and B && C
A= &looks_like_number($rows->[$_][2])B= is_finite($rows->[$_][2])C= $rows->[$_][2] > 0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 557 | 2 | my $least = min( map { $rows->[$_][2] } @drawn ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 558 | 2 | ▶ | return unless defined $least; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 559 | 2 | my $base = $least / 10; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 560 | 2 | my ( %top, @rect ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 561 | 2 | foreach my $r (@drawn) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 562 | 7 | my ( $j, $i, $v ) = @{ $rows->[$r] }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 563 | 7 | my ( $lo, $hi ) = ( $j + 1 - $width / 2, $j + 1 + $width / 2 ); # ggplot2 places category j at j + 1 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 564 | 7 | ▶ | ( $lo, $hi ) = ( $lo + $i * $width / $n, $lo + ( $i + 1 ) * $width / $n ) unless $a->{stacked} || ( $n == 1 ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$a->{"stacked"} or $n == 1
MC/DC0%0/2
$a->{"stacked"} or $n == 1
$a->{"stacked"}
$n == 1
Truth table66%2/3
A or B
A= $a->{"stacked"}B= $n == 1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 565 | # a bar reaches its value, and in a stack the running sum of the values, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 566 | # as on a linear axis; only the first stands on $base | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 567 | 7 | ▶ | my $bottom = $a->{stacked} ? ( $top{$j} // $base ) : $base; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$top{$j} // $base
MC/DC0%0/2
$top{$j} // $base
$top{$j}
$base
Truth table66%2/3
A // B
A= $top{$j}B= $base
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 568 | 7 | ▶ | my $top = $a->{stacked} ? ( $top{$j} // 0 ) + $v : $v; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$top{$j} // 0
MC/DC100%1/1
$top{$j} // 0
$top{$j}
Truth table100%2/2
A
A= $top{$j}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 569 | 7 | ▶ | $top{$j} = $top if $a->{stacked}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 570 | 7 | push @rect, [ $lo, $hi, $bottom, $top, $r ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 571 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 572 | 2 | my @column = map_rects( \@rect, [ 0 .. $#rect ], $horizontal ); # a list, not a hash: the script is the same from run to run | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 573 | 2 | my $fill = ''; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 574 | 2 | ▶ | if ( defined $fills ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 575 | 1 | $fill = ', fill = ' . r_strs( map { $fills->[ $_->[4] % scalar @{$fills} ] } @rect ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 576 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 577 | 1 | push @column, s => 'factor(' . r_strs( map { $names->[ $rows->[ $_->[4] ][1] ] } @rect ) . ', levels = ' . r_strs( @{$names} ) . ')'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 578 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 579 | 2 | ▶ | my $colour = ( defined $a->{plot}{edgecolor} ) ? ', colour = c(' . join( ', ', map { $edges->[ $_->[4] ] } @rect ) . ')' : ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 580 | 2 | ▶ | push @{ $s->{layers} }, 'ggplot2::geom_blank(data = ' . r_frame( k => r_factor( @{$keys} ) ) . ', ggplot2::aes(' . ( $horizontal ? 'y' : 'x' ) . ' = k))'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 581 | 2 | ▶ | push @{ $s->{layers} }, 'ggplot2::geom_rect(data = ' . r_frame(@column) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 582 | . ', ggplot2::aes(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax' . ( defined $fills ? '' : ', fill = s' ) . ")$fill$colour)"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 583 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 584 | 26 | ▶ | sub map_rects { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
map_rects: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 585 | # The columns of a rectangle data.frame for the rectangles at @$idx, with | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 586 | # the position and value axes swapped for a horizontal chart. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 587 | 26 | my ( $rects, $idx, $horizontal ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 588 | 26 | ▶ | my ( $p_lo, $p_hi, $v_lo, $v_hi ) = $horizontal ? ( 'ymin', 'ymax', 'xmin', 'xmax' ) : ( 'xmin', 'xmax', 'ymin', 'ymax' ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 589 | return ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 590 | 65 | $p_lo => r_nums( map { $rects->[$_][0] } @{$idx} ), | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 591 | 65 | $p_hi => r_nums( map { $rects->[$_][1] } @{$idx} ), | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 592 | 65 | $v_lo => r_nums( map { $rects->[$_][2] } @{$idx} ), | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 593 | 26 | $v_hi => r_nums( map { $rects->[$_][3] } @{$idx} ), | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 594 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 595 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 596 | 15 | ▶ | sub rect_layer { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
rect_layer: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 597 | # Rectangles each in its own fill and edge color, out of any legend. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 598 | 15 | my ( $rects, $fills, $edges, $idx, $horizontal ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 599 | return 'ggplot2::geom_rect(data = ' . r_frame( map_rects( $rects, $idx, $horizontal ) ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 600 | 15 | . ', ggplot2::aes(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax), fill = ' . r_strs( @{$fills} ) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 601 | 15 | . ', colour = c(' . join( ', ', @{$edges} ) . '), na.rm = TRUE)'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 602 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 603 | 34 | ▶ | sub draw_bar_errors { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
draw_bar_errors: called CC=23 SCAR=33.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 604 | # "yerr" on a bar chart and "xerr" on a barh, in matplotlib's shapes: one | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 605 | # number for every bar, an array of one per bar, two such arrays (lower, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 606 | # then upper), or a hash by key of one number or a [ lower, upper ] pair. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 607 | # Drawn without caps, as matplotlib draws them by default. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 608 | 34 | my ( $plot, $s, $keys, $horizontal, $where, $sub ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 609 | 34 | ▶ | my $along = $horizontal ? 'xerr' : 'yerr'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 610 | 34 | ▶ | my $across = $horizontal ? 'yerr' : 'xerr'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 611 | 34 | ▶ | if ( defined $plot->{$across} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 612 | 1 | die "$sub: \"$across\"$where would be an error bar across the bars of a $plot->{plot_type}; the $engine engine draws \"$along\", along them"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 613 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 614 | 33 | ▶ | return unless defined $plot->{$along}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 615 | 5 | my $err = $plot->{$along}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 616 | 5 | my $n = scalar @{$keys}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 617 | 5 | my ( @low, @high ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 618 | 5 | ▶ | if ( ref $err eq '' ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 619 | 0 | check_numbers( [$err], "\"$along\"", $sub ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 620 | 0 | @low = @high = ($err) x $n; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 621 | } elsif ( ref $err eq 'ARRAY' ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 622 | 0 | ▶ | my @rows = ( grep { ref } @{$err} ) ? @{$err} : ($err); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 623 | 1 | ▶ | if ( ( scalar @rows > 2 ) || ( grep { ref $_ ne 'ARRAY' } @rows ) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
scalar @rows > 2 or grep {ref $_ ne "ARRAY";} @rows
MC/DC0%0/2
scalar @rows > 2 or grep {ref $_ ne "ARRAY";} @rows
scalar @rows > 2
grep {ref $_ ne "ARRAY";} @rows
Truth table33%1/3
A or B
A= scalar @rows > 2B= grep {ref $_ ne "ARRAY";} @rows
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 624 | 0 | die "$sub: \"$along\" is an array of one error per bar, or of two such arrays (lower, then upper)"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 625 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 626 | 1 | foreach my $row (@rows) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 627 | 0 | ▶ | die "$sub: \"$along\" has " . scalar( @{$row} ) . " values in a row, but there are $n bars" if scalar @{$row} != $n; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 628 | 1 | check_numbers( $row, "\"$along\"", $sub ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 629 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 630 | 1 | @low = @{ $rows[0] }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 631 | 1 | @high = @{ $rows[-1] }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 632 | } elsif ( ref $err eq 'HASH' ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 633 | 4 | my @missing = grep { not defined $err->{$_} } @{$keys}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 634 | 0 | ▶ | die "$sub: the keys " . join( ', ', map { "\"$_\"" } @missing ) . " have no \"$along\"" if scalar @missing > 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 635 | 4 | foreach my $key ( @{$keys} ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 636 | 8 | my $e = $err->{$key}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 637 | 8 | ▶ | my @pair = ( ref $e eq 'ARRAY' ) ? @{$e} : ( ref $e eq '' ) ? ( $e, $e ) : (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 638 | 8 | ▶ | die "$sub: $along/$key should be one number, or exactly 2 items: low and high error bars" if scalar @pair != 2; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 639 | 8 | check_numbers( \@pair, "\"$along\" of \"$key\"", $sub ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 640 | 8 | push @low, $pair[0]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 641 | 8 | push @high, $pair[1]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 642 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 643 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 644 | 0 | die "$sub: \"$along\" is a number, an array or a hash, not a " . ref($err) . ' reference'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 645 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 646 | 5 | my @v = map { $plot->{data}{$_} } @{$keys}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 647 | 5 | ▶ | if ( $s->{native} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 648 | # ggplot2's own error bar, capped, on the discrete axis of the keys | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 649 | 3 | ▶ | my $aes = $horizontal ? 'y = p, xmin = lo, xmax = hi' : 'x = p, ymin = lo, ymax = hi'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 650 | 3 | ▶ | push @{ $s->{after} }, 'ggplot2::geom_errorbar(data = ' . r_frame( p => r_factor( @{$keys} ), lo => r_nums( map { $v[$_] - $low[$_] } 0 .. $n - 1 ), hi => r_nums( map { $v[$_] + $high[$_] } 0 .. $n - 1 ) ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 651 | . ", ggplot2::aes($aes), width = 0.25" . ( $horizontal ? ', orientation = "y"' : '' ) . ', na.rm = TRUE)'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 652 | 3 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 653 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 654 | my %cols = ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 655 | p => r_nums( 0 .. $n - 1 ), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 656 | 4 | lo => r_nums( map { $v[$_] - $low[$_] } 0 .. $n - 1 ), | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 657 | 2 | hi => r_nums( map { $v[$_] + $high[$_] } 0 .. $n - 1 ), | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 658 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 659 | 2 | ▶ | my $aes = $horizontal ? 'y = p, xmin = lo, xmax = hi' : 'x = p, ymin = lo, ymax = hi'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 660 | 2 | ▶ | push @{ $s->{after} }, 'ggplot2::geom_linerange(data = ' . r_frame( map { $_ => $cols{$_} } qw(p lo hi) ) . ", ggplot2::aes($aes), colour = \"#000000\", linewidth = $line_mm" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 661 | . ( $horizontal ? ', orientation = "y"' : '' ) . ', na.rm = TRUE)'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 662 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 663 | 53 | ▶ | sub sets_of { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
sets_of: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 664 | # The sets of a boxplot, violin or hist as ( \@keys, \%values ): a single | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 665 | # array is one set, named '' for the boxes and violins, 'A' for a hist, as | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 666 | # matplotlib's engine names them. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 667 | 53 | my ( $plot, $single_name, $sub ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 668 | 53 | ▶ | my $data = ( ref $plot->{data} eq 'ARRAY' ) ? { $single_name => $plot->{data} } : $plot->{data}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 669 | 53 | return ( [ key_order_of( $plot, $data, 'key_order', $sub ) ], $data ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 670 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 671 | 29 | ▶ | sub distribution_colors { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
distribution_colors: called CC=6 SCAR=18.3
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 672 | # The fill of each box or violin: "colors", a hash by key, else "color" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 673 | # for all, else $default. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 674 | 29 | my ( $plot, $keys, $default, $sub ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 675 | 29 | ▶ | if ( ref $plot->{color} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 676 | 0 | die "$sub: \"color\" is one color for every $plot->{plot_type}, not a " . ref( $plot->{color} ) . ' reference; give one color per key in "colors", e.g. colors => { A => \'red\' }'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 677 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 678 | 29 | ▶ | if ( defined $plot->{colors} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 679 | 5 | ▶ | die "$sub: \"colors\" is a hash of one color per key" unless ref $plot->{colors} eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 680 | # The keys sets_of named, not those of "data", which as one array has | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 681 | # none: that died as "Not a HASH reference". | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 682 | 5 | my @uncolored = grep { not defined $plot->{colors}{$_} } @{$keys}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 683 | 0 | ▶ | die "$sub: the data keys " . join( ', ', map { "\"$_\"" } sort @uncolored ) . ' have no defined color' if scalar @uncolored > 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 684 | 5 | return map { color_of( $plot->{colors}{$_}, "\"colors\" of \"$_\"", $sub ) } @{$keys}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 685 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 686 | 24 | ▶ | return (color_of( $plot->{color}, '"color"', $sub )) x scalar @{$keys} if defined $plot->{color}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 687 | 22 | return ($default) x scalar @{$keys}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 688 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 689 | 53 | ▶ | sub orientation_of { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
orientation_of: called CC=4 SCAR=14.6
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 690 | 53 | my ( $plot, $sub ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 691 | 53 | ▶ | my $o = $plot->{orientation} // 'vertical'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$plot->{"orientation"} // "vertical"
MC/DC100%1/1
$plot->{"orientation"} // "vertical"
$plot->{"orientation"}
Truth table100%2/2
A
A= $plot->{"orientation"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 692 | 53 | ▶ | die "$sub needs either \"horizontal\" or \"vertical\", not \"$o\"" if ( ref $o ) || ( $o !~ m/^(?:horizontal|vertical)$/ ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $o or not $o =~ /^(?:horizontal|vertical)$/
MC/DC0%0/2
ref $o or not $o =~ /^(?:horizontal|vertical)$/
ref $o
not $o =~ /^(?:horizontal|vertical)$/
Truth table33%1/3
A or not(B)
A= ref $oB= not $o =~ /^(?:horizontal|vertical)$/
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 693 | 53 | return $o; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 694 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 695 | 13 | ▶ | sub draw_boxplot { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
draw_boxplot: called CC=33 SCAR=35.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 696 | 13 | my ( $plot, $s, $where, $sub ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 697 | 13 | ▶ | my $horizontal = ( orientation_of( $plot, $sub ) eq 'horizontal' ) ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 698 | 13 | ▶ | my ( $pos_axis, $val_axis ) = $horizontal ? ( 'y', 'x' ) : ( 'x', 'y' ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 699 | 13 | my ( $keys, $data ) = sets_of( $plot, '', $sub ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 700 | 13 | ▶ | my %switch = map { $_ => bool_option( $plot->{$_} // ( $_ eq 'notch' ? 0 : 1 ), $_, $sub ) } qw(notch showcaps showfliers showmeans); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$plot->{$_} // ($_ eq "notch" ? 0 : 1)
MC/DC100%1/1
$plot->{$_} // ($_ eq "notch" ? 0 : 1)
$plot->{$_}
Truth table100%2/2
A
A= $plot->{$_}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 701 | 13 | $s->{axis}{$val_axis}{log} = 1 foreach value_axis_log( $plot, $pos_axis, $sub ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 702 | 12 | ▶ | my @fills = distribution_colors( $plot, $keys, ( $s->{native} ? undef : $cycle[0] ), $sub ); # patch_artist boxes take "patch.facecolor", C0, without advancing the cycle; ggplot2's are white | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 703 | 12 | my $n = scalar @{$keys}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 704 | 12 | my ( @native_k, @native_v, @native_fill ); # under "native": the rows of one geom_boxplot, and the fill of each box drawn | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 705 | # bxp's default widths: 0.15 of the span of the positions, kept between | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 706 | # 0.15 and 0.5. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 707 | 12 | my $width = max( 0.15, min( 0.5, 0.15 * ( $n - 1 ) ) ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 708 | 12 | my @labels; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 709 | 12 | foreach my $i ( 0 .. $n - 1 ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 710 | 20 | my $key = $keys->[$i]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 711 | 20 | ▶ | my @v = grep { ( defined ) && ( not is_nan($_) ) } @{ $data->{$key} }; # NaN is a missing value, as undef is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
defined $_ && !is_nan($_)
MC/DC100%2/2
defined $_ && !is_nan($_)
defined $_
!is_nan($_)
Truth table100%3/3
A && not(B)
A= defined $_B= !is_nan($_)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 712 | 20 | my @bad = grep { not looks_like_number($_) } @v; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 713 | 20 | ▶ | die "$sub: $key has non-numeric values: \"" . join( '", "', @bad ) . '"' if scalar @bad > 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 714 | 20 | push @labels, "$key (" . with_commas( scalar @v ) . ')'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 715 | 20 | ▶ | next if scalar @v == 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 716 | 20 | ▶ | if ( $s->{native} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 717 | 10 | push @native_k, ($key) x scalar @v; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 718 | 10 | push @native_v, @v; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 719 | 10 | push @native_fill, $fills[$i]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 720 | 10 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 721 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 722 | 10 | my $p = $i + 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 723 | 10 | ▶ | my $aes = $horizontal ? 'x = v, y = p, group = p' : 'x = p, y = v, group = p'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 724 | 10 | ▶ | push @{ $s->{layers} }, 'ggplot2::geom_boxplot(data = ' . r_frame( p => r_num($p), v => r_nums(@v) ) . ", ggplot2::aes($aes), fill = " . r_str( $fills[$i] ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 725 | . ", colour = \"#000000\", width = $width, notch = " . ( $switch{notch} ? 'TRUE' : 'FALSE' ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 726 | . ', staplewidth = ' . ( $switch{showcaps} ? 0.5 : 0 ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 727 | 10 | . ', outlier.shape = ' . ( $switch{showfliers} ? 1 : 'NA' ) . ', outlier.size = 2' | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 728 | . ( $horizontal ? ', orientation = "y"' : '' ) . ', na.rm = TRUE)'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 729 | 10 | my @sorted = sort { $a <=> $b } @v; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 730 | 10 | my $median = percentile( \@sorted, 0.5 ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 731 | # matplotlib's median line is C1, orange, where ggplot2 draws it in the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 732 | # box's own outline color | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 733 | 10 | push @{ $s->{layers} }, segment_layer( [ [ $p - $width / 2, $p + $width / 2, $median, $median ] ], $horizontal, r_str( $cycle[1] ), $line_mm * 2 / 3 ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 734 | 10 | ▶ | if ( $switch{showmeans} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 735 | 8 | my $mean = sum(@v) / scalar @v; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 736 | 8 | ▶ | my @xy = $horizontal ? ( x => r_num($mean), y => r_num($p) ) : ( x => r_num($p), y => r_num($mean) ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 737 | 8 | push @{ $s->{layers} }, 'ggplot2::geom_point(data = ' . r_frame(@xy) . ', ggplot2::aes(x = x, y = y), shape = 17, colour = ' . r_str( $cycle[2] ) . ", size = $marker_mm)"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 738 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 739 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 740 | 12 | ▶ | if ( $s->{native} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 741 | # stat_boxplot's own quartiles, whiskers and outliers, on a discrete axis | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 742 | # of the keys, each labelled by its key alone. The switches change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 743 | # ggplot2's defaults only where they are given: it draws no caps and no | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 744 | # mean, and draws the outliers. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 745 | 6 | $s->{discrete}{$pos_axis} = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 746 | 6 | ▶ | return unless @native_v; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 747 | 6 | ▶ | my $aes = $horizontal ? 'x = v, y = k' : 'x = k, y = v'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 748 | 6 | my $frame = r_frame( k => 'factor(' . r_strs(@native_k) . ', levels = ' . r_strs( @{$keys} ) . ')', v => r_nums(@native_v) ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 749 | 6 | ▶ | my $extra = ( defined $native_fill[0] ? ', fill = ' . r_strs(@native_fill) : '' ) . ( $horizontal ? ', orientation = "y"' : '' ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 750 | 6 | ▶ | $extra .= ', notch = ' . ( $switch{notch} ? 'TRUE' : 'FALSE' ) if defined $plot->{notch}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 751 | 6 | ▶ | $extra .= ', staplewidth = ' . ( $switch{showcaps} ? 0.5 : 0 ) if defined $plot->{showcaps}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 752 | 6 | ▶ | $extra .= ', outlier.shape = NA' if ( defined $plot->{showfliers} ) && ( not $switch{showfliers} ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $plot->{"showfliers"} and not $switch{"showfliers"}
MC/DC50%1/2
defined $plot->{"showfliers"} and not $switch{"showfliers"}
defined $plot->{"showfliers"}
not $switch{"showfliers"}
Truth table66%2/3
A and not(B)
A= defined $plot->{"showfliers"}B= not $switch{"showfliers"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 753 | 6 | push @{ $s->{layers} }, "ggplot2::geom_boxplot(data = $frame, ggplot2::aes($aes)$extra, na.rm = TRUE)"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 754 | 6 | ▶ | if ( ( defined $plot->{showmeans} ) && $switch{showmeans} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $plot->{"showmeans"} and $switch{"showmeans"}
MC/DC50%1/2
defined $plot->{"showmeans"} and $switch{"showmeans"}
defined $plot->{"showmeans"}
$switch{"showmeans"}
Truth table66%2/3
A and B
A= defined $plot->{"showmeans"}B= $switch{"showmeans"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 755 | 3 | ▶ | push @{ $s->{layers} }, "ggplot2::stat_summary(data = $frame, ggplot2::aes($aes), fun = mean, geom = \"point\"" . ( $horizontal ? ', orientation = "y"' : '' ) . ', na.rm = TRUE)'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 756 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 757 | 6 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 758 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 759 | 6 | category_ticks( $s, $pos_axis, [ 1 .. $n ], \@labels ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 760 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 761 | 17 | ▶ | sub draw_violin { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
draw_violin: called CC=30 SCAR=34.7
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 762 | 17 | my ( $plot, $s, $where, $sub ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 763 | 17 | ▶ | my $horizontal = ( orientation_of( $plot, $sub ) eq 'horizontal' ) ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 764 | 17 | ▶ | my ( $pos_axis, $val_axis ) = $horizontal ? ( 'y', 'x' ) : ( 'x', 'y' ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 765 | 17 | my ( $keys, $data ) = sets_of( $plot, '', $sub ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 766 | 17 | ▶ | my $medians = bool_option( $plot->{medians} // 1, 'medians', $sub ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$plot->{"medians"} // 1
MC/DC100%1/1
$plot->{"medians"} // 1
$plot->{"medians"}
Truth table100%2/2
A
A= $plot->{"medians"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 767 | 17 | ▶ | my $whiskers = bool_option( $plot->{whiskers} // 1, 'whiskers', $sub ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$plot->{"whiskers"} // 1
MC/DC100%1/1
$plot->{"whiskers"} // 1
$plot->{"whiskers"}
Truth table100%2/2
A
A= $plot->{"whiskers"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 768 | 17 | $s->{axis}{$val_axis}{log} = 1 foreach value_axis_log( $plot, $pos_axis, $sub ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 769 | 17 | my $line_color = next_color( $s, 'line' ); # the bodies and the lines of a violinplot take one color of the cycle | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 770 | 17 | my @fills = distribution_colors( $plot, $keys, $line_color, $sub ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 771 | # The bodies of a violinplot have an alpha of 0.3, which matplotlib applies | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 772 | # to the edge as well as the fill; ggplot2's "alpha" reaches only the fill. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 773 | 17 | ▶ | my $edge_hex = color_of( $plot->{edgecolor} // 'black', '"edgecolor"', $sub ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$plot->{"edgecolor"} // "black"
MC/DC100%1/1
$plot->{"edgecolor"} // "black"
$plot->{"edgecolor"}
Truth table100%2/2
A
A= $plot->{"edgecolor"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 774 | 17 | ▶ | my $edge = r_str( length $edge_hex == 7 ? $edge_hex . '4d' : $edge_hex ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 775 | 17 | my $width = 0.5; # violinplot's default widths | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 776 | 17 | my @labels; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 777 | 17 | my ( @native_k, @native_v, @native_fill ); # under "native": the rows of one geom_violin, and the fill of each violin | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 778 | 17 | foreach my $i ( 0 .. $#{$keys} ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 779 | 25 | my $key = $keys->[$i]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 780 | 25 | ▶ | my @v = grep { ( defined ) && looks_like_number($_) && ( not is_nan($_) ) } @{ $data->{$key} }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%2/6
defined $_ && &looks_like_number($_) && !is_nan($_)
defined $_ and &looks_like_number($_)
MC/DC0%0/3
defined $_ && &looks_like_number($_) && !is_nan($_)
defined $_
&looks_like_number($_)
!is_nan($_)
Truth table25%1/4
A and B && not(C)
A= defined $_B= &looks_like_number($_)C= !is_nan($_)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 781 | 25 | ▶ | if ( scalar @v == 0 ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 782 | 0 | die "$sub: \"$key\" has no numeric values to plot (" . scalar( @{ $data->{$key} } ) . ' value(s) given, all undefined, NaN or non-numeric)'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 783 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 784 | 25 | my @infinite = grep { not is_finite($_) } @v; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 785 | 25 | ▶ | die "$sub: \"$key\" has infinite values, which $plot->{plot_type} cannot draw a density for" if scalar @infinite > 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 786 | 25 | my $n = scalar @v; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 787 | 25 | ▶ | if ( $s->{native} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 788 | # stat_ydensity's own bandwidth, bw.nrd0, which gives a sample with no | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 789 | # spread a width of its own, so nothing is refused for it here | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 790 | 12 | push @native_k, ($key) x $n; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 791 | 12 | push @native_v, @v; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 792 | 12 | push @native_fill, $fills[$i]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 793 | 12 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 794 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 795 | 13 | my $mean = sum(@v) / $n; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 796 | 13 | ▶ | my $sd = ( $n > 1 ) ? sqrt( sum( map { ( $_ - $mean )**2 } @v ) / ( $n - 1 ) ) : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 797 | # scipy's gaussian_kde, which matplotlib's violinplot uses, has a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 798 | # bandwidth of Scott's factor, n ** (-1/5), times the sample's standard | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 799 | # deviation; geom_violin's "bw" is that same kernel standard deviation. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 800 | # A sample with no spread has no density, and gaussian_kde refuses it. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 801 | 13 | ▶ | die "$sub: \"$key\" has no spread -- every value is $v[0] -- so there is no density to draw a violin from" if $sd == 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 802 | 12 | my $bw = $sd * $n**( -1 / 5 ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 803 | 12 | my $p = $i + 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 804 | 12 | push @labels, "$key (" . with_commas($n) . ')'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 805 | 12 | ▶ | my $aes = $horizontal ? 'x = v, y = p, group = p' : 'x = p, y = v, group = p'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 806 | 12 | ▶ | push @{ $s->{layers} }, 'ggplot2::geom_violin(data = ' . r_frame( p => r_num($p), v => r_nums(@v) ) . ", ggplot2::aes($aes), fill = " . r_str( $fills[$i] ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 807 | . ", colour = $edge, alpha = 0.3, width = $width, scale = \"width\", trim = TRUE, bw = " . r_num($bw) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 808 | . ( $horizontal ? ', orientation = "y"' : '' ) . ', na.rm = TRUE)'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 809 | 12 | my @sorted = sort { $a <=> $b } @v; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 810 | 12 | my $half = $width / 4; # the caps of the extrema and the median line span a quarter of the width each side | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 811 | 12 | my @segs = ( [ $p, $p, $sorted[0], $sorted[-1] ], [ $p - $half, $p + $half, $sorted[0], $sorted[0] ], [ $p - $half, $p + $half, $sorted[-1], $sorted[-1] ] ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 812 | 12 | ▶ | push @segs, [ $p - $half, $p + $half, ( percentile( \@sorted, 0.5 ) ) x 2 ] if $medians; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 813 | 12 | push @{ $s->{layers} }, segment_layer( \@segs, $horizontal, r_str($line_color), $line_mm * 2 / 3 ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 814 | 12 | ▶ | if ($whiskers) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 815 | 11 | my ( $q1, $q3 ) = ( percentile( \@sorted, 0.25 ), percentile( \@sorted, 0.75 ) ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 816 | # matplotlib's "customized violin" example: the whiskers reach 1.5 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 817 | # interquartile ranges past the quartiles, but no further than the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 818 | # data. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 819 | 11 | my $upper = min( max( $q3 + ( $q3 - $q1 ) * 1.5, $q3 ), $sorted[-1] ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 820 | 11 | my $lower = max( min( $q1 - ( $q3 - $q1 ) * 1.5, $q1 ), $sorted[0] ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 821 | # 5 pt and 1 pt at 2.845 pt per mm, as 5000 / 2845 rather than | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 822 | # 5 / 2.845: perl 5.10.1 reads the literal 2.845 a double low, and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 823 | # wrote the width with a different last digit. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 824 | 11 | push @{ $s->{layers} }, segment_layer( [ [ $p, $p, $q1, $q3 ] ], $horizontal, '"#000000"', 5000 / 2845 ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 825 | 11 | push @{ $s->{layers} }, segment_layer( [ [ $p, $p, $lower, $upper ] ], $horizontal, '"#000000"', 1000 / 2845 ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 826 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 827 | 12 | ▶ | my @xy = $horizontal ? ( x => r_num($mean), y => r_num($p) ) : ( x => r_num($p), y => r_num($mean) ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 828 | 12 | push @{ $s->{layers} }, 'ggplot2::geom_point(data = ' . r_frame(@xy) . ", ggplot2::aes(x = x, y = y), shape = 16, colour = \"#ff0000\", size = $marker_mm)"; # "ro", the red mean | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 829 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 830 | 16 | ▶ | if ( $s->{native} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 831 | # geom_violin with its defaults -- bw.nrd0, trimmed to the data, scaled | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 832 | # so that every violin has the same area -- on a discrete axis of the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 833 | # keys. It draws no median and no whiskers of its own, so those are drawn | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 834 | # only when asked for: the median as a short line across each violin, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 835 | # the whiskers as a narrow geom_boxplot inside it, which is how they are | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 836 | # drawn with ggplot2. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 837 | 7 | $s->{discrete}{$pos_axis} = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 838 | 7 | ▶ | my $aes = $horizontal ? 'x = v, y = k' : 'x = k, y = v'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 839 | 7 | ▶ | my $orient = $horizontal ? ', orientation = "y"' : ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 840 | 7 | my @columns = ( k => 'factor(' . r_strs(@native_k) . ', levels = ' . r_strs( @{$keys} ) . ')', v => r_nums(@native_v) ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 841 | # A given fill is a column of the frame, one color per row, mapped | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 842 | # through I() so that no scale reads it (ggplot2 3.5.0's NEWS: "Plot | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 843 | # scales now ignore AsIs objects"). As a parameter, "fill = c(...)" with | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 844 | # one color per violin, it died in R as "Aesthetics must be either length | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 845 | # 1 or the same as the data (1030)", since stat_ydensity draws each | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 846 | # violin from hundreds of rows; geom_boxplot's one row per box took it. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 847 | 7 | my $fill_aes = ''; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 848 | 7 | ▶ | if ( defined $native_fill[0] ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 849 | 2 | my %fill_of = map { $keys->[$_] => $fills[$_] } 0 .. $#{$keys}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 850 | 2 | push @columns, f => r_strs( map { $fill_of{$_} } @native_k ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 851 | 2 | $fill_aes = ', fill = I(f)'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 852 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 853 | 7 | my $frame = r_frame(@columns); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 854 | 7 | ▶ | my $extra = defined $plot->{edgecolor} ? ', colour = ' . r_str( color_of( $plot->{edgecolor}, '"edgecolor"', $sub ) ) : ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 855 | 7 | push @{ $s->{layers} }, "ggplot2::geom_violin(data = $frame, ggplot2::aes($aes$fill_aes)$extra$orient, na.rm = TRUE)"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 856 | 7 | ▶ | if ( ( defined $plot->{whiskers} ) && $whiskers ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $plot->{"whiskers"} and $whiskers
MC/DC50%1/2
defined $plot->{"whiskers"} and $whiskers
defined $plot->{"whiskers"}
$whiskers
Truth table66%2/3
A and B
A= defined $plot->{"whiskers"}B= $whiskers
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 857 | 1 | push @{ $s->{layers} }, "ggplot2::geom_boxplot(data = $frame, ggplot2::aes($aes), width = 0.1, outlier.shape = NA$orient, na.rm = TRUE)"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 858 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 859 | 7 | ▶ | if ( ( defined $plot->{medians} ) && $medians ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $plot->{"medians"} and $medians
MC/DC50%1/2
defined $plot->{"medians"} and $medians
defined $plot->{"medians"}
$medians
Truth table66%2/3
A and B
A= defined $plot->{"medians"}B= $medians
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 860 | 1 | push @{ $s->{layers} }, "ggplot2::stat_summary(data = $frame, ggplot2::aes($aes), fun = stats::median, fun.min = stats::median, fun.max = stats::median, geom = \"crossbar\", width = 0.25$orient, na.rm = TRUE)"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 861 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 862 | 7 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 863 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 864 | 9 | category_ticks( $s, $pos_axis, [ 1 .. scalar @{$keys} ], \@labels ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 865 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 866 | 44 | ▶ | sub segment_layer { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
segment_layer: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 867 | # Line segments given as [ p0, p1, v0, v1 ] along the position and value | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 868 | # axes, swapped for a horizontal plot. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 869 | # The width is written in 15 digits, as a double perl stringifies it; a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 870 | # quadmath perl wrote all 36 of its own. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 871 | 44 | my ( $segs, $horizontal, $colour, $width ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 872 | 44 | ▶ | my ( $px, $vx ) = $horizontal ? ( 'y', 'x' ) : ( 'x', 'y' ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 873 | return 'ggplot2::geom_segment(data = ' . r_frame( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 874 | 79 | $px => r_nums( map { $_->[0] } @{$segs} ), "${px}end" => r_nums( map { $_->[1] } @{$segs} ), | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 875 | 44 | $vx => r_nums( map { $_->[2] } @{$segs} ), "${vx}end" => r_nums( map { $_->[3] } @{$segs} ), | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 876 | ) . ", ggplot2::aes(x = x, xend = xend, y = y, yend = yend), colour = $colour, linewidth = " . sprintf( '%.15g', $width ) . ')'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 877 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 878 | 23 | ▶ | sub draw_hist { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
draw_hist: called CC=47 SCAR=40.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 879 | 23 | my ( $plot, $s, $where, $sub ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 880 | 23 | ▶ | my $alpha = $plot->{alpha} // 0.5; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$plot->{"alpha"} // "0.5"
MC/DC100%1/1
$plot->{"alpha"} // "0.5"
$plot->{"alpha"}
Truth table100%2/2
A
A= $plot->{"alpha"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 881 | 23 | check_numbers( [$alpha], '"alpha"', $sub ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 882 | 23 | ▶ | die "$sub: \"alpha\" is an opacity from 0 to 1, not $alpha" unless is_finite($alpha) && ( $alpha >= 0 ) && ( $alpha <= 1 ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
is_finite($alpha) and $alpha >= 0 and $alpha <= 1
is_finite($alpha) and $alpha >= 0
MC/DC0%0/3
is_finite($alpha) and $alpha >= 0 and $alpha <= 1
is_finite($alpha)
$alpha >= 0
$alpha <= 1
Truth table25%1/4
A and B and C
A= is_finite($alpha)B= $alpha >= 0C= $alpha <= 1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 883 | 23 | ▶ | if ( ref $plot->{orientation} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 884 | 0 | die "$sub: \"orientation\" is one of \"vertical\" or \"horizontal\" for every set, not a " . ref( $plot->{orientation} ) . ' reference'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 885 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 886 | 23 | ▶ | my $horizontal = ( orientation_of( $plot, $sub ) eq 'horizontal' ) ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 887 | 23 | $s->{axis}{$_}{log} = 1 foreach logscale_axes( $plot->{logscale}, $sub ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 888 | 23 | my ( $keys, $data ) = sets_of( $plot, 'A', $sub ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 889 | 23 | foreach my $arg ( grep { ref $plot->{$_} eq 'HASH' } ( 'bins', 'color' ) ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 890 | 4 | my @stray = sort grep { not exists $data->{$_} } keys %{ $plot->{$arg} }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 891 | 0 | ▶ | die "$sub: the keys " . join( ', ', map { "\"$_\"" } @stray ) . " of \"$arg\" are not sets of \"data\"" if scalar @stray > 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 892 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 893 | 23 | ▶ | if ( ( ref $plot->{color} ) && ( ref $plot->{color} ne 'HASH' ) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $plot->{"color"} and ref $plot->{"color"} ne "HASH"
MC/DC0%0/2
ref $plot->{"color"} and ref $plot->{"color"} ne "HASH"
ref $plot->{"color"}
ref $plot->{"color"} ne "HASH"
Truth table66%2/3
A and B
A= ref $plot->{"color"}B= ref $plot->{"color"} ne "HASH"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 894 | 0 | die "$sub: \"color\" is one color for every set, or a hash of one color per set, not a " . ref( $plot->{color} ) . ' reference'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 895 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 896 | 23 | ▶ | my $show_legend = bool_option( $plot->{show_legend} // ( ( scalar keys %{$data} > 1 ) ? 1 : 0 ), 'show_legend', $sub ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$plot->{"show_legend"} // (scalar keys %{$data;} > 1 ? 1 : 0)
MC/DC100%1/1
$plot->{"show_legend"} // (scalar keys %{$data;} > 1 ? 1 : 0)
$plot->{"show_legend"}
Truth table100%2/2
A
A= $plot->{"show_legend"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 897 | 23 | my ( @rects, @names, @colors ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 898 | 23 | my @native; # under "native": [ set, \@values, bins, color or undef ] of each set | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 899 | 23 | foreach my $set ( @{$keys} ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 900 | 34 | my @bad = grep { not looks_like_number($_) } @{ $data->{$set} }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 901 | 0 | ▶ | die "$sub: $set has non-numeric values, which for hist must be numeric: \"" . join( '", "', map { $_ // 'undef' } @bad ) . '"' if scalar @bad > 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$_ // "undef"
MC/DC0%0/1
$_ // "undef"
$_
Truth table0%0/2
A
A= $_
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 902 | 34 | ▶ | my @infinite = grep { not( is_nan($_) || is_finite($_) ) } @{ $data->{$set} }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
is_nan($_) || is_finite($_)
MC/DC0%0/2
is_nan($_) || is_finite($_)
is_nan($_)
is_finite($_)
Truth table66%2/3
A || B
A= is_nan($_)B= is_finite($_)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 903 | 34 | ▶ | die "$sub: $set has infinite values, which hist cannot bin" if scalar @infinite > 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 904 | 34 | my @v = grep { not is_nan($_) } @{ $data->{$set} }; # numpy bins around a NaN | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 905 | 34 | ▶ | my $bins = ( ref $plot->{bins} eq 'HASH' ) ? ( $plot->{bins}{$set} // 10 ) : ( $plot->{bins} // 10 ); # rcParams hist.bins | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition75%3/4
$plot->{"bins"}{$set} // 10
$plot->{"bins"} // 10
MC/DC0%0/1
$plot->{"bins"}{$set} // 10
$plot->{"bins"}{$set}
MC/DC100%1/1
$plot->{"bins"} // 10
$plot->{"bins"}
Truth table50%1/2
A
A= $plot->{"bins"}{$set}
Truth table100%2/2
A
A= $plot->{"bins"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 906 | 34 | my @edges; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 907 | 34 | ▶ | if ( ref $bins eq 'ARRAY' ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 908 | 5 | check_numbers( $bins, "\"bins\" of \"$set\"", $sub ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 909 | # Two equal edges are a bin of no width, which holds nothing, as numpy | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 910 | # allows them; edges that fall are refused, as numpy refuses them. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 911 | # R's cut() refuses equal breaks, so the native style does too. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 912 | 5 | ▶ | die "$sub: \"bins\" of \"$set\" are bin edges, so at least 2, never falling" if ( scalar @{$bins} < 2 ) || grep { $bins->[$_] < $bins->[ $_ - 1 ] } 1 .. $#{$bins}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
scalar @{$bins;} < 2 or grep {$bins->[$_] < $$bins[$_ - 1];} 1 .. $#{$bins;}
MC/DC50%1/2
scalar @{$bins;} < 2 or grep {$bins->[$_] < $$bins[$_ - 1];} 1 .. $#{$bins;}
scalar @{$bins;} < 2
grep {$bins->[$_] < $$bins[$_ - 1];} 1 .. $#{$bins;}
Truth table66%2/3
A or B
A= scalar @{$bins;} < 2B= grep {$bins->[$_] < $$bins[$_ - 1];} 1 .. $#{$bins;}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 913 | 4 | ▶ | if ( $s->{native} && grep { $bins->[$_] == $bins->[ $_ - 1 ] } 1 .. $#{$bins} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$s->{"native"} and grep {$bins->[$_] == $$bins[$_ - 1];} 1 .. $#{$bins;}
MC/DC100%2/2
$s->{"native"} and grep {$bins->[$_] == $$bins[$_ - 1];} 1 .. $#{$bins;}
$s->{"native"}
grep {$bins->[$_] == $$bins[$_ - 1];} 1 .. $#{$bins;}
Truth table100%3/3
A and B
A= $s->{"native"}B= grep {$bins->[$_] == $$bins[$_ - 1];} 1 .. $#{$bins;}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 914 | 1 | die "$sub: \"bins\" of \"$set\" has two equal edges, a bin of no width, which ggplot2's stat_bin refuses under style => 'native'; style => 'matplotlib' draws it"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 915 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 916 | 3 | @edges = map { as_double($_) } @{$bins}; # see bin_index | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 917 | ▶ | } elsif ( ( not ref $bins ) && looks_like_number($bins) && is_finite($bins) && ( $bins == int $bins ) && ( $bins >= 1 ) ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%8/12
not ref $bins and &looks_like_number($bins) and is_finite($bins) and $bins == int $bins and $bins >= 1
not ref $bins and &looks_like_number($bins) and is_finite($bins) and $bins == int $bins
not ref $bins and &looks_like_number($bins) and is_finite($bins)
not ref $bins and &looks_like_number($bins)
MC/DC20%1/5
not ref $bins and &looks_like_number($bins) and is_finite($bins) and $bins == int $bins and $bins >= 1
not ref $bins
&looks_like_number($bins)
is_finite($bins)
$bins == int $bins
$bins >= 1
Truth table33%2/6
not(A) and B and C and D and E
A= not ref $binsB= &looks_like_number($bins)C= is_finite($bins)D= $bins == int $binsE= $bins >= 1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 918 | 28 | ▶ | my ( $lo, $hi ) = scalar @v ? ( min(@v), max(@v) ) : ( 0, 1 ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 919 | 28 | ▶ | ( $lo, $hi ) = ( $lo - 0.5, $hi + 0.5 ) if $lo == $hi; # numpy's range for data of one value | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 920 | 28 | @edges = linspace( $lo, $hi, $bins + 1 ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 921 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 922 | 1 | ▶ | die "$sub: \"bins\" is a whole number of bins, or an array of bin edges, for the $engine engine, not \"" . ( ref $bins || $bins ) . '"; a numpy estimator such as "auto" is matplotlib\'s alone'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
ref $bins || $bins
MC/DC0%0/2
ref $bins || $bins
ref $bins
$bins
Truth table33%1/3
A || B
A= ref $binsB= $bins
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 923 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 924 | 31 | ▶ | if ( $s->{native} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 925 | 17 | ▶ | my $c = ( ref $plot->{color} eq 'HASH' ) ? $plot->{color}{$set} : $plot->{color}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 926 | 17 | ▶ | push @native, [ $set, \@v, $bins, defined $c ? color_of( $c, "\"color\" of \"$set\"", $sub ) : undef ]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 927 | 17 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 928 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 929 | 14 | my @counts = (0) x ( scalar @edges - 1 ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 930 | 14 | foreach my $x (@v) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 931 | 119 | my $i = bin_index( \@edges, $x ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 932 | 119 | ▶ | $counts[$i]++ if defined $i; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 933 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 934 | 14 | ▶ | my $c = ( ref $plot->{color} eq 'HASH' ) ? $plot->{color}{$set} : $plot->{color}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 935 | 14 | ▶ | my $color = defined $c ? color_of( $c, "\"color\" of \"$set\"", $sub ) : next_color( $s, 'patch' ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 936 | 14 | foreach my $i ( 0 .. $#counts ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 937 | 104 | push @rects, [ $edges[$i], $edges[ $i + 1 ], 0, $counts[$i] ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 938 | 104 | push @names, $set; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 939 | 104 | push @colors, $color; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 940 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 941 | 14 | ▶ | add_legend( $s->{fill_d}, $set, $color ) if $show_legend; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 942 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 943 | 20 | ▶ | if ( $s->{native} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 944 | 11 | draw_hist_native( $plot, $s, \@native, $show_legend, $horizontal ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 945 | 11 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 946 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 947 | 9 | ▶ | my ( $b_lo, $b_hi, $c_lo, $c_hi ) = $horizontal ? ( 'ymin', 'ymax', 'xmin', 'xmax' ) : ( 'xmin', 'xmax', 'ymin', 'ymax' ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 948 | my $frame = r_frame( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 949 | 104 | $b_lo => r_nums( map { $_->[0] } @rects ), $b_hi => r_nums( map { $_->[1] } @rects ), | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 950 | 9 | $c_lo => r_nums( map { $_->[2] } @rects ), $c_hi => r_nums( map { $_->[3] } @rects ), | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 951 | s => r_strs(@names), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 952 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 953 | 9 | my $aes = 'xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 954 | 9 | ▶ | if ($show_legend) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 955 | 4 | push @{ $s->{layers} }, "ggplot2::geom_rect(data = $frame, ggplot2::aes($aes, fill = s), alpha = " . r_num($alpha) . ', colour = NA, na.rm = TRUE)'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 956 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 957 | 5 | push @{ $s->{layers} }, "ggplot2::geom_rect(data = $frame, ggplot2::aes($aes), fill = " . r_strs(@colors) . ', alpha = ' . r_num($alpha) . ', colour = NA, na.rm = TRUE)'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 958 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 959 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 960 | 11 | ▶ | sub draw_hist_native { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
draw_hist_native: called CC=18 SCAR=29.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 961 | # The sets of a hist as geom_histogram bins them, with stat_bin: over the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 962 | # range of the data its layer holds, which is every set at once unless | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 963 | # "bins" is a hash of one per set, when each set is a layer of its own. A | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 964 | # "bins" that is a count is passed as "bins", and one that is edges as | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 965 | # "breaks". With no "bins", 10 is passed, matplotlib's default: stat_bin's | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 966 | # own is 30, which it warns about every time ("Pick better value"), asking | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 967 | # for exactly this choice to be made. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 968 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 969 | # Sets are overlaid, not stacked as geom_histogram stacks them by default: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 970 | # a hist of several sets compares their distributions, which stacking | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 971 | # would change into a distribution of them all. So that the overlap shows, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 972 | # each takes "alpha", 0.5 where several sets are drawn and none is given. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 973 | 11 | my ( $plot, $s, $sets, $show_legend, $horizontal ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 974 | 11 | ▶ | my $several = ( scalar @{$sets} > 1 ) ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 975 | 11 | ▶ | my $alpha = $plot->{alpha} // ( $several ? 0.5 : undef ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$plot->{"alpha"} // ($several ? "0.5" : undef)
MC/DC0%0/1
$plot->{"alpha"} // ($several ? "0.5" : undef)
$plot->{"alpha"}
Truth table50%1/2
A
A= $plot->{"alpha"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 976 | 11 | ▶ | my $aes = $horizontal ? 'y = v' : 'x = v'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 977 | 11 | ▶ | my $mapped = ( $show_legend || $several ) ? 1 : 0; # one set alone, unlabelled, is ggplot2's own grey | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$show_legend or $several
MC/DC50%1/2
$show_legend or $several
$show_legend
$several
Truth table66%2/3
A or B
A= $show_legendB= $several
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 978 | 11 | ▶ | if ($mapped) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 979 | 6 | add_legend( $s->{fill_d}, $_->[0], $_->[3] ) foreach @{$sets}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 980 | 6 | ▶ | $s->{guide_none}{fill} = 1 unless $show_legend; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 981 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 982 | 12 | ▶ | my $bins_of = sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 983 | 12 | my ($bins) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 984 | 12 | ▶ | return ( ref $bins eq 'ARRAY' ) ? 'breaks = ' . r_nums( @{$bins} ) : 'bins = ' . r_num($bins); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 985 | 11 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 986 | 11 | ▶ | my @layers = ( ref $plot->{bins} eq 'HASH' ) ? map { [$_] } @{$sets} : ( $sets ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 987 | 11 | foreach my $layer (@layers) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 988 | 12 | my @v = map { @{ $_->[1] } } @{$layer}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 989 | 12 | ▶ | next if scalar @v == 0; # nothing to bin, as matplotlib bins nothing; c() would be NULL, and no column at all in R | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 990 | 12 | my @s = map { ( $_->[0] ) x scalar @{ $_->[1] } } @{$layer}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 991 | 12 | my $frame = r_frame( v => r_nums(@v), s => 'factor(' . r_strs(@s) . ', levels = ' . r_strs( map { $_->[0] } @{$sets} ) . ')' ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 992 | 12 | ▶ | my $extra = $mapped ? '' : ( defined $layer->[0][3] ? ', fill = ' . r_str( $layer->[0][3] ) : '' ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 993 | 12 | ▶ | $extra .= ', alpha = ' . r_num($alpha) if defined $alpha; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 994 | 12 | ▶ | push @{ $s->{layers} }, "ggplot2::geom_histogram(data = $frame, ggplot2::aes($aes" . ( $mapped ? ', fill = s' : '' ) . '), ' . $bins_of->( $layer->[0][2] ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 995 | . ', position = "identity"' . $extra . ( $horizontal ? ', orientation = "y"' : '' ) . ', na.rm = TRUE)'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 996 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 997 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 998 | 39 | ▶ | sub gradient_of { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
gradient_of: called CC=28 SCAR=35.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 999 | # A continuous color scale, checked: the colormap, the ends, and a log | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1000 | # scale, which needs a bottom end above 0. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1001 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1002 | # Under "native" (args: native => 1) a colormap not asked for is ggplot2's | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1003 | # default gradient (colors => undef), the ends not given are left to | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1004 | # ggplot2 to take from what it draws, and a title not given is ggplot2's | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1005 | # own -- the name of what is colored -- where "default_title" is undef | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1006 | # (waiver => 1). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1007 | 39 | my ($args) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1008 | 39 | my ( $plot, $values, $sub ) = @{$args}{qw(plot values sub)}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1009 | 39 | ▶ | my $native = $args->{native} ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1010 | 39 | ▶ | my $colors = ( $native && ( not defined $plot->{cmap} ) ) ? undef : [ colormap_colors( $plot->{cmap} // $args->{default_cmap}, $engine, $sub ) ]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$native and not defined $plot->{"cmap"}
$plot->{"cmap"} // $args->{"default_cmap"}
MC/DC100%2/2
$native and not defined $plot->{"cmap"}
$native
not defined $plot->{"cmap"}
MC/DC0%0/2
$plot->{"cmap"} // $args->{"default_cmap"}
$plot->{"cmap"}
$args->{"default_cmap"}
Truth table100%3/3
A and not(B)
A= $nativeB= not defined $plot->{"cmap"}
Truth table66%2/3
A // B
A= $plot->{"cmap"}B= $args->{"default_cmap"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1011 | 38 | my $log = bool_option( $plot->{cb_logscale}, 'cb_logscale', $sub ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1012 | 38 | foreach my $end ( grep { defined $plot->{$_} } ( 'vmin', 'vmax' ) ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1013 | 6 | check_numbers( [ $plot->{$end} ], "\"$end\"", $sub ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1014 | 6 | ▶ | die "$sub: \"$end\" must be a finite number, not \"$plot->{$end}\"" unless is_finite( $plot->{$end} ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1015 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1016 | 38 | ▶ | if ($native) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1017 | 18 | ▶ | die "$sub: \"cb_logscale\" cannot start at $plot->{vmin}: a log scale needs a minimum above 0" if $log && ( defined $plot->{vmin} ) && ( $plot->{vmin} <= 0 ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
$log and defined $plot->{"vmin"} and $plot->{"vmin"} <= 0
$log and defined $plot->{"vmin"}
MC/DC0%0/3
$log and defined $plot->{"vmin"} and $plot->{"vmin"} <= 0
$log
defined $plot->{"vmin"}
$plot->{"vmin"} <= 0
Truth table25%1/4
A and B and C
A= $logB= defined $plot->{"vmin"}C= $plot->{"vmin"} <= 0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1018 | 18 | ▶ | my $show = bool_option( $plot->{colorbar_on} // 1, 'colorbar_on', $sub ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$plot->{"colorbar_on"} // 1
MC/DC0%0/1
$plot->{"colorbar_on"} // 1
$plot->{"colorbar_on"}
Truth table50%1/2
A
A= $plot->{"colorbar_on"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1019 | 18 | ▶ | $show = 0 if defined $plot->{show_colorbar} && ( not bool_option( $plot->{show_colorbar}, 'show_colorbar', $sub ) ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $plot->{"show_colorbar"} and not bool_option($plot->{"show_colorbar"}, "show_colorbar", $sub)
MC/DC0%0/2
defined $plot->{"show_colorbar"} and not bool_option($plot->{"show_colorbar"}, "show_colorbar", $sub)
defined $plot->{"show_colorbar"}
not bool_option($plot->{"show_colorbar"}, "show_colorbar", $sub)
Truth table33%1/3
A and not(B)
A= defined $plot->{"show_colorbar"}B= not bool_option($plot->{"show_colorbar"}, "show_colorbar", $sub)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1020 | 18 | ▶ | my $title = defined $plot->{cblabel} ? plain_text( $plot->{cblabel}, 'cblabel', $engine, $sub ) : $args->{default_title}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1021 | return { colors => $colors, lo => $plot->{vmin}, hi => $plot->{vmax}, log => $log, show => $show, title => $title, waiver => ( defined $title ? 0 : 1 ), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1022 | 18 | ▶ | type => $plot->{plot_type}, aes => $args->{aes}, na => $args->{na} // 'NA' }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$args->{"na"} // "NA"
MC/DC100%1/1
$args->{"na"} // "NA"
$args->{"na"}
Truth table100%2/2
A
A= $args->{"na"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1023 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1024 | 20 | my @colors = @{$colors}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1025 | 20 | my @finite = finite_values( @{$values} ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1026 | 20 | my ( $lo, $hi ) = ( $plot->{vmin}, $plot->{vmax} ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1027 | 20 | ▶ | if ($log) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1028 | 1 | ▶ | $lo = $lo // min( grep { $_ > 0 } @finite ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$lo // &min(grep({$_ > 0;} @finite))
MC/DC0%0/2
$lo // &min(grep({$_ > 0;} @finite))
$lo
&min(grep({$_ > 0;} @finite))
Truth table33%1/3
A // B
A= $loB= &min(grep({$_ > 0;} @finite))
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1029 | 1 | ▶ | die "$sub: \"cb_logscale\" needs a color value above 0 to start the scale at, or a \"vmin\" or \"cb_min\" above 0, and there is neither" unless defined $lo; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1030 | 1 | ▶ | die "$sub: \"cb_logscale\" cannot start at $lo: a log scale needs a minimum above 0" if $lo <= 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1031 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1032 | 19 | ▶ | $lo = $lo // min(@finite); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$lo // &min(@finite)
MC/DC0%0/2
$lo // &min(@finite)
$lo
&min(@finite)
Truth table66%2/3
A // B
A= $loB= &min(@finite)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1033 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1034 | 20 | ▶ | $hi = $hi // max(@finite); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$hi // &max(@finite)
MC/DC0%0/2
$hi // &max(@finite)
$hi
&max(@finite)
Truth table66%2/3
A // B
A= $hiB= &max(@finite)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1035 | 20 | ▶ | my $show = bool_option( $plot->{colorbar_on} // 1, 'colorbar_on', $sub ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$plot->{"colorbar_on"} // 1
MC/DC100%1/1
$plot->{"colorbar_on"} // 1
$plot->{"colorbar_on"}
Truth table100%2/2
A
A= $plot->{"colorbar_on"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1036 | 20 | ▶ | $show = 0 if defined $plot->{show_colorbar} && ( not bool_option( $plot->{show_colorbar}, 'show_colorbar', $sub ) ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $plot->{"show_colorbar"} and not bool_option($plot->{"show_colorbar"}, "show_colorbar", $sub)
MC/DC0%0/2
defined $plot->{"show_colorbar"} and not bool_option($plot->{"show_colorbar"}, "show_colorbar", $sub)
defined $plot->{"show_colorbar"}
not bool_option($plot->{"show_colorbar"}, "show_colorbar", $sub)
Truth table66%2/3
A and not(B)
A= defined $plot->{"show_colorbar"}B= not bool_option($plot->{"show_colorbar"}, "show_colorbar", $sub)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1037 | 20 | ▶ | my $title = defined $plot->{cblabel} ? plain_text( $plot->{cblabel}, 'cblabel', $engine, $sub ) : $args->{default_title}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1038 | 20 | ▶ | return { colors => \@colors, lo => $lo, hi => $hi, log => $log, show => $show, title => $title, type => $plot->{plot_type}, aes => $args->{aes}, na => $args->{na} // 'NA' }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$args->{"na"} // "NA"
MC/DC100%1/1
$args->{"na"} // "NA"
$args->{"na"}
Truth table100%2/2
A
A= $args->{"na"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1039 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1040 | 15 | ▶ | sub draw_hist2d { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
draw_hist2d: called CC=38 SCAR=40.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1041 | 15 | my ( $plot, $s, $where, $sub ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1042 | 15 | cb_limits( $plot, $sub ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1043 | 15 | bin_counts( $plot, $sub ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1044 | 15 | my @keys = key_order_of( $plot, $plot->{data}, 'key_order', $sub ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1045 | 15 | ▶ | die "$sub: There must be exactly 2 keys for hist2d, not " . scalar(@keys) if scalar @keys != 2; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1046 | 15 | my @xy = map { $plot->{data}{$_} } @keys; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1047 | 0 | ▶ | die "$sub: The length of both keys must be equal: \"$keys[0]\" has " . scalar( @{ $xy[0] } ) . ", \"$keys[1]\" has " . scalar( @{ $xy[1] } ) if scalar @{ $xy[0] } != scalar @{ $xy[1] }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1048 | 15 | foreach my $i ( 0, 1 ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1049 | 30 | ▶ | my @bad = grep { ( not defined $xy[$i][$_] ) || ( not looks_like_number( $xy[$i][$_] ) ) } 0 .. $#{ $xy[$i] }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
defined $xy[$i][$_] && &looks_like_number($xy[$i][$_])
MC/DC0%0/2
defined $xy[$i][$_] && &looks_like_number($xy[$i][$_])
defined $xy[$i][$_]
&looks_like_number($xy[$i][$_])
Truth table33%1/3
A && B
A= defined $xy[$i][$_]B= &looks_like_number($xy[$i][$_])
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1050 | 30 | ▶ | die "$sub: \"$keys[$i]\" is undefined or not a number at indices " . join( ', ', @bad ) if scalar @bad > 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1051 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1052 | 15 | @{ $s->{default_label} }{ 'x', 'y' } = @keys; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1053 | 15 | my %log_axis = map { $_ => 1 } logscale_axes( $plot->{logscale}, $sub ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1054 | 15 | ▶ | if ( $s->{native} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1055 | 8 | draw_hist2d_native( $plot, $s, \@keys, \@xy, \%log_axis, $where, $sub ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1056 | 7 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1057 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1058 | 7 | my @edges; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1059 | 7 | foreach my $i ( 0, 1 ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1060 | 13 | my $axis = ( 'x', 'y' )[$i]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1061 | 13 | my @finite = finite_values( @{ $xy[$i] } ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1062 | 13 | foreach my $end ( 'min', 'max' ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1063 | 26 | my $opt = "$axis$end"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1064 | 26 | ▶ | if ( defined $plot->{$opt} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1065 | 2 | check_numbers( [ $plot->{$opt} ], "\"$opt\"", $sub ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1066 | 2 | ▶ | die "$sub: \"$opt\" must be a finite number, not \"$plot->{$opt}\"" unless is_finite( $plot->{$opt} ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1067 | } elsif ( scalar @finite == 0 ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1068 | 0 | die "$sub: \"$keys[$i]\" has no finite value, so hist2d has no range to bin it over; give \"${axis}min\" and \"${axis}max\""; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1069 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1070 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1071 | 13 | ▶ | my $lo = $plot->{"${axis}min"} // min(@finite); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$$plot{"${axis}min"} // &min(@finite)
MC/DC0%0/2
$$plot{"${axis}min"} // &min(@finite)
$$plot{"${axis}min"}
&min(@finite)
Truth table66%2/3
A // B
A= $$plot{"${axis}min"}B= &min(@finite)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1072 | 13 | ▶ | my $hi = $plot->{"${axis}max"} // max(@finite); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$$plot{"${axis}max"} // &max(@finite)
MC/DC0%0/2
$$plot{"${axis}max"} // &max(@finite)
$$plot{"${axis}max"}
&max(@finite)
Truth table66%2/3
A // B
A= $$plot{"${axis}max"}B= &max(@finite)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1073 | 13 | ▶ | die "$sub: \"${axis}min\" = $lo is above \"${axis}max\" = $hi" if $lo > $hi; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1074 | # Refused on a log axis, where widening by 0.5 each way could reach 0 or | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1075 | # below, and died taking its log; matplotlib's np.logspace of one value | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1076 | # has edges that do not rise, and fails too. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1077 | 13 | ▶ | die "$sub: \"logscale\" puts $axis on a log scale, but every $axis is $lo, a range of nothing; give \"${axis}min\" and \"${axis}max\"" if $log_axis{$axis} && ( $lo == $hi ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$log_axis{$axis} and $lo == $hi
MC/DC50%1/2
$log_axis{$axis} and $lo == $hi
$log_axis{$axis}
$lo == $hi
Truth table66%2/3
A and B
A= $log_axis{$axis}B= $lo == $hi
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1078 | 12 | ▶ | ( $lo, $hi ) = ( $lo - 0.5, $hi + 0.5 ) if $lo == $hi; # numpy's range for data of one value | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1079 | 12 | my $n = $plot->{"${axis}bins"}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1080 | 12 | ▶ | if ( $log_axis{$axis} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1081 | 0 | ▶ | die "$sub: \"logscale\" puts $axis on a log scale, which cannot start at $lo: give a \"${axis}min\" above 0" if $lo <= 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1082 | 0 | $edges[$i] = [ logspace( $lo, $hi, $n + 1 ) ]; # as the matplotlib engine writes them | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1083 | 0 | $s->{axis}{$axis}{log} = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1084 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1085 | 12 | $edges[$i] = [ linspace( $lo, $hi, $n + 1 ) ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1086 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1087 | 12 | $s->{axis}{$axis}{expand0} = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1088 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1089 | 6 | my @count; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1090 | 6 | my $n_in = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1091 | 6 | foreach my $j ( 0 .. $#{ $xy[0] } ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1092 | 70 | my ( $x, $y ) = ( $xy[0][$j], $xy[1][$j] ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1093 | 70 | ▶ | next unless is_finite($x) && is_finite($y); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
is_finite($x) and is_finite($y)
MC/DC0%0/2
is_finite($x) and is_finite($y)
is_finite($x)
is_finite($y)
Truth table33%1/3
A and B
A= is_finite($x)B= is_finite($y)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1094 | 70 | my ( $bx, $by ) = ( bin_index( $edges[0], $x ), bin_index( $edges[1], $y ) ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1095 | 70 | ▶ | next unless ( defined $bx ) && ( defined $by ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $bx and defined $by
MC/DC0%0/2
defined $bx and defined $by
defined $bx
defined $by
Truth table33%1/3
A and B
A= defined $bxB= defined $by
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1096 | 70 | $count[$bx][$by]++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1097 | 70 | $n_in++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1098 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1099 | 6 | my $density = bool_option( $plot->{density}, 'density', $sub ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1100 | 6 | foreach my $opt ( grep { defined $plot->{$_} } ( 'cmin', 'cmax' ) ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1101 | 1 | check_numbers( [ $plot->{$opt} ], "\"$opt\"", $sub ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1102 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1103 | 6 | my ( @r, @v ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1104 | 6 | foreach my $bx ( 0 .. $#{ $edges[0] } - 1 ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1105 | 67 | foreach my $by ( 0 .. $#{ $edges[1] } - 1 ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1106 | 926 | ▶ | my $c = $count[$bx][$by] // 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$count[$bx][$by] // 0
MC/DC100%1/1
$count[$bx][$by] // 0
$count[$bx][$by]
Truth table100%2/2
A
A= $count[$bx][$by]
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1107 | 926 | ▶ | if ($density) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1108 | 0 | my $area = ( $edges[0][ $bx + 1 ] - $edges[0][$bx] ) * ( $edges[1][ $by + 1 ] - $edges[1][$by] ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1109 | 0 | ▶ | $c = ( $n_in > 0 ) ? $c / ( $n_in * $area ) : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1110 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1111 | # hist2d leaves a bin out of the colors below "cmin" or above | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1112 | # "cmax", and LogNorm leaves out a bin of 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1113 | 926 | ▶ | $c = undef if ( defined $plot->{cmin} && $c < $plot->{cmin} ) || ( defined $plot->{cmax} && $c > $plot->{cmax} ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%6/9
defined $plot->{"cmin"} and $c < $plot->{"cmin"} or defined $plot->{"cmax"} and $c > $plot->{"cmax"}
defined $plot->{"cmin"} and $c < $plot->{"cmin"}
defined $plot->{"cmax"} and $c > $plot->{"cmax"}
MC/DC50%2/4
defined $plot->{"cmin"} and $c < $plot->{"cmin"} or defined $plot->{"cmax"} and $c > $plot->{"cmax"}
defined $plot->{"cmin"}
$c < $plot->{"cmin"}
defined $plot->{"cmax"}
$c > $plot->{"cmax"}
Truth table42%3/7
A and B or C and D
A= defined $plot->{"cmin"}B= $c < $plot->{"cmin"}C= defined $plot->{"cmax"}D= $c > $plot->{"cmax"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1114 | 926 | push @r, [ $edges[0][$bx], $edges[0][ $bx + 1 ], $edges[1][$by], $edges[1][ $by + 1 ] ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1115 | 926 | push @v, $c; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1116 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1117 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1118 | 6 | my $gradient = gradient_of({ plot => $plot, values => [ grep { defined } @v ], default_cmap => 'gist_rainbow', default_title => 'Density', aes => 'fill', sub => $sub, native => $s->{native} }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1119 | 6 | ▶ | if ( $gradient->{log} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1120 | 1 | ▶ | @v = map { ( defined $_ && $_ > 0 ) ? $_ : undef } @v; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $_ and $_ > 0
MC/DC50%1/2
defined $_ and $_ > 0
defined $_
$_ > 0
Truth table66%2/3
A and B
A= defined $_B= $_ > 0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1121 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1122 | 6 | set_gradient( $s, $gradient, $where, $sub ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1123 | my $frame = r_frame( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1124 | 701 | xmin => r_nums( map { $_->[0] } @r ), xmax => r_nums( map { $_->[1] } @r ), | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1125 | 5 | ymin => r_nums( map { $_->[2] } @r ), ymax => r_nums( map { $_->[3] } @r ), | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1126 | v => r_nums(@v), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1127 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1128 | 5 | push @{ $s->{layers} }, "ggplot2::geom_rect(data = subset($frame, !is.na(v)), ggplot2::aes(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax, fill = v), colour = NA)"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1129 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1130 | 8 | ▶ | sub draw_hist2d_native { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
draw_hist2d_native: called CC=25 SCAR=32.7
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1131 | # A hist2d as geom_bin_2d bins it, with stat_bin_2d, "xbins" by "ybins" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1132 | # over the range of the data, or over "xmin" .. "xmax" and "ymin" .. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1133 | # "ymax" where any is given, as breaks. On a log axis it bins in log units, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1134 | # as the scale transforms the data, and the breaks, before the stat sees | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1135 | # them. "density" is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1136 | # stat_bin_2d's own density, the share of the points in each bin, which is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1137 | # not matplotlib's (that share divided by the bin's area). "cmin" and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1138 | # "cmax" leave out of the colors a bin below or above them, as matplotlib | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1139 | # does, by what is colored. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1140 | 8 | my ( $plot, $s, $keys, $xy, $log_axis, $where, $sub ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1141 | 8 | ▶ | my @ok = grep { is_finite( $xy->[0][$_] ) && is_finite( $xy->[1][$_] ) } 0 .. $#{ $xy->[0] }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
is_finite($xy->[0][$_]) && is_finite($xy->[1][$_])
MC/DC0%0/2
is_finite($xy->[0][$_]) && is_finite($xy->[1][$_])
is_finite($xy->[0][$_])
is_finite($xy->[1][$_])
Truth table33%1/3
A && B
A= is_finite($xy->[0][$_])B= is_finite($xy->[1][$_])
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1142 | 8 | ▶ | if ( scalar @ok == 0 ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1143 | # Refused as the matplotlib style refuses it, where an axis has no | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1144 | # range to bin over; with every range given, an empty panel. A frame of | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1145 | # no points has NULL columns, and ggplot2 died with "object 'x' not | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1146 | # found". | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1147 | 1 | foreach my $i ( 0, 1 ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1148 | 1 | my $axis = ( 'x', 'y' )[$i]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1149 | 1 | ▶ | next if scalar finite_values( @{ $xy->[$i] } ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1150 | 1 | ▶ | next if ( defined $plot->{"${axis}min"} ) && ( defined $plot->{"${axis}max"} ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $$plot{"${axis}min"} and defined $$plot{"${axis}max"}
MC/DC0%0/2
defined $$plot{"${axis}min"} and defined $$plot{"${axis}max"}
defined $$plot{"${axis}min"}
defined $$plot{"${axis}max"}
Truth table33%1/3
A and B
A= defined $$plot{"${axis}min"}B= defined $$plot{"${axis}max"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1151 | 1 | die "$sub: \"$keys->[$i]\" has no finite value, so hist2d has no range to bin it over; give \"${axis}min\" and \"${axis}max\""; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1152 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1153 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1154 | 7 | my %breaks; # axis => its breaks, for an axis with "min" or "max" given | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1155 | 7 | foreach my $i ( 0, 1 ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1156 | 14 | my $axis = ( 'x', 'y' )[$i]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1157 | 14 | ▶ | $s->{axis}{$axis}{log} = 1 if $log_axis->{$axis}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1158 | 14 | foreach my $end ( grep { defined $plot->{"$axis$_"} } ( 'min', 'max' ) ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1159 | 4 | check_numbers( [ $plot->{"$axis$end"} ], "\"$axis$end\"", $sub ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1160 | 4 | ▶ | die "$sub: \"$axis$end\" must be a finite number, not \"" . $plot->{"$axis$end"} . '"' unless is_finite( $plot->{"$axis$end"} ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1161 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1162 | 14 | ▶ | next unless grep { defined $plot->{"$axis$_"} } ( 'min', 'max' ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1163 | 3 | my @finite = finite_values( @{ $xy->[$i] } ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1164 | 3 | ▶ | my $lo = $plot->{"${axis}min"} // min(@finite); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$$plot{"${axis}min"} // &min(@finite)
MC/DC0%0/2
$$plot{"${axis}min"} // &min(@finite)
$$plot{"${axis}min"}
&min(@finite)
Truth table66%2/3
A // B
A= $$plot{"${axis}min"}B= &min(@finite)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1165 | 3 | ▶ | my $hi = $plot->{"${axis}max"} // max(@finite); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$$plot{"${axis}max"} // &max(@finite)
MC/DC0%0/2
$$plot{"${axis}max"} // &max(@finite)
$$plot{"${axis}max"}
&max(@finite)
Truth table66%2/3
A // B
A= $$plot{"${axis}max"}B= &max(@finite)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1166 | 3 | ▶ | die "$sub: \"${axis}min\" = $lo is above \"${axis}max\" = $hi" if $lo > $hi; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1167 | 3 | my $seq = 'seq(%s, %s, length.out = ' . ( $plot->{"${axis}bins"} + 1 ) . ')'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1168 | 3 | ▶ | if ( $log_axis->{$axis} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1169 | 1 | ▶ | die "$sub: \"logscale\" puts $axis on a log scale, which cannot start at $lo: give a \"${axis}min\" above 0" if $lo <= 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1170 | # Even in log units, but given in the data's: stat_bin_2d puts the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1171 | # breaks through the scale's transform, as it does the data. Given | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1172 | # as log10s, they were taken to run from 10^0 to 10^0.9 and no bin | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1173 | # held a point, under ggplot2 3.5.2 and 4.0.3 alike. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1174 | 1 | $breaks{$axis} = '10^' . sprintf( $seq, r_num( log10($lo) ), r_num( log10($hi) ) ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1175 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1176 | 2 | $breaks{$axis} = sprintf( $seq, r_num($lo), r_num($hi) ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1177 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1178 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1179 | 7 | foreach my $opt ( grep { defined $plot->{$_} } ( 'cmin', 'cmax' ) ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1180 | 2 | check_numbers( [ $plot->{$opt} ], "\"$opt\"", $sub ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1181 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1182 | 7 | ▶ | my $what = bool_option( $plot->{density}, 'density', $sub ) ? 'density' : 'count'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1183 | 7 | my @out; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1184 | 7 | ▶ | push @out, "$what < " . r_num( $plot->{cmin} ) if defined $plot->{cmin}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1185 | 7 | ▶ | push @out, "$what > " . r_num( $plot->{cmax} ) if defined $plot->{cmax}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1186 | 7 | ▶ | my $fill = @out ? 'ifelse(' . join( ' | ', @out ) . ", NA, $what)" : $what; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1187 | 7 | @{ $s->{default_label} }{ 'x', 'y' } = @{$keys}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1188 | 7 | set_gradient( $s, gradient_of({ plot => $plot, values => [], aes => 'fill', sub => $sub, native => 1, default_title => undef }), $where, $sub ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1189 | 7 | $s->{gradient}{na} = 'NA'; # a bin left out by "cmin" or "cmax" is not drawn | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1190 | 7 | ▶ | return if scalar @ok == 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1191 | # Both axes are named in "breaks", the one without a "min" or "max" as | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1192 | # NULL, which stat_bin_2d bins by "bins" as if no breaks were given. With | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1193 | # that axis left out, "breaks = list(x = ...)" was read as the breaks of | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1194 | # both, and ggplot2 4.0.3 failed with "`breaks` must be a <numeric> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1195 | # vector, not a list" -- as a warning, so the panel was drawn empty and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1196 | # the script still succeeded. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1197 | 7 | ▶ | my $breaks = %breaks ? 'breaks = list(' . join( ', ', map { "$_ = " . ( $breaks{$_} // 'NULL' ) } ( 'x', 'y' ) ) . '), ' : ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$breaks{$_} // "NULL"
MC/DC100%1/1
$breaks{$_} // "NULL"
$breaks{$_}
Truth table100%2/2
A
A= $breaks{$_}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1198 | 7 | push @{ $s->{layers} }, 'ggplot2::geom_bin_2d(data = ' . r_frame( x => r_nums( map { $xy->[0][$_] } @ok ), y => r_nums( map { $xy->[1][$_] } @ok ) ) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1199 | . ", ggplot2::aes(x = x, y = y, fill = ggplot2::after_stat($fill)), $breaks" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1200 | 7 | . 'bins = ' . r_nums( $plot->{xbins}, $plot->{ybins} ) . ', na.rm = TRUE)'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1201 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1202 | 6 | ▶ | sub log10 { return log( $_[0] ) / log(10) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
log10: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1203 | 13 | ▶ | sub draw_imshow { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
draw_imshow: called CC=26 SCAR=35.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1204 | 13 | my ( $plot, $s, $where, $sub ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1205 | 13 | cb_limits( $plot, $sub ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1206 | 13 | my $data = $plot->{data}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1207 | 13 | ▶ | die "$sub can only accept 2-D arrays as input in \"data\", but received " . ( ref $data || 'a scalar' ) if ref $data ne 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
ref $data || 'a scalar'
MC/DC0%0/1
ref $data || 'a scalar'
ref $data
Truth table0%0/2
A
A= ref $data
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1208 | 13 | my @bad_rows = grep { ref $data->[$_] ne 'ARRAY' } 0 .. $#{$data}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1209 | 13 | ▶ | die "$sub: rows " . join( ', ', @bad_rows ) . ' of "data" are not array references, which imshow needs' if scalar @bad_rows > 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1210 | 13 | ▶ | die "$sub was given no data to draw" if ( scalar @{$data} == 0 ) || ( not grep { scalar @{$_} > 0 } @{$data} ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
scalar @{$data;} == 0 or not grep({scalar @{$_;} > 0;} @{$data;})
MC/DC0%0/2
scalar @{$data;} == 0 or not grep({scalar @{$_;} > 0;} @{$data;})
scalar @{$data;} == 0
not grep({scalar @{$_;} > 0;} @{$data;})
Truth table33%1/3
A or not(B)
A= scalar @{$data;} == 0B= not grep({scalar @{$_;} > 0;} @{$data;})
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1211 | 13 | my %lengths = map { scalar @{$_} => 1 } @{$data}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1212 | 0 | ▶ | die "$sub: the rows of \"data\" have different lengths (" . join( ', ', map { scalar @{$_} } @{$data} ) . '), but imshow draws a rectangle of cells' if scalar keys %lengths > 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1213 | 13 | my @undef_cells; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1214 | 13 | foreach my $r ( 0 .. $#{$data} ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1215 | 0 | push @undef_cells, map { "[$r][$_]" } grep { not defined $data->[$r][$_] } 0 .. $#{ $data->[$r] }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1216 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1217 | 13 | ▶ | die "$sub cannot draw an undefined cell, and \"data\" has them at " . join( ', ', @undef_cells[ 0 .. min( 4, $#undef_cells ) ] ) . '; a NaN is drawn as an empty cell' if scalar @undef_cells > 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1218 | 13 | my $strings = grep { grep { not looks_like_number($_) } @{$_} } @{$data}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1219 | 13 | ▶ | die "$sub needs a map to translate strings to integers" if $strings && ( not defined $plot->{stringmap} ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$strings and not defined $plot->{"stringmap"}
MC/DC0%0/2
$strings and not defined $plot->{"stringmap"}
$strings
not defined $plot->{"stringmap"}
Truth table66%2/3
A and not(B)
A= $stringsB= not defined $plot->{"stringmap"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1220 | 13 | my ( @x, @y, @v ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1221 | 13 | foreach my $r ( 0 .. $#{$data} ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1222 | 21 | foreach my $c ( 0 .. $#{ $data->[$r] } ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1223 | 48 | push @x, $c; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1224 | 48 | push @y, $r; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1225 | 48 | push @v, $data->[$r][$c]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1226 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1227 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1228 | # Row 0 at the top, as matplotlib's "upper" origin puts it, and each cell | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1229 | # centred on its whole-number coordinates. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1230 | 13 | $s->{axis}{y}{reverse} = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1231 | 13 | $s->{axis}{$_}{expand0} = 1 foreach grep { not $s->{native} } 'x', 'y'; # ggplot2 pads a raster as it pads anything else | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1232 | 13 | my $layer_aes = 'ggplot2::aes(x = x, y = y, fill = v)'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1233 | 13 | ▶ | if ($strings) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1234 | 3 | ▶ | die "$sub: \"stringmap\" is a hash of each string in \"data\" to the label it is shown with" unless ref $plot->{stringmap} eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1235 | 3 | ▶ | die "$sub: \"cb_logscale\" means nothing for imshow with a \"stringmap\": its colors are categories, not a scale" if bool_option( $plot->{cb_logscale}, 'cb_logscale', $sub ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1236 | 3 | my @categories = sort keys %{ $plot->{stringmap} }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1237 | 3 | my %known = map { $_ => 1 } @categories; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1238 | 0 | my @unmapped = sort { $a cmp $b } keys %{ { map { $_ => 1 } grep { not $known{$_} } @v } }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1239 | 0 | ▶ | die "$sub: the values " . join( ', ', map { "\"$_\"" } @unmapped ) . ' of "data" are not keys of "stringmap", which imshow needs to give each one a color' if scalar @unmapped > 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1240 | 3 | my @colors; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1241 | 3 | ▶ | if ( $s->{native} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1242 | 1 | @colors = (undef) x scalar @categories; # ggplot2's hue scale | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1243 | } elsif ( scalar @categories <= scalar @cycle ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1244 | 1 | @colors = @cycle[ 0 .. $#categories ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1245 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1246 | # more categories than the property cycle has colors: gist_rainbow, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1247 | # resampled to as many colors as there are categories | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1248 | 1 | my @stops = colormap_colors( 'gist_rainbow', $engine, $sub ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1249 | 1 | ▶ | @colors = map { colormap_at( \@stops, ( scalar @categories == 1 ) ? 0 : $_ / $#categories ) } 0 .. $#categories; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1250 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1251 | 3 | ▶ | my $show = bool_option( $plot->{colorbar_on} // 1, 'colorbar_on', $sub ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$plot->{"colorbar_on"} // 1
MC/DC0%0/1
$plot->{"colorbar_on"} // 1
$plot->{"colorbar_on"}
Truth table50%1/2
A
A= $plot->{"colorbar_on"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1252 | $s->{category_fill} = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1253 | breaks => \@categories, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1254 | 16 | ▶ | labels => [ map { $plot->{stringmap}{$_} } @categories ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1255 | colors => \@colors, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1256 | 3 | title => defined $plot->{cblabel} ? plain_text( $plot->{cblabel}, 'cblabel', $engine, $sub ) : undef, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1257 | show => $show, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1258 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1259 | 3 | push @{ $s->{layers} }, 'ggplot2::geom_tile(data = ' . r_frame( x => r_nums(@x), y => r_nums(@y), v => r_strs(@v) ) . ", $layer_aes)"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1260 | 3 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1261 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1262 | 10 | set_gradient( $s, gradient_of({ plot => $plot, values => \@v, default_cmap => 'viridis', aes => 'fill', sub => $sub, native => $s->{native} }), $where, $sub ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1263 | 9 | ▶ | @v = map { ( $s->{gradient}{log} && looks_like_number($_) && ( $_ <= 0 ) ) ? undef : $_ } @v; # LogNorm masks what it cannot place | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
$s->{"gradient"}{"log"} and &looks_like_number($_) and $_ <= 0
$s->{"gradient"}{"log"} and &looks_like_number($_)
MC/DC0%0/3
$s->{"gradient"}{"log"} and &looks_like_number($_) and $_ <= 0
$s->{"gradient"}{"log"}
&looks_like_number($_)
$_ <= 0
Truth table25%1/4
A and B and C
A= $s->{"gradient"}{"log"}B= &looks_like_number($_)C= $_ <= 0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1264 | 9 | push @{ $s->{layers} }, 'ggplot2::geom_tile(data = ' . r_frame( x => r_nums(@x), y => r_nums(@y), v => r_nums(@v) ) . ", $layer_aes)"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1265 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1266 | 8 | ▶ | sub draw_colored_table { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
draw_colored_table: called CC=30 SCAR=37.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1267 | 8 | my ( $plot, $s, $where, $sub ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1268 | 8 | my ( $row_keys, $cols, $cells ) = table_cells( $plot, $sub ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1269 | 8 | my @rows = @{$row_keys}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1270 | 8 | ▶ | if ( defined $plot->{row_labels} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1271 | 1 | ▶ | die "$sub: \"row_labels\" is an array of one label per row" unless ref $plot->{row_labels} eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1272 | 0 | ▶ | die "$sub: \"row_labels\" has " . scalar( @{ $plot->{row_labels} } ) . ' labels, but the table has ' . scalar(@rows) . ' rows' if scalar @{ $plot->{row_labels} } != scalar @rows; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1273 | 1 | @rows = @{ $plot->{row_labels} }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1274 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1275 | 8 | ▶ | if ( defined $plot->{default_undefined} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1276 | 0 | ▶ | die "$sub: \"default_undefined\" = $plot->{default_undefined} must be a number" unless looks_like_number( $plot->{default_undefined} ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1277 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1278 | 8 | my @non_numeric; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1279 | 8 | foreach my $k1 ( @{$row_keys} ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1280 | 0 | ▶ | push @non_numeric, map { "$k1/$_ = \"$cells->{$k1}{$_}\"" } grep { ( defined $cells->{$k1}{$_} ) && ( not looks_like_number( $cells->{$k1}{$_} ) ) } @{$cols}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
defined $cells->{$k1}{$_} && !&looks_like_number($cells->{$k1}{$_})
MC/DC0%0/2
defined $cells->{$k1}{$_} && !&looks_like_number($cells->{$k1}{$_})
defined $cells->{$k1}{$_}
!&looks_like_number($cells->{$k1}{$_})
Truth table66%2/3
A && not(B)
A= defined $cells->{$k1}{$_}B= !&looks_like_number($cells->{$k1}{$_})
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1281 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1282 | 8 | ▶ | die "$sub: the cells " . join( ', ', @non_numeric ) . ' are not numbers, which colored_table needs; leave a cell out, or undefined, to draw it in "undef_color"' if scalar @non_numeric > 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1283 | 8 | foreach my $opt ( grep { defined $plot->{$_} } 'cb_min', 'cb_max' ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1284 | 2 | ▶ | die "$sub: \"$opt\" = $plot->{$opt} must be a finite number" unless looks_like_number( $plot->{$opt} ) && is_finite( $plot->{$opt} ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
&looks_like_number($plot->{$opt}) and is_finite($plot->{$opt})
MC/DC0%0/2
&looks_like_number($plot->{$opt}) and is_finite($plot->{$opt})
&looks_like_number($plot->{$opt})
is_finite($plot->{$opt})
Truth table33%1/3
A and B
A= &looks_like_number($plot->{$opt})B= is_finite($plot->{$opt})
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1285 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1286 | 8 | my ( @x, @y, @v, @text ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1287 | 8 | foreach my $r ( 0 .. $#{$row_keys} ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1288 | 15 | foreach my $c ( 0 .. $#{$cols} ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1289 | 30 | ▶ | my $value = $cells->{ $row_keys->[$r] }{ $cols->[$c] } // $plot->{default_undefined}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$$cells{$row_keys->[$r]}{$cols->[$c]} // $plot->{"default_undefined"}
MC/DC50%1/2
$$cells{$row_keys->[$r]}{$cols->[$c]} // $plot->{"default_undefined"}
$$cells{$row_keys->[$r]}{$cols->[$c]}
$plot->{"default_undefined"}
Truth table66%2/3
A // B
A= $$cells{$row_keys->[$r]}{$cols->[$c]}B= $plot->{"default_undefined"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1290 | 30 | push @x, $c + 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1291 | 30 | push @y, $r + 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1292 | 30 | push @v, $value; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1293 | 30 | ▶ | push @text, ( ( defined $value ) && ( not is_nan($value) ) ) ? ( 0 + $value ) : ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $value and not is_nan($value)
MC/DC50%1/2
defined $value and not is_nan($value)
defined $value
not is_nan($value)
Truth table66%2/3
A and not(B)
A= defined $valueB= not is_nan($value)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1294 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1295 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1296 | 8 | my @finite = finite_values(@v); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1297 | 8 | ▶ | die "$sub: no cell of the data holds a number, so colored_table has nothing to color by" if ( scalar @finite == 0 ) && ( ( not defined $plot->{cb_min} ) || ( not defined $plot->{cb_max} ) ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition16%1/6
scalar @finite == 0 and not defined $plot->{"cb_min"} && defined $plot->{"cb_max"}
defined $plot->{'cb_min'} && defined $plot->{'cb_max'}
MC/DC0%0/3
scalar @finite == 0 and not defined $plot->{"cb_min"} && defined $plot->{"cb_max"}
scalar @finite == 0
defined $plot->{'cb_min'}
defined $plot->{'cb_max'}
Truth table25%1/4
A and not(B && C)
A= scalar @finite == 0B= defined $plot->{'cb_min'}C= defined $plot->{'cb_max'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1298 | 8 | my %scale_plot = ( %{$plot}, vmin => $plot->{cb_min}, vmax => $plot->{cb_max} ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1299 | my $gradient = gradient_of({ plot => \%scale_plot, values => \@v, default_cmap => 'gist_rainbow', aes => 'fill', sub => $sub, native => $s->{native}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1300 | 8 | ▶ | na => r_str( color_of( $plot->{undef_color} // ( $s->{native} ? '#7f7f7f' : 'gray' ), '"undef_color"', $sub ) ) }); # #7f7f7f is R's grey50, ggplot2's own na.value | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$plot->{"undef_color"} // ($s->{"native"} ? "#7f7f7f" : "gray")
MC/DC100%1/1
$plot->{"undef_color"} // ($s->{"native"} ? "#7f7f7f" : "gray")
$plot->{"undef_color"}
Truth table100%2/2
A
A= $plot->{"undef_color"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1301 | 8 | ▶ | @v = map { ( $gradient->{log} && defined $_ && looks_like_number($_) && $_ <= 0 ) ? undef : $_ } @v; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%3/9
$gradient->{"log"} and defined $_ and &looks_like_number($_) and $_ <= 0
$gradient->{"log"} and defined $_ and &looks_like_number($_)
$gradient->{"log"} and defined $_
MC/DC0%0/4
$gradient->{"log"} and defined $_ and &looks_like_number($_) and $_ <= 0
$gradient->{"log"}
defined $_
&looks_like_number($_)
$_ <= 0
Truth table20%1/5
A and B and C and D
A= $gradient->{"log"}B= defined $_C= &looks_like_number($_)D= $_ <= 0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1302 | 8 | set_gradient( $s, $gradient, $where, $sub ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1303 | # The rows run down from the top, as in the table they are, in either | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1304 | # style; the column labels above it and the tiles' black borders are | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1305 | # matplotlib's table, and ggplot2's own tiles have neither. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1306 | 8 | $s->{kind} = 'table'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1307 | 8 | $s->{axis}{y}{reverse} = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1308 | 8 | ▶ | if ( not $s->{native} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1309 | 4 | $s->{axis}{x}{top} = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1310 | 4 | $s->{axis}{$_}{expand0} = 1 foreach 'x', 'y'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1311 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1312 | 8 | category_ticks( $s, 'x', [ 1 .. scalar @{$cols} ], $cols ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1313 | 8 | category_ticks( $s, 'y', [ 1 .. scalar @rows ], \@rows ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1314 | 8 | ▶ | push @{ $s->{layers} }, 'ggplot2::geom_tile(data = ' . r_frame( x => r_nums(@x), y => r_nums(@y), v => r_nums(@v) ) . ', ggplot2::aes(x = x, y = y, fill = v)' . ( $s->{native} ? '' : ', colour = "#000000", linewidth = 0.3' ) . ')'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1315 | 8 | ▶ | if ( bool_option( $plot->{show_numbers}, 'show_numbers', $sub ) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1316 | 4 | push @{ $s->{layers} }, 'ggplot2::geom_text(data = ' . r_frame( x => r_nums(@x), y => r_nums(@y), t => r_strs(@text) ) . ', ggplot2::aes(x = x, y = y, label = t), size = 10 / ggplot2::.pt)'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1317 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1318 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1319 | 9 | ▶ | sub draw_pie { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
draw_pie: called CC=16 SCAR=27.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1320 | 9 | my ( $plot, $s, $where, $sub ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1321 | 9 | my @keys = key_order_of( $plot, $plot->{data}, 'key_order', $sub ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1322 | 9 | foreach my $key (@keys) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1323 | 19 | my $v = $plot->{data}{$key}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1324 | 19 | ▶ | die "$sub: \"$key\" is not a number, which pie needs to size a wedge" unless looks_like_number($v); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1325 | 19 | ▶ | die "$sub: \"$key\" is not a finite number, which pie needs to size a wedge" unless is_finite($v); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1326 | 19 | ▶ | die "$sub: \"$key\" is $v, but pie cannot size a wedge below 0" if $v < 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1327 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1328 | 9 | ▶ | die "$sub: every wedge of pie is 0, so there is no pie to divide between them" unless grep { $plot->{data}{$_} > 0 } @keys; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1329 | 9 | my %dist = ( labeldistance => 1.1, pctdistance => 0.6 ); # matplotlib's defaults | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1330 | 9 | foreach my $arg ( grep { defined $plot->{$_} } keys %dist ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1331 | 2 | check_numbers( [ $plot->{$arg} ], "\"$arg\"", $sub ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1332 | 2 | $dist{$arg} = $plot->{$arg}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1333 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1334 | 9 | ▶ | my $autopct = $plot->{autopct} // ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$plot->{"autopct"} // ""
MC/DC100%1/1
$plot->{"autopct"} // ""
$plot->{"autopct"}
Truth table100%2/2
A
A= $plot->{"autopct"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1335 | # A printf format with one number in it, which is what matplotlib's | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1336 | # "autopct" string is; Perl's sprintf reads it the same. A Python | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1337 | # function is matplotlib's alone. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1338 | 9 | ▶ | if ( ( $autopct ne '' ) && ( ( ref $autopct ) || ( $autopct !~ m/\A(?:[^%]|%%)*%[-+ 0#]*\d*(?:\.\d+)?[dfFeEgGi](?:[^%]|%%)*\z/ ) ) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$autopct ne "" and ref $autopct || !($autopct =~ /\A(?:[^%]|%%)*%[-+ 0#]*\d*(?:\.\d+)?[dfFeEgGi](?:[^%]|%%)*\z/)
ref $autopct or not $autopct =~ /\A(?:[^%]|%%)*%[-+ 0#]*\d*(?:\.\d+)?[dfFeEgGi](?:[^%]|%%)*\z/
MC/DC66%2/3
$autopct ne "" and ref $autopct || !($autopct =~ /\A(?:[^%]|%%)*%[-+ 0#]*\d*(?:\.\d+)?[dfFeEgGi](?:[^%]|%%)*\z/)
$autopct ne ""
ref $autopct
not $autopct =~ /\A(?:[^%]|%%)*%[-+ 0#]*\d*(?:\.\d+)?[dfFeEgGi](?:[^%]|%%)*\z/
Truth table75%3/4
A and B or not(C)
A= $autopct ne ""B= ref $autopctC= not $autopct =~ /\A(?:[^%]|%%)*%[-+ 0#]*\d*(?:\.\d+)?[dfFeEgGi](?:[^%]|%%)*\z/
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1339 | 1 | die "$sub: \"autopct\" is a format with one number in it, such as '%1.1f%%', for the $engine engine, not \"$autopct\""; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1340 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1341 | 8 | $s->{kind} = 'pie'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1342 | 8 | my $total = sum( map { $plot->{data}{$_} } @keys ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1343 | 8 | my $theta = 0; # matplotlib's startangle 0: from the positive x axis, counterclockwise | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1344 | 8 | my ( @px, @py, @pg, @fills, @label_rows, @pct_rows ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1345 | 8 | foreach my $i ( 0 .. $#keys ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1346 | 18 | my $frac = $plot->{data}{ $keys[$i] } / $total; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1347 | 18 | my $t2 = $theta + 2 * 3.14159265358979 * $frac; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1348 | 18 | my $n_pts = max( 2, int( 360 * $frac ) + 1 ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1349 | 18 | my @ring = map { $theta + ( $t2 - $theta ) * $_ / ( $n_pts - 1 ) } 0 .. $n_pts - 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1350 | 18 | push @px, 0, map { cos } @ring; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1351 | 18 | push @py, 0, map { sin } @ring; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1352 | 18 | push @pg, ($i) x ( $n_pts + 1 ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1353 | 18 | push @fills, next_color( $s, 'patch' ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1354 | 18 | my $mid = ( $theta + $t2 ) / 2; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1355 | 18 | my ( $lx, $ly ) = ( $dist{labeldistance} * cos $mid, $dist{labeldistance} * sin $mid ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1356 | 18 | ▶ | push @label_rows, [ $lx, $ly, $keys[$i], ( $lx > 0 ) ? 0 : 1 ]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1357 | 18 | ▶ | push @pct_rows, [ $dist{pctdistance} * cos $mid, $dist{pctdistance} * sin $mid, sprintf( $autopct, 100 * $frac ) ] if $autopct ne ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1358 | 18 | $theta = $t2; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1359 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1360 | # Under "native" the wedges take ggplot2's hue scale, as many colors as | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1361 | # there are wedges; their shapes are still worked out above, since ggplot2 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1362 | # has no stat of its own for a pie. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1363 | 8 | ▶ | my $fill = $s->{native} ? 'scales::hue_pal()(' . scalar(@keys) . ')[' . r_nums( map { $_ + 1 } @pg ) . ']' : r_strs( map { $fills[$_] } @pg ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1364 | 8 | push @{ $s->{layers} }, 'ggplot2::geom_polygon(data = ' . r_frame( x => r_nums(@px), y => r_nums(@py), g => r_nums(@pg) ) . ', ggplot2::aes(x = x, y = y, group = g), fill = ' . $fill . ', colour = NA)'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1365 | 8 | push @{ $s->{layers} }, 'ggplot2::geom_text(data = ' . r_frame( x => r_nums( map { $_->[0] } @label_rows ), y => r_nums( map { $_->[1] } @label_rows ), t => r_strs( map { $_->[2] } @label_rows ), h => r_nums( map { $_->[3] } @label_rows ) ) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1366 | . ', ggplot2::aes(x = x, y = y, label = t, hjust = h), size = 10 / ggplot2::.pt)'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1367 | 8 | ▶ | if (@pct_rows) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1368 | 4 | push @{ $s->{layers} }, 'ggplot2::geom_text(data = ' . r_frame( x => r_nums( map { $_->[0] } @pct_rows ), y => r_nums( map { $_->[1] } @pct_rows ), t => r_strs( map { $_->[2] } @pct_rows ) ) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1369 | . ', ggplot2::aes(x = x, y = y, label = t), size = 10 / ggplot2::.pt)'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1370 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1371 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1372 | 18 | ▶ | sub draw_plot { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
draw_plot: called CC=34 SCAR=38.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1373 | 18 | my ( $plot, $s, $where, $sub ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1374 | 18 | ▶ | my $show_legend = bool_option( $plot->{show_legend} // 1, 'show_legend', $sub ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$plot->{"show_legend"} // 1
MC/DC100%1/1
$plot->{"show_legend"} // 1
$plot->{"show_legend"}
Truth table100%2/2
A
A= $plot->{"show_legend"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1375 | 18 | $s->{axis}{$_}{log} = 1 foreach logscale_axes( $plot->{logscale}, $sub ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1376 | 18 | my $data = $plot->{data}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1377 | 18 | my @lines; # [ name or undef, \@x, \@y ] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1378 | 18 | ▶ | if ( ( ref $data eq 'ARRAY' ) && ( scalar @{$data} == 2 ) && ( ref $data->[0] eq 'ARRAY' ) && ( ref $data->[1] eq 'ARRAY' ) && ( ref $data->[0][0] ne 'ARRAY' ) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition75%9/12
ref $data eq "ARRAY" and scalar @{$data;} == 2 and ref $data->[0] eq "ARRAY" and ref $data->[1] eq "ARRAY" and ref $data->[0][0] ne "ARRAY"
ref $data eq "ARRAY" and scalar @{$data;} == 2 and ref $data->[0] eq "ARRAY" and ref $data->[1] eq "ARRAY"
ref $data eq "ARRAY" and scalar @{$data;} == 2 and ref $data->[0] eq "ARRAY"
ref $data eq "ARRAY" and scalar @{$data;} == 2
MC/DC40%2/5
ref $data eq "ARRAY" and scalar @{$data;} == 2 and ref $data->[0] eq "ARRAY" and ref $data->[1] eq "ARRAY" and ref $data->[0][0] ne "ARRAY"
ref $data eq "ARRAY"
scalar @{$data;} == 2
ref $data->[0] eq "ARRAY"
ref $data->[1] eq "ARRAY"
ref $data->[0][0] ne "ARRAY"
Truth table50%3/6
A and B and C and D and E
A= ref $data eq "ARRAY"B= scalar @{$data;} == 2C= ref $data->[0] eq "ARRAY"D= ref $data->[1] eq "ARRAY"E= ref $data->[0][0] ne "ARRAY"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1379 | 5 | $data = [$data]; # one line given as [ \@x, \@y ] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1380 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1381 | 18 | ▶ | if ( ref $data eq 'ARRAY' ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1382 | 6 | foreach my $i ( 0 .. $#{$data} ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1383 | 7 | my $arr = $data->[$i]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1384 | 7 | ▶ | die "$sub: index $i is \"" . ref($arr) . '" but must be "ARRAY"' if ref $arr ne 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1385 | 0 | ▶ | die "$sub: there must be 2 array references (x, y) but index $i has " . scalar( @{$arr} ) if scalar @{$arr} != 2; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1386 | 7 | push @lines, [ undef, @{$arr}, "index $i" ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1387 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1388 | } elsif ( ref $data eq 'HASH' ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1389 | 12 | foreach my $set ( key_order_of( $plot, $data, 'key_order', $sub ) ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1390 | 18 | my $xy = $data->{$set}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1391 | 18 | ▶ | die "$sub: $set must have two arrays, x and y coordinates, or one array of y values, but instead has a " . ( ref $xy || 'scalar' ) if ref $xy ne 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
ref $xy || 'scalar'
MC/DC0%0/1
ref $xy || 'scalar'
ref $xy
Truth table0%0/2
A
A= ref $xy
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1392 | 18 | my $n_refs = grep { ref } @{$xy}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1393 | 18 | ▶ | if ( ( $n_refs == 0 ) && ( scalar @{$xy} > 0 ) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
$n_refs == 0 and scalar @{$xy;} > 0
MC/DC50%1/2
$n_refs == 0 and scalar @{$xy;} > 0
$n_refs == 0
scalar @{$xy;} > 0
Truth table66%2/3
A and B
A= $n_refs == 0B= scalar @{$xy;} > 0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1394 | 4 | $xy = [ [ 0 .. $#{$xy} ], $xy ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1395 | 14 | } elsif ( $n_refs < scalar @{$xy} ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1396 | 0 | die "$sub: set \"$set\" mixes arrays with plain values: it must be [ \\\@x, \\\@y ] or one array of y values"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1397 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1398 | 0 | ▶ | die "$sub: " . scalar( @{$xy} ) . " were entered for $set, but there must be exactly 2" if scalar @{$xy} != 2; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1399 | 18 | ▶ | push @lines, [ ( $show_legend ? $set : undef ), @{$xy}, "set \"$set\"" ]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1400 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1401 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1402 | 0 | ▶ | die "$sub: \"data\" for plot is an array of [ \\\@x, \\\@y ] pairs or a hash of them, not " . ( ref $data || 'a scalar' ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
ref $data || 'a scalar'
MC/DC0%0/1
ref $data || 'a scalar'
ref $data
Truth table0%0/2
A
A= ref $data
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1403 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1404 | 18 | foreach my $line (@lines) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1405 | 25 | my ( $name, $x, $y, $what ) = @{$line}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1406 | 0 | ▶ | die "$sub: $what has length = " . scalar( @{$x} ) . ' for x; length = ' . scalar( @{$y} ) . ' for y: x & y must be of equal length' if scalar @{$x} != scalar @{$y}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1407 | 25 | foreach my $axis ( [ 'x', $x ], [ 'y', $y ] ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1408 | 50 | ▶ | my @bad = grep { ( not defined $_ ) || ( not looks_like_number($_) ) } @{ $axis->[1] }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
defined $_ && &looks_like_number($_)
MC/DC0%0/2
defined $_ && &looks_like_number($_)
defined $_
&looks_like_number($_)
Truth table33%1/3
A && B
A= defined $_B= &looks_like_number($_)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1409 | 0 | ▶ | die "$sub: $what axis $axis->[0] has undefined or non-numeric values: \"" . join( '", "', map { $_ // 'undef' } @bad ) . '"' if scalar @bad > 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$_ // "undef"
MC/DC0%0/1
$_ // "undef"
$_
Truth table0%0/2
A
A= $_
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1410 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1411 | 25 | my $color = next_color( $s, 'line' ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1412 | # a value matplotlib cannot place, NaN or infinite, is a gap in the line | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1413 | 25 | ▶ | my $frame = r_frame( x => r_nums( map { is_finite($_) ? $_ : undef } @{$x} ), y => r_nums( map { is_finite($_) ? $_ : undef } @{$y} ) ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1414 | 25 | my $lw = line_width($s); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1415 | 25 | ▶ | add_legend( $s->{colour_d}, $name, $color ) if defined $name; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1416 | # A line of no points is drawn as nothing, as matplotlib draws it, its | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1417 | # color taken from the cycle all the same; R's cbind() cannot add a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1418 | # column to a frame of no rows, and died as "differing number of rows". | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1419 | 25 | ▶ | next if scalar @{$x} == 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1420 | 23 | ▶ | if ( defined $name ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1421 | 14 | push @{ $s->{layers} }, "ggplot2::geom_path(data = cbind($frame, s = " . r_str($name) . "), ggplot2::aes(x = x, y = y, colour = s)$lw, na.rm = TRUE)"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1422 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1423 | 9 | ▶ | push @{ $s->{layers} }, "ggplot2::geom_path(data = $frame, ggplot2::aes(x = x, y = y)" . ( defined $color ? ', colour = ' . r_str($color) : '' ) . "$lw, na.rm = TRUE)"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1424 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1425 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1426 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1427 | 23 | ▶ | sub draw_scatter { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
draw_scatter: called CC=29 SCAR=34.6
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1428 | 23 | my ( $plot, $s, $where, $sub ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1429 | 23 | foreach my $cb ( grep { defined $plot->{$_} } ( 'cb_min', 'cb_max' ) ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1430 | 2 | check_numbers( [ $plot->{$cb} ], "\"$cb\"", $sub ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1431 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1432 | 23 | cb_limits( $plot, $sub ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1433 | 23 | my $data = $plot->{data}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1434 | 23 | ▶ | die "$sub: scatter only takes a hash of arrays or a hash of hashes, not " . ( ref $data || 'a scalar' ) if ref $data ne 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
ref $data || 'a scalar'
MC/DC0%0/1
ref $data || 'a scalar'
ref $data
Truth table0%0/2
A
A= ref $data
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1435 | 23 | my %ref_counts; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1436 | 23 | $ref_counts{ ref $data->{$_} }++ foreach keys %{$data}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1437 | 23 | ▶ | die "$sub: different kinds of data were entered$where; should be a hash of arrays or a hash of hashes" if scalar keys %ref_counts > 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1438 | 23 | $s->{axis}{$_}{log} = 1 foreach logscale_axes( $plot->{logscale}, $sub ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1439 | 23 | ▶ | my $sorted = sub { sort { lc $a cmp lc $b || $a cmp $b } @_ }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
Condition33%1/3
lc $a cmp lc $b || $a cmp $b
MC/DC0%0/2
lc $a cmp lc $b || $a cmp $b
lc $a cmp lc $b
$a cmp $b
Truth table33%1/3
A || B
A= lc $a cmp lc $bB= $a cmp $b
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1440 | # x, y and color keys of one set, as scatter_helper settles them | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1441 | 31 | ▶ | my $axes_of = sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=13 SCAR=26.7
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1442 | 31 | my ( $set, $what ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1443 | 31 | ▶ | my @keys = defined $plot->{keys} ? check_key_order({ keys => $plot->{keys}, data => $set, option => 'keys', sub => $sub }) : $sorted->( keys %{$set} ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1444 | 31 | my $n_keys = scalar keys %{$set}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1445 | 31 | ▶ | die "$sub: scatterplots can only take 2 or 3 keys as data, but$what has $n_keys" if ( $n_keys != 2 ) && ( $n_keys != 3 ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$n_keys != 2 and $n_keys != 3
MC/DC0%0/2
$n_keys != 2 and $n_keys != 3
$n_keys != 2
$n_keys != 3
Truth table66%2/3
A and B
A= $n_keys != 2B= $n_keys != 3
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1446 | 31 | my $color_key; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1447 | 31 | ▶ | if ( defined $plot->{color_key} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1448 | 3 | $color_key = $plot->{color_key}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1449 | # before the keys are counted, or a "color_key" that is not a key of | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1450 | # three is reported as leaving 3 keys, without its name | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1451 | 3 | ▶ | die "$sub: \"$color_key\" isn't defined$what" unless defined $set->{$color_key}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1452 | 1 | @keys = grep { $_ ne $color_key } @keys; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1453 | } elsif ( scalar @keys == 3 ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1454 | 8 | $color_key = pop @keys; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1455 | } elsif ( $n_keys == 3 ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1456 | 0 | my %named = map { $_ => 1 } @keys; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1457 | 0 | ($color_key) = grep { not $named{$_} } $sorted->( keys %{$set} ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1458 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1459 | 29 | ▶ | die "$sub$what needs one key for x and one for y, but \"keys\" and \"color_key\" leave " . scalar(@keys) if scalar @keys != 2; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1460 | 29 | foreach my $key (@keys) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1461 | 58 | check_scatter_values( $set->{$key}, "\"$key\"$what", $sub ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1462 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1463 | 29 | ▶ | if ( defined $color_key ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1464 | 9 | ▶ | die "$sub: \"$color_key\" isn't defined$what" unless defined $set->{$color_key}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1465 | 9 | check_scatter_values( $set->{$color_key}, "\"$color_key\"$what", $sub ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1466 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1467 | 29 | ▶ | check_scatter_lengths( $set, [ @keys, ( defined $color_key ? $color_key : () ) ], $what, $sub ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1468 | 29 | return ( \@keys, $color_key ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1469 | 23 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1470 | 23 | my @sets; # [ name or undef, \%set, \@xy keys, color key ] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1471 | 23 | ▶ | if ( defined $ref_counts{ARRAY} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1472 | 14 | ▶ | die "$sub: \"key_order\" orders the sets of a hash of sets; for the axes of a single set, use \"keys\"" if defined $plot->{key_order}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1473 | 14 | my ( $keys, $color_key ) = $axes_of->( $data, '' ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1474 | 13 | push @sets, [ undef, $data, $keys, $color_key ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1475 | } elsif ( defined $ref_counts{HASH} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1476 | 9 | ▶ | my $show_legend = bool_option( $plot->{show_legend} // 1, 'show_legend', $sub ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$plot->{"show_legend"} // 1
MC/DC100%1/1
$plot->{"show_legend"} // 1
$plot->{"show_legend"}
Truth table100%2/2
A
A= $plot->{"show_legend"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1477 | 9 | foreach my $name ( key_order_of( $plot, $data, 'key_order', $sub ) ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1478 | 17 | my $inner = $data->{$name}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1479 | # a set of numbers alone is one point | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1480 | 17 | ▶ | $inner = { map { $_ => [ $inner->{$_} ] } keys %{$inner} } if ( scalar keys %{$inner} > 0 ) && ( not grep { ref } values %{$inner} ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
scalar keys %{$inner;} > 0 and not grep({ref $_;} values %{$inner;})
MC/DC50%1/2
scalar keys %{$inner;} > 0 and not grep({ref $_;} values %{$inner;})
scalar keys %{$inner;} > 0
not grep({ref $_;} values %{$inner;})
Truth table66%2/3
A and not(B)
A= scalar keys %{$inner;} > 0B= not grep({ref $_;} values %{$inner;})
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1481 | 17 | my ( $keys, $color_key ) = $axes_of->( $inner, " of set \"$name\"" ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1482 | 16 | ▶ | push @sets, [ ( $show_legend ? $name : undef ), $inner, $keys, $color_key ]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1483 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1484 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1485 | 0 | die "$sub: Could not determine the scatter type of \"data\"$where"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1486 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1487 | 21 | @{ $s->{default_label} }{ 'x', 'y' } = @{ $sets[-1][2] }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1488 | 21 | my @colored = grep { defined $_->[3] } @sets; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1489 | 21 | ▶ | if (@colored) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1490 | 8 | my @z = map { @{ $_->[1]{ $_->[3] } } } @colored; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1491 | 8 | set_gradient( $s, gradient_of({ plot => $plot, values => \@z, default_cmap => 'gist_rainbow', default_title => $colored[-1][3], aes => 'colour', sub => $sub, native => $s->{native} }), $where, $sub ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1492 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1493 | # Sets colored by value are told apart by their markers, in the order of | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1494 | # scatter_helper's (o s ^ D v P X * < > p h d) as near as R's own point | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1495 | # shapes come; the list repeats once it runs out. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1496 | 21 | my @shapes = ( 16, 15, 17, 18, 25, 3, 4, 8, 1, 0, 2, 5, 6 ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1497 | 21 | my $shape_i = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1498 | 21 | foreach my $set (@sets) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1499 | 29 | my ( $name, $values, $keys, $color_key ) = @{$set}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1500 | # A set of no points is drawn as nothing, as matplotlib draws it: its | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1501 | # empty columns were written c(), which R reads as NULL and leaves out | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1502 | # of the frame, and ggplot2 died with "object 'x' not found". | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1503 | 29 | ▶ | next if scalar @{ $values->{ $keys->[0] } } == 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1504 | 25 | my @cols = ( x => r_nums( @{ $values->{ $keys->[0] } } ), y => r_nums( @{ $values->{ $keys->[1] } } ) ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1505 | 25 | ▶ | if ( defined $color_key ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1506 | 9 | my @z = @{ $values->{$color_key} }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1507 | 0 | ▶ | @z = map { $_ > 0 ? $_ : undef } @z if $s->{gradient}{log}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1508 | 9 | push @cols, z => r_nums(@z); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1509 | # Under "native" the sets take ggplot2's own shape scale, in the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1510 | # order of the sets, and a lone set its default point. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1511 | 9 | ▶ | my $shape = $s->{native} ? undef : ( scalar @sets > 1 ) ? $shapes[ $shape_i++ % scalar @shapes ] : 16; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1512 | 9 | my $size = marker_size($s); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1513 | 9 | ▶ | if ( defined $name ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1514 | 2 | add_legend( $s->{shape_d}, $name, $shape ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1515 | 2 | ▶ | push @{ $s->{layers} }, 'ggplot2::geom_point(data = cbind(' . r_frame(@cols) . ', s = ' . r_str($name) . "), ggplot2::aes(x = x, y = y, colour = z, shape = s)" . ( $s->{native} ? '' : ', fill = NA' ) . "$size, na.rm = TRUE)"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1516 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1517 | 7 | ▶ | push @{ $s->{layers} }, 'ggplot2::geom_point(data = ' . r_frame(@cols) . ", ggplot2::aes(x = x, y = y, colour = z)" . ( defined $shape ? ", shape = $shape" : '' ) . "$size, na.rm = TRUE)"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1518 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1519 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1520 | 16 | my $color = next_color( $s, 'patch' ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1521 | 16 | ▶ | my $style = $s->{native} ? marker_size($s) : ", shape = 16" . marker_size($s); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1522 | 16 | ▶ | if ( defined $name ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1523 | 12 | add_legend( $s->{colour_d}, $name, $color ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1524 | 12 | push @{ $s->{layers} }, 'ggplot2::geom_point(data = cbind(' . r_frame(@cols) . ', s = ' . r_str($name) . "), ggplot2::aes(x = x, y = y, colour = s)$style, na.rm = TRUE)"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1525 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1526 | 4 | ▶ | push @{ $s->{layers} }, 'ggplot2::geom_point(data = ' . r_frame(@cols) . ', ggplot2::aes(x = x, y = y)' . ( defined $color ? ', colour = ' . r_str($color) : '' ) . "$style, na.rm = TRUE)"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1527 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1528 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1529 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1530 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1531 | 7 | ▶ | sub wide_summary { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
wide_summary: called CC=16 SCAR=28.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1532 | # The runs of one "wide" group, summarised as write_wide_group in | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1533 | # Matplotlib::Simple::Matplotlib summarises them: each run sorted by x and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1534 | # interpolated onto 101 points across the group's x range, NaN outside the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1535 | # run's own range, and at each point the mean and the population standard | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1536 | # deviation of the runs that reach it. Returns ( \@base_x, \@mean, \@sd ). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1537 | 7 | my ($runs) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1538 | 7 | my ( $lo, $hi ) = ( 9**9**9, -9**9**9 ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1539 | 7 | foreach my $run ( @{$runs} ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1540 | 14 | my @f = finite_values( @{ $run->[0] } ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1541 | 14 | $lo = min( $lo, @f ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1542 | 14 | $hi = max( $hi, @f ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1543 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1544 | 7 | my @base = linspace( $lo, $hi, 101 ); # as the matplotlib engine's np.linspace(min, max, 101) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1545 | 7 | my @ys; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1546 | 7 | foreach my $run ( @{$runs} ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1547 | 14 | my @order = sort { $run->[0][$a] <=> $run->[0][$b] } 0 .. $#{ $run->[0] }; # Perl's sort is stable, as numpy's 'stable' is | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1548 | 14 | my @x = @{ $run->[0] }[@order]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1549 | 14 | my @y = @{ $run->[1] }[@order]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1550 | 14 | my @interp; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1551 | 14 | foreach my $bx (@base) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1552 | 1414 | my $v; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1553 | 1414 | ▶ | if ( ( $bx >= $x[0] ) && ( $bx <= $x[-1] ) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$bx >= $x[0] and $bx <= $x[-1]
MC/DC50%1/2
$bx >= $x[0] and $bx <= $x[-1]
$bx >= $x[0]
$bx <= $x[-1]
Truth table66%2/3
A and B
A= $bx >= $x[0]B= $bx <= $x[-1]
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1554 | 1314 | my $j = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1555 | 1314 | ▶ | $j++ while ( $j < $#x - 1 ) && ( $x[ $j + 1 ] < $bx ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$j < $#x - 1 and $x[$j + 1] < $bx
MC/DC100%2/2
$j < $#x - 1 and $x[$j + 1] < $bx
$j < $#x - 1
$x[$j + 1] < $bx
Truth table100%3/3
A and B
A= $j < $#x - 1B= $x[$j + 1] < $bx
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1556 | 1314 | ▶ | if ( $bx == $x[$j] ) { $v = $y[$j] } # numpy returns a sample point's own value | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1557 | 22 | ▶ | elsif ( ( $j < $#x ) && ( $bx == $x[ $j + 1 ] ) ) { $v = $y[ $j + 1 ] } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$j < $#x and $bx == $x[$j + 1]
MC/DC50%1/2
$j < $#x and $bx == $x[$j + 1]
$j < $#x
$bx == $x[$j + 1]
Truth table66%2/3
A and B
A= $j < $#xB= $bx == $x[$j + 1]
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1558 | 1278 | elsif ( $j < $#x ) { $v = $y[$j] + ( $y[ $j + 1 ] - $y[$j] ) * ( $bx - $x[$j] ) / ( $x[ $j + 1 ] - $x[$j] ) } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1559 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1560 | 1414 | ▶ | push @interp, ( ( defined $v ) && ( not is_nan($v) ) ) ? $v : undef; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $v and not is_nan($v)
MC/DC50%1/2
defined $v and not is_nan($v)
defined $v
not is_nan($v)
Truth table66%2/3
A and not(B)
A= defined $vB= not is_nan($v)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1561 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1562 | 14 | push @ys, \@interp; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1563 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1564 | 7 | my ( @mean, @sd ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1565 | 7 | foreach my $i ( 0 .. $#base ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1566 | 707 | my @at = grep { defined } map { $_->[$i] } @ys; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1567 | 707 | ▶ | if ( scalar @at == 0 ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1568 | 0 | push @mean, undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1569 | 0 | push @sd, undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1570 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1571 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1572 | 707 | my $m = sum(@at) / scalar @at; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1573 | 707 | push @mean, $m; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1574 | 707 | push @sd, sqrt( sum( map { ( $_ - $m )**2 } @at ) / scalar @at ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1575 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1576 | 7 | return ( \@base, \@mean, \@sd ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1577 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1578 | 6 | ▶ | sub draw_wide { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
draw_wide: called CC=31 SCAR=35.3
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1579 | 6 | my ( $plot, $s, $where, $sub ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1580 | 6 | ▶ | my $show_legend = bool_option( $plot->{show_legend} // 1, 'show_legend', $sub ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$plot->{"show_legend"} // 1
MC/DC100%1/1
$plot->{"show_legend"} // 1
$plot->{"show_legend"}
Truth table100%2/2
A
A= $plot->{"show_legend"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1581 | 6 | my @groups; # [ name or undef, runs, color ] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1582 | 6 | ▶ | if ( ref $plot->{data} eq 'HASH' ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1583 | 5 | ▶ | die "$sub: \"data\" is a HASH of groups, so \"color\" must be a HASH of one color per group" if ( defined $plot->{color} ) && ( ref $plot->{color} ne 'HASH' ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $plot->{"color"} and ref $plot->{"color"} ne "HASH"
MC/DC0%0/2
defined $plot->{"color"} and ref $plot->{"color"} ne "HASH"
defined $plot->{"color"}
ref $plot->{"color"} ne "HASH"
Truth table66%2/3
A and B
A= defined $plot->{"color"}B= ref $plot->{"color"} ne "HASH"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1584 | 5 | my $i = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1585 | 5 | foreach my $group ( sort keys %{ $plot->{data} } ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1586 | 5 | check_wide_runs( $plot->{data}{$group}, "group \"$group\"", $sub ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1587 | 5 | ▶ | my $c = defined $plot->{color}{$group} ? color_of( $plot->{color}{$group}, "\"color\" of \"$group\"", $sub ) : $s->{native} ? undef : $cycle[ $i % scalar @cycle ]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1588 | 5 | ▶ | push @groups, [ ( ( $show_legend || $s->{native} ) ? $group : undef ), $plot->{data}{$group}, $c ]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$show_legend or $s->{"native"}
MC/DC50%1/2
$show_legend or $s->{"native"}
$show_legend
$s->{"native"}
Truth table66%2/3
A or B
A= $show_legendB= $s->{"native"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1589 | 5 | $i++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1590 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1591 | } elsif ( ref $plot->{data} eq 'ARRAY' ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1592 | 1 | ▶ | die "$sub: \"data\" is one unlabelled group, so \"color\" must be a single color, not a " . ref( $plot->{color} ) . ' reference' if ref $plot->{color}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1593 | 1 | check_wide_runs( $plot->{data}, '"data"', $sub ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1594 | 1 | ▶ | push @groups, [ undef, $plot->{data}, ( ( defined $plot->{color} ) || ( not $s->{native} ) ) ? color_of( $plot->{color} // 'b', '"color"', $sub ) : undef ]; # undef: ggplot2's own black line and grey band | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition40%2/5
defined $plot->{"color"} or not $s->{"native"}
$plot->{"color"} // "b"
MC/DC0%0/2
defined $plot->{"color"} or not $s->{"native"}
defined $plot->{"color"}
not $s->{"native"}
MC/DC0%0/1
$plot->{"color"} // "b"
$plot->{"color"}
Truth table33%1/3
A or not(B)
A= defined $plot->{"color"}B= not $s->{"native"}
Truth table50%1/2
A
A= $plot->{"color"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1595 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1596 | 0 | ▶ | die "$sub cannot take ref type \"" . ( ref $plot->{data} || 'scalar' ) . '" for "data"'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
ref $plot->{'data'} || 'scalar'
MC/DC0%0/1
ref $plot->{'data'} || 'scalar'
ref $plot->{'data'}
Truth table0%0/2
A
A= ref $plot->{'data'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1597 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1598 | # Under "native" every group with a name is mapped to the colour and fill | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1599 | # scales, which give it ggplot2's hue where it has no color of its own and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1600 | # keep one legend, of the colour; with "show_legend" off the legend is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1601 | # hidden, but the groups are still told apart. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1602 | 6 | ▶ | $s->{guide_none}{colour} = 1 if $s->{native} && ( not $show_legend ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$s->{"native"} and not $show_legend
MC/DC0%0/2
$s->{"native"} and not $show_legend
$s->{"native"}
not $show_legend
Truth table66%2/3
A and not(B)
A= $s->{"native"}B= not $show_legend
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1603 | 6 | foreach my $group (@groups) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1604 | 6 | my ( $name, $runs, $color ) = @{$group}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1605 | 6 | ▶ | my $mapped = $s->{native} && ( defined $name ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$s->{'native'} && defined $name
MC/DC50%1/2
$s->{'native'} && defined $name
$s->{'native'}
defined $name
Truth table66%2/3
A && B
A= $s->{'native'}B= defined $name
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1606 | 6 | my ( $line_aes, $fill_aes, $line_fix, $fill_fix ) = ( '', '', '', '' ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1607 | 6 | ▶ | if ($mapped) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1608 | 1 | ( $line_aes, $fill_aes ) = ( ', colour = s', ', fill = s' ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1609 | 1 | add_legend( $s->{colour_d}, $name, $color ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1610 | 1 | add_legend( $s->{fill_d}, $name, $color ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1611 | 1 | $s->{guide_none}{fill} = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1612 | } elsif ( defined $color ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1613 | 5 | ( $line_fix, $fill_fix ) = ( ', colour = ' . r_str($color), ', fill = ' . r_str($color) ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1614 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1615 | 6 | ▶ | my $named = sub { my ($frame) = @_; return $mapped ? "cbind($frame, s = " . r_str($name) . ')' : $frame }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1616 | 6 | my $lw = line_width($s); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1617 | 6 | foreach my $run ( @{$runs} ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1618 | 12 | my @order = sort { $run->[0][$a] <=> $run->[0][$b] } 0 .. $#{ $run->[0] }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1619 | 12 | ▶ | push @{ $s->{layers} }, 'ggplot2::geom_path(data = ' . $named->( r_frame( x => r_nums( @{ $run->[0] }[@order] ), y => r_nums( map { is_finite($_) ? $_ : undef } @{ $run->[1] }[@order] ) ) ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1620 | . ", ggplot2::aes(x = x, y = y$line_aes)$line_fix, alpha = 0.15$lw, na.rm = TRUE)"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1621 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1622 | 6 | my ( $base, $mean, $sd ) = wide_summary($runs); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1623 | # The band of one standard deviation each side of the mean, as one | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1624 | # polygon for each stretch of x where the mean is defined: geom_ribbon | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1625 | # draws a polygon per interval, and in a PNG the seams between them | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1626 | # showed as faint vertical streaks. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1627 | 6 | my ( @px, @py, @pg, @run ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1628 | 6 | my $g = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1629 | 6 | foreach my $i ( 0 .. $#{$base}, undef ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1630 | 612 | ▶ | if ( ( defined $i ) && ( defined $mean->[$i] ) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $i and defined $mean->[$i]
MC/DC50%1/2
defined $i and defined $mean->[$i]
defined $i
defined $mean->[$i]
Truth table66%2/3
A and B
A= defined $iB= defined $mean->[$i]
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1631 | 606 | push @run, $i; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1632 | 606 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1633 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1634 | 6 | ▶ | if ( scalar @run > 1 ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1635 | 6 | push @px, @{$base}[@run], reverse @{$base}[@run]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1636 | 6 | push @py, ( map { $mean->[$_] - $sd->[$_] } @run ), ( map { $mean->[$_] + $sd->[$_] } reverse @run ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1637 | 6 | push @pg, ($g) x ( 2 * scalar @run ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1638 | 6 | $g++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1639 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1640 | 6 | @run = (); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1641 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1642 | 6 | ▶ | if (@px) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1643 | 6 | push @{ $s->{layers} }, 'ggplot2::geom_polygon(data = ' . $named->( r_frame( x => r_nums(@px), y => r_nums(@py), g => r_nums(@pg) ) ) . ", ggplot2::aes(x = x, y = y, group = g$fill_aes)$fill_fix, alpha = 0.3, colour = NA)"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1644 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1645 | 6 | my $frame = r_frame( x => r_nums( @{$base} ), m => r_nums( @{$mean} ) ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1646 | 6 | ▶ | if ( ( defined $name ) && ( not $s->{native} ) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $name and not $s->{"native"}
MC/DC100%2/2
defined $name and not $s->{"native"}
defined $name
not $s->{"native"}
Truth table100%3/3
A and not(B)
A= defined $nameB= not $s->{"native"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1647 | 3 | add_legend( $s->{colour_d}, $name, $color ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1648 | 3 | push @{ $s->{layers} }, "ggplot2::geom_path(data = cbind($frame, s = " . r_str($name) . "), ggplot2::aes(x = x, y = m, colour = s)$lw, na.rm = TRUE)"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1649 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1650 | 3 | push @{ $s->{layers} }, 'ggplot2::geom_path(data = ' . $named->($frame) . ", ggplot2::aes(x = x, y = m$line_aes)$line_fix$lw, na.rm = TRUE)"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1651 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1652 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1653 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1654 | 5 | my %drawer = ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1655 | bar => \&draw_bar, barh => \&draw_bar, boxplot => \&draw_boxplot, colored_table => \&draw_colored_table, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1656 | hist => \&draw_hist, hist2d => \&draw_hist2d, imshow => \&draw_imshow, pie => \&draw_pie, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1657 | plot => \&draw_plot, scatter => \&draw_scatter, violin => \&draw_violin, violinplot => \&draw_violin, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1658 | wide => \&draw_wide, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1659 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1660 | 11 | ▶ | sub last_value { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
last_value: called CC=2 SCAR=7.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1661 | # An option that matplotlib calls once per element of an array: the last | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1662 | # call is the one that stands. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1663 | 11 | my ($v) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1664 | 11 | ▶ | return ( ref $v eq 'ARRAY' ) ? $v->[-1] : $v; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1665 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1666 | 183 | ▶ | sub apply_axes_keywords { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
apply_axes_keywords: called CC=11 SCAR=24.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1667 | # The title, the axis labels, ranges and scales one plot hash asks for. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1668 | # The pyplot forms ("title", "xlabel", "ylim", "xscale") come after the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1669 | # axes forms ("set_title", ...), as matplotlib's engine writes them, so | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1670 | # where both are given the pyplot one stands. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1671 | 183 | my ( $plot, $s, $where, $sub ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1672 | 183 | my %text = ( title => [ 'set_title', 'title' ], x => [ 'set_xlabel', 'xlabel' ], y => [ 'set_ylabel', 'ylabel' ] ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1673 | 183 | foreach my $what ( sort keys %text ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1674 | 545 | foreach my $option ( grep { defined $plot->{$_} } @{ $text{$what} } ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1675 | 10 | my $value = last_value( $plot->{$option} ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1676 | 10 | ▶ | die "$sub: \"$option\"$where is text, not a " . ref($value) . ' reference' if ref $value; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1677 | 10 | $s->{text}{$what} = plain_text( $value, $option, $engine, $sub ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1678 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1679 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1680 | 181 | foreach my $option ( grep { defined $plot->{$_} } ( 'set_xlim', 'set_ylim', 'ylim' ) ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1681 | 2 | ▶ | my $axis = ( $option =~ m/x/ ) ? 'x' : 'y'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1682 | 2 | ▶ | die "$sub: \"$option\" means nothing for a $plot->{plot_type}, which has no axes" if $s->{kind} ne 'cartesian'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1683 | 2 | $s->{axis}{$axis}{lim} = [ axis_limits( $plot->{$option}, $option, $engine, $sub ) ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1684 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1685 | 180 | foreach my $option ( grep { defined $plot->{$_} } ( 'set_xscale', 'set_yscale', 'xscale', 'yscale' ) ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1686 | 2 | ▶ | my $axis = ( $option =~ m/x/ ) ? 'x' : 'y'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1687 | 2 | ▶ | die "$sub: \"$option\" means nothing for a $plot->{plot_type}, which has no axes" if $s->{kind} ne 'cartesian'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1688 | 2 | ▶ | $s->{axis}{$axis}{log} = ( axis_scale( $plot->{$option}, $option, $engine, $sub ) eq 'log' ) ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1689 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1690 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1691 | 201 | ▶ | sub panel_code { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
panel_code: called CC=64 SCAR=41.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1692 | # One subplot, its "add" graphs drawn first and the plot itself last, as | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1693 | # R code making a ggplot object. $native: 1 = style "native". | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1694 | 201 | my ( $plot, $where, $sub, $native ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1695 | 201 | my $s = new_panel($native); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1696 | 201 | ▶ | my @graphs = ( @{ $plot->{add} // [] }, $plot ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$plot->{"add"} // []
MC/DC100%1/1
$plot->{"add"} // []
$plot->{"add"}
Truth table100%2/2
A
A= $plot->{"add"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1697 | 201 | my %axis_kind; # under "native": axis => [ 'discrete' or 'continuous', the type that made it so ] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1698 | 201 | foreach my $graph (@graphs) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1699 | 207 | my $type = $graph->{plot_type}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1700 | 207 | ▶ | my $what = ( $graph == $plot ) ? $where : " for an \"add\" graph$where"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1701 | 207 | ▶ | if ( ( scalar @graphs > 1 ) && ( ( $type eq 'pie' ) || ( $type eq 'colored_table' ) ) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
scalar @graphs > 1 and $type eq "pie" || $type eq "colored_table"
$type eq "pie" or $type eq "colored_table"
MC/DC66%2/3
scalar @graphs > 1 and $type eq "pie" || $type eq "colored_table"
scalar @graphs > 1
$type eq "pie"
$type eq "colored_table"
Truth table75%3/4
A and B or C
A= scalar @graphs > 1B= $type eq "pie"C= $type eq "colored_table"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1702 | 1 | die "$sub: a $type$what has no axes to share, so the $engine engine draws it alone, with no \"add\" graph over it or under it"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1703 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1704 | 206 | $s->{discrete} = {}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1705 | 206 | $drawer{$type}->( $graph, $s, $what, $sub ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1706 | 187 | ▶ | next unless $native; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1707 | # A bar, box or violin stands on a discrete axis of its keys under | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1708 | # "native", and ggplot2 cannot put that axis under a graph whose | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1709 | # positions on it are numbers. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1710 | 87 | foreach my $axis ( 'x', 'y' ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1711 | 173 | ▶ | my $kind = $s->{discrete}{$axis} ? 'discrete' : 'continuous'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1712 | 173 | ▶ | if ( ( defined $axis_kind{$axis} ) && ( $axis_kind{$axis}[0] ne $kind ) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $axis_kind{$axis} and $axis_kind{$axis}[0] ne $kind
MC/DC50%1/2
defined $axis_kind{$axis} and $axis_kind{$axis}[0] ne $kind
defined $axis_kind{$axis}
$axis_kind{$axis}[0] ne $kind
Truth table66%2/3
A and B
A= defined $axis_kind{$axis}B= $axis_kind{$axis}[0] ne $kind
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1713 | 1 | die "$sub: in style \"native\" the $engine engine puts the keys of a bar, boxplot or violin on a discrete axis, so the $type$what cannot share the $axis axis with the $axis_kind{$axis}[1] it is drawn with; " | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1714 | . 'give "style => \'matplotlib\'", where every position is a number'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1715 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1716 | 172 | ▶ | $axis_kind{$axis} //= [ $kind, $type ]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$axis_kind{$axis} //= [$kind, $type]
MC/DC0%0/1
$axis_kind{$axis} //= [$kind, $type]
$axis_kind{$axis}
Truth table50%1/2
A
A= $axis_kind{$axis}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1717 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1718 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1719 | 180 | ▶ | push @{ $s->{layers} }, @{ $s->{after} // [] }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$s->{"after"} // []
MC/DC100%1/1
$s->{"after"} // []
$s->{"after"}
Truth table100%2/2
A
A= $s->{"after"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1720 | 180 | apply_axes_keywords( $_, $s, $where, $sub ) foreach @graphs; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1721 | 177 | my @code = ( 'ggplot2::ggplot()', @{ $s->{layers} } ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1722 | 177 | foreach my $axis ( 'x', 'y' ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1723 | 354 | my $a = $s->{axis}{$axis}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1724 | 354 | ▶ | next if $s->{kind} eq 'pie'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1725 | 338 | my @arg; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1726 | 338 | ▶ | if ( defined $a->{breaks} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1727 | 53 | push @arg, 'breaks = ' . r_nums( @{ $a->{breaks} } ), 'labels = ' . r_strs( @{ $a->{labels} } ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1728 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1729 | 338 | ▶ | if ( $a->{log} && $a->{reverse} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$a->{"log"} and $a->{"reverse"}
MC/DC0%0/2
$a->{"log"} and $a->{"reverse"}
$a->{"log"}
$a->{"reverse"}
Truth table66%2/3
A and B
A= $a->{"log"}B= $a->{"reverse"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1730 | 0 | die "$sub: the $engine engine cannot put the $axis axis$where on a log scale, since it runs downwards, as the rows of a $plot->{plot_type} do"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1731 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1732 | 338 | ▶ | push @arg, 'transform = "log10"' if $a->{log}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1733 | 338 | ▶ | push @arg, 'transform = "reverse"' if $a->{reverse}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1734 | 338 | ▶ | push @arg, 'expand = c(0, 0)' if $a->{expand0}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1735 | 338 | ▶ | push @arg, 'expand = ggplot2::expansion(mult = c(0, 0.05))' if $a->{floor}; # the bars of a log axis start at its bottom edge, as draw_bar explains | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1736 | 338 | ▶ | push @arg, 'position = "top"' if $a->{top}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1737 | 338 | ▶ | push @code, "ggplot2::scale_${axis}_continuous(" . join( ', ', @arg ) . ')' if @arg; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1738 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1739 | 177 | ▶ | my @lim = map { $s->{axis}{$_}{lim} ? ( "${_}lim = " . r_nums( @{ $s->{axis}{$_}{lim} } ) ) : () } 'x', 'y'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1740 | 177 | ▶ | if ( $s->{kind} eq 'pie' ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1741 | 8 | push @code, 'ggplot2::coord_equal(clip = "off")'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1742 | } elsif (@lim) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1743 | 1 | push @code, 'ggplot2::coord_cartesian(' . join( ', ', @lim ) . ')'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1744 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1745 | 177 | foreach my $legend ( [ 'colour', 'colour_d' ], [ 'fill', 'fill_d' ], [ 'shape', 'shape_d' ] ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1746 | 531 | my ( $aes, $list ) = @{$legend}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1747 | 531 | ▶ | next unless @{ $s->{$list} }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1748 | 53 | ▶ | if ( ( defined $s->{gradient} ) && ( $s->{gradient}{aes} eq $aes ) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $s->{"gradient"} and $s->{"gradient"}{"aes"} eq $aes
MC/DC0%0/2
defined $s->{"gradient"} and $s->{"gradient"}{"aes"} eq $aes
defined $s->{"gradient"}
$s->{"gradient"}{"aes"} eq $aes
Truth table66%2/3
A and B
A= defined $s->{"gradient"}B= $s->{"gradient"}{"aes"} eq $aes
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1749 | 0 | die "$sub: the $engine engine cannot give the $aes of the plot$where both a scale of values and a legend of names"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1750 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1751 | 53 | my @names = map { $_->[0] } @{ $s->{$list} }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1752 | 53 | my @given = grep { defined $s->{$list}[$_][1] } 0 .. $#names; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1753 | 53 | ▶ | my $guide = $s->{guide_none}{$aes} ? ', guide = "none"' : ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1754 | 53 | ▶ | if ( scalar @given == scalar @names ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1755 | 27 | ▶ | my $values = ( $aes eq 'shape' ) ? r_nums( map { $_->[1] } @{ $s->{$list} } ) : r_strs( map { $_->[1] } @{ $s->{$list} } ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1756 | 27 | push @code, "ggplot2::scale_${aes}_manual(name = NULL, values = stats::setNames($values, " . r_strs(@names) . '), breaks = ' . r_strs(@names) . "$guide)"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1757 | } elsif ( scalar @given == 0 ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1758 | # "native", with no color given: ggplot2's own scale, its levels in | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1759 | # the order the series were drawn | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1760 | 25 | ▶ | my $scale = ( $aes eq 'shape' ) ? 'scale_shape_discrete' : "scale_${aes}_hue"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1761 | 25 | push @code, "ggplot2::$scale(name = NULL, limits = " . r_strs(@names) . ', breaks = ' . r_strs(@names) . "$guide)"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1762 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1763 | # "native", with some colors given: ggplot2's hue for as many series | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1764 | # as there are, each given color put in its series' place | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1765 | push @code, "ggplot2::scale_${aes}_manual(name = NULL, values = replace(stats::setNames(scales::hue_pal()(" . scalar(@names) . '), ' . r_strs(@names) . '), ' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1766 | 1 | . r_nums( map { $_ + 1 } @given ) . ', ' . r_strs( map { $s->{$list}[$_][1] } @given ) . '), breaks = ' . r_strs(@names) . "$guide)"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1767 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1768 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1769 | 177 | ▶ | if ( my $c = $s->{category_fill} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1770 | 3 | ▶ | die "$sub: the $engine engine cannot give the plot$where two legends of fill colors" if @{ $s->{fill_d} }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1771 | 3 | ▶ | my $name = 'name = ' . ( defined $c->{title} ? r_str( $c->{title} ) : 'NULL' ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1772 | 3 | ▶ | my $tail = ', breaks = ' . r_strs( @{ $c->{breaks} } ) . ', labels = ' . r_strs( @{ $c->{labels} } ) . ', guide = ' . ( $c->{show} ? '"legend"' : '"none"' ) . ')'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1773 | 3 | ▶ | if ( grep { not defined } @{ $c->{colors} } ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1774 | 1 | push @code, "ggplot2::scale_fill_hue($name, limits = " . r_strs( @{ $c->{breaks} } ) . $tail; # "native": ggplot2's hue for each category | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1775 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1776 | 2 | push @code, "ggplot2::scale_fill_manual($name, values = stats::setNames(" . r_strs( @{ $c->{colors} } ) . ', ' . r_strs( @{ $c->{breaks} } ) . ')' . $tail; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1777 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1778 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1779 | 177 | ▶ | if ( my $g = $s->{gradient} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1780 | 36 | ▶ | if ( $s->{native} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1781 | # ggplot2's own gradient where no colormap was asked for, its own | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1782 | # title where none was given, and its own ends where none were | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1783 | 18 | my @arg; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1784 | 18 | ▶ | push @arg, 'name = ' . r_str( $g->{title} ) unless $g->{waiver}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1785 | 18 | ▶ | push @arg, 'colours = ' . r_strs( @{ $g->{colors} } ) if defined $g->{colors}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1786 | 18 | ▶ | push @arg, 'limits = ' . r_nums( $g->{lo}, $g->{hi} ), 'oob = scales::squish' if ( defined $g->{lo} ) || ( defined $g->{hi} ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $g->{"lo"} or defined $g->{"hi"}
MC/DC0%0/2
defined $g->{"lo"} or defined $g->{"hi"}
defined $g->{"lo"}
defined $g->{"hi"}
Truth table33%1/3
A or B
A= defined $g->{"lo"}B= defined $g->{"hi"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1787 | 18 | ▶ | push @arg, "na.value = $g->{na}" unless $g->{na} eq 'NA' && $g->{type} ne 'hist2d'; # NA leaves ggplot2's grey50, except for the bins hist2d leaves out | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$g->{"na"} eq "NA" and $g->{"type"} ne "hist2d"
MC/DC100%2/2
$g->{"na"} eq "NA" and $g->{"type"} ne "hist2d"
$g->{"na"} eq "NA"
$g->{"type"} ne "hist2d"
Truth table100%3/3
A and B
A= $g->{"na"} eq "NA"B= $g->{"type"} ne "hist2d"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1788 | 18 | ▶ | push @arg, 'transform = "log10"' if $g->{log}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1789 | 18 | ▶ | push @arg, 'guide = "none"' unless $g->{show}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1790 | 18 | ▶ | push @code, "ggplot2::scale_$g->{aes}_" . ( defined $g->{colors} ? 'gradientn' : 'gradient' ) . '(' . join( ', ', @arg ) . ')'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1791 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1792 | 18 | ▶ | push @code, "ggplot2::scale_$g->{aes}_gradientn(name = " . ( defined $g->{title} ? r_str( $g->{title} ) : 'NULL' ) . ', colours = ' . r_strs( @{ $g->{colors} } ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1793 | . ', limits = ' . r_nums( $g->{lo}, $g->{hi} ) . ', oob = scales::squish, na.value = ' . $g->{na} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1794 | 18 | ▶ | . ( $g->{log} ? ', transform = "log10"' : '' ) . ', guide = ' . ( $g->{show} ? 'ggplot2::guide_colourbar(theme = ggplot2::theme(legend.key.height = grid::unit(1, "null")))' : '"none"' ) . ')'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1795 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1796 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1797 | 177 | my $title = $s->{text}{title}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1798 | 177 | ▶ | my %label = map { $_ => $s->{text}{$_} // $s->{default_label}{$_} } 'x', 'y'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$s->{"text"}{$_} // $s->{"default_label"}{$_}
MC/DC100%2/2
$s->{"text"}{$_} // $s->{"default_label"}{$_}
$s->{"text"}{$_}
$s->{"default_label"}{$_}
Truth table100%3/3
A // B
A= $s->{"text"}{$_}B= $s->{"default_label"}{$_}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1799 | # NULL where nothing is given: ggplot2 would otherwise label an axis with | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1800 | # the name of the data.frame column, which is not the caller's text. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1801 | 177 | ▶ | push @code, 'ggplot2::labs(title = ' . ( defined $title ? r_str($title) : 'NULL' ) . join( '', map { ", $_ = " . ( defined $label{$_} ? r_str( $label{$_} ) : 'NULL' ) } 'x', 'y' ) . ')'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1802 | 177 | ▶ | if ( $s->{native} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1803 | # theme_grey, ggplot2's default, needs no call; a pie has no axes in | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1804 | # ggplot2 either, so it is drawn on theme_void, with room for its labels | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1805 | 85 | ▶ | push @code, 'ggplot2::theme_void()', 'ggplot2::theme(plot.margin = ggplot2::margin(20, 40, 20, 40))' if $s->{kind} eq 'pie'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1806 | 85 | return join( " +\n\t\t", @code ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1807 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1808 | 92 | push @code, 'ggplot2::theme_bw(base_size = 10)', | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1809 | 'ggplot2::theme(panel.grid = ggplot2::element_blank(), plot.title = ggplot2::element_text(hjust = 0.5, size = 12), axis.text = ggplot2::element_text(size = 10, colour = "#000000"), legend.text = ggplot2::element_text(size = 10))'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1810 | 92 | ▶ | push @code, 'ggplot2::theme(axis.ticks = ggplot2::element_blank(), panel.border = ggplot2::element_blank())' if $s->{kind} eq 'table'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1811 | 92 | ▶ | push @code, 'ggplot2::theme_void(base_size = 10)', 'ggplot2::theme(plot.title = ggplot2::element_text(hjust = 0.5, size = 12), plot.margin = ggplot2::margin(20, 40, 20, 40))' if $s->{kind} eq 'pie'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1812 | 92 | return join( " +\n\t\t", @code ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1813 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1814 | 201 | ▶ | sub device_of { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
device_of: called CC=5 SCAR=16.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1815 | # The R graphics device for an output file, by its extension, as R code | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1816 | # opening it at the given size. Every one of these devices reads a "%" in | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1817 | # its file name as the start of a page-number format, so each is doubled. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1818 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1819 | # An SVG is drawn into a temporary file, svg_drawn, which creator_code | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1820 | # writes to $file once, with its note. svg() wrote $file itself, and the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1821 | # note was then spliced into it, so that a viewer watching the file saw it | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1822 | # written twice: Eye of GNOME 45.3 stopped reloading it. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1823 | 201 | my ( $file, $w, $h, $sub ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1824 | 201 | my ($ext) = $file =~ m/\.([^.\/\\]+)\z/; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1825 | 201 | ▶ | $ext = lc( $ext // '' ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$ext // ""
MC/DC0%0/1
$ext // ""
$ext
Truth table50%1/2
A
A= $ext
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1826 | 201 | ( my $name = $file ) =~ s/%/%%/g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1827 | 201 | my $path = r_str($name); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1828 | 201 | ▶ | return qq{svg_drawn <- tempfile(fileext = ".svg")\n\tgrDevices::svg(gsub("%", "%%", svg_drawn, fixed = TRUE), width = $w, height = $h)} if $ext eq 'svg'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1829 | 3 | ▶ | return "grDevices::png($path, width = " . int( $w * $png_dpi + 0.5 ) . ', height = ' . int( $h * $png_dpi + 0.5 ) . ", res = $png_dpi)" if $ext eq 'png'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1830 | 1 | ▶ | return "grDevices::cairo_pdf($path, width = $w, height = $h)" if $ext eq 'pdf'; # cairo, as svg() is, so that text outside Latin-1 is drawn | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1831 | 1 | die "$sub: the $engine engine writes .svg, .png and .pdf files, so it cannot write \"$file\""; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1832 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1833 | 174 | ▶ | sub creator_code { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
creator_code: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1834 | # R that gives the SVG just drawn into svg_drawn (see device_of) the note | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1835 | # matplotlib gives its own, in <dc:title>, which no R device writes, and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1836 | # writes it to $file. The script reads the R and ggplot2 versions, the user | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1837 | # and the host as it runs, as the matplotlib engine's does, so that a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1838 | # script written with "execute => 0" and run later names whoever ran it. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1839 | # The file is spliced as raw bytes, so no encoding R might take it to be in | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1840 | # can change it. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1841 | 174 | my ( $file, $sub ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1842 | 174 | my ( $before, $after ) = svg_creator_metadata(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1843 | 174 | my $note = r_str( creator_note($sub) ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1844 | 174 | return <<"R"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1845 | local({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1846 | # Sys.info() is NULL where R cannot ask, and its user is "unknown" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1847 | # when R cannot find one; as matplotlib's script, the note says so. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1848 | field <- function(name, otherwise) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1849 | value <- Sys.info()[name] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1850 | if (is.null(value) || is.na(value) || !nzchar(value) || value == "unknown") otherwise else unname(value) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1851 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1852 | note <- paste0($note, ", R ", getRversion(), ", ggplot2 ", utils::packageVersion("ggplot2"), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1853 | ", by ", field("user", "unknown user"), " on ", field("nodename", "unknown host")) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1854 | note <- gsub("&", "&", note, fixed = TRUE) # first, or the escapes below would be escaped again | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1855 | note <- gsub("<", "<", note, fixed = TRUE) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1856 | note <- gsub(">", ">", note, fixed = TRUE) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1857 | bytes <- readBin(svg_drawn, "raw", file.size(svg_drawn)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1858 | at <- grepRaw(">", bytes, offset = grepRaw("<svg", bytes, fixed = TRUE), fixed = TRUE) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1859 | 174 | block <- charToRaw(enc2utf8(paste0(${\ r_str($before) }, note, ${\ r_str($after) }))) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1860 | 174 | writeBin(c(bytes[seq_len(at)], block, bytes[-seq_len(at)]), ${\ r_str($file) }) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1861 | unlink(svg_drawn) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1862 | }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1863 | R | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1864 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1865 | 201 | ▶ | sub render { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
render: called CC=21 SCAR=30.6
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1866 | # plt's figure as an R script, which is run unless "execute" is false. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1867 | # Returns the script's path. Its arguments are those of | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1868 | # Matplotlib::Simple::Matplotlib::render. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1869 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1870 | # Each figure is self-contained -- it loads ggplot2, opens its own device, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1871 | # draws inside local() and closes the device -- so that several calls can | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1872 | # write into one "fh", with "execute => 0" on all but the last, and the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1873 | # script run once draws them all. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1874 | 201 | my ( $args, $context ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1875 | 201 | my $sub = $context->{sub}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1876 | 201 | ▶ | my @plots = $context->{single_plot} ? ($args) : @{ $args->{plots} }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1877 | 201 | ▶ | if ( not defined $args->{output_file} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1878 | 0 | die "$sub: the $engine engine writes the figure to \"output_file\", which is not given"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1879 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1880 | 201 | my %scale; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1881 | 201 | foreach my $opt ( 'scale', 'scalex', 'scaley' ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1882 | 603 | ▶ | $scale{$opt} = $args->{$opt} // 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$args->{$opt} // 1
MC/DC100%1/1
$args->{$opt} // 1
$args->{$opt}
Truth table100%2/2
A
A= $args->{$opt}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1883 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1884 | 201 | my $w = $fig_w * $scale{scale} * $scale{scalex}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1885 | 201 | my $h = $fig_h * $scale{scale} * $scale{scaley}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1886 | 201 | my $device = device_of( $args->{output_file}, $w, $h, $sub ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1887 | 200 | my @panels; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1888 | 200 | foreach my $i ( 0 .. $#plots ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1889 | 201 | ▶ | push @panels, panel_code( $plots[$i], $context->{single_plot} ? '' : " at subplot $i", $sub, ( ( $context->{style} // 'native' ) eq 'native' ) ? 1 : 0 ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition50%1/2
$context->{"style"} // "native"
MC/DC0%0/1
$context->{"style"} // "native"
$context->{"style"}
Truth table50%1/2
A
A= $context->{"style"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1890 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1891 | 176 | ▶ | my $suptitle = defined $args->{suptitle} ? plain_text( last_value( $args->{suptitle} ), 'suptitle', $engine, $sub ) : undef; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1892 | 176 | my $fh = script_handle( $args, '.R', $sub ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1893 | # The note is a comment, so it is made ASCII by hand: r_str's escapes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1894 | # mean nothing there. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1895 | 0 | ( my $note = creator_note($sub) ) =~ s/([^\x20-\x7e])/sprintf '\\x{%x}', ord $1/ge; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1896 | 176 | say {$fh} "# Written by Matplotlib::Simple's $engine engine: $note"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1897 | 176 | say {$fh} 'if (!requireNamespace("ggplot2", quietly = TRUE) || utils::packageVersion("ggplot2") < ' . r_str($min_ggplot2) . ') {'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1898 | 176 | say {$fh} "\tmessage(\"Matplotlib::Simple: the $engine engine needs the R package ggplot2 $min_ggplot2 or newer, which this R cannot load; install it with install.packages(\\\"ggplot2\\\")\")"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1899 | 176 | say {$fh} "\tquit(status = 1)"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1900 | 176 | say {$fh} '}'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1901 | 176 | say {$fh} 'local({'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1902 | 176 | foreach my $i ( 0 .. $#panels ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1903 | 177 | say {$fh} "\tp$i <- $panels[$i]"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1904 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1905 | 176 | ▶ | my ( $nrows, $ncols ) = ( $args->{nrows} // 1, $args->{ncols} // 1 ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%2/4
$args->{"nrows"} // 1
$args->{"ncols"} // 1
MC/DC0%0/1
$args->{"nrows"} // 1
$args->{"nrows"}
MC/DC0%0/1
$args->{"ncols"} // 1
$args->{"ncols"}
Truth table50%1/2
A
A= $args->{"nrows"}
Truth table50%1/2
A
A= $args->{"ncols"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1906 | 176 | ▶ | my $top = defined $suptitle ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1907 | 176 | say {$fh} "\t$device"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1908 | 176 | say {$fh} "\tgrid::grid.newpage()"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1909 | 176 | ▶ | say {$fh} "\tgrid::pushViewport(grid::viewport(layout = grid::grid.layout($nrows + $top, $ncols, heights = grid::unit.c(" . ( $top ? 'grid::unit(2, "lines"), ' : '' ) . "grid::unit(rep(1, $nrows), \"null\")))))"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1910 | 176 | ▶ | say {$fh} "\tgrid::grid.text(" . r_str($suptitle) . ", gp = grid::gpar(fontsize = 12), vp = grid::viewport(layout.pos.row = 1, layout.pos.col = 1:$ncols))" if $top; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1911 | 176 | foreach my $i ( 0 .. $#panels ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1912 | 177 | my ( $row, $col ) = ( int( $i / $ncols ) + 1 + $top, $i % $ncols + 1 ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1913 | 177 | say {$fh} "\tprint(p$i, vp = grid::viewport(layout.pos.row = $row, layout.pos.col = $col))"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1914 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1915 | 176 | say {$fh} "\tinvisible(grDevices::dev.off())"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1916 | 176 | ▶ | print {$fh} creator_code( $args->{output_file}, $sub ) if lc( ( $args->{output_file} =~ m/\.([^.\/\\]+)\z/ )[0] // '' ) eq 'svg'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
($args->{"output_file"} =~ m(\.([^./\\]+)\z))[0] // ""
MC/DC0%0/1
($args->{"output_file"} =~ m(\.([^./\\]+)\z))[0] // ""
($args->{"output_file"} =~ m(\.([^./\\]+)\z))[0]
Truth table50%1/2
A
A= ($args->{"output_file"} =~ m(\.([^./\\]+)\z))[0]
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1917 | 176 | say {$fh} '})'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1918 | 176 | ▶ | if ( $args->{execute} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1919 | 1 | $fh->flush; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1920 | 1 | ▶ | run_script( [ program_from_env('MATPLOTLIB_SIMPLE_RSCRIPT') // 'Rscript', $fh->filename ] ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
program_from_env("MATPLOTLIB_SIMPLE_RSCRIPT") // "Rscript"
MC/DC0%0/1
program_from_env("MATPLOTLIB_SIMPLE_RSCRIPT") // "Rscript"
program_from_env("MATPLOTLIB_SIMPLE_RSCRIPT")
Truth table50%1/2
A
A= program_from_env("MATPLOTLIB_SIMPLE_RSCRIPT")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1921 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1922 | 175 | say 'will write ' . "\e[36;103m$args->{output_file}\e[0m"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1923 | 175 | return $fh->filename; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1924 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1925 | 5 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||