From f398a36a8b210fcc81ae42539c1f931f026d9db0 Mon Sep 17 00:00:00 2001 From: Peter Staab Date: Wed, 3 Jun 2026 17:28:22 -0400 Subject: [PATCH] Creation of sample problems using StatisticalPlots macro. --- .../Statistics/BarGraphStatPlot.pg | 105 ++++++++++++++ .../sample-problems/Statistics/DonutPlot.pg | 82 +++++++++++ .../sample-problems/Statistics/Histogram.pg | 96 +++++++++++++ .../Statistics/LinearRegression.pg | 2 +- .../Statistics/MultiBoxPlot.pg | 120 ++++++++++++++++ .../sample-problems/Statistics/PieChart.pg | 78 +++++++++++ .../sample-problems/Statistics/ScatterPlot.pg | 5 +- .../Statistics/ScatterPlotStatPlot.pg | 95 +++++++++++++ .../sample-problems/Statistics/boxplot.pg | 132 ++++++++++++++++++ 9 files changed, 713 insertions(+), 2 deletions(-) create mode 100644 tutorial/sample-problems/Statistics/BarGraphStatPlot.pg create mode 100644 tutorial/sample-problems/Statistics/DonutPlot.pg create mode 100644 tutorial/sample-problems/Statistics/Histogram.pg create mode 100644 tutorial/sample-problems/Statistics/MultiBoxPlot.pg create mode 100644 tutorial/sample-problems/Statistics/PieChart.pg create mode 100644 tutorial/sample-problems/Statistics/ScatterPlotStatPlot.pg create mode 100644 tutorial/sample-problems/Statistics/boxplot.pg diff --git a/tutorial/sample-problems/Statistics/BarGraphStatPlot.pg b/tutorial/sample-problems/Statistics/BarGraphStatPlot.pg new file mode 100644 index 0000000000..2212e5b849 --- /dev/null +++ b/tutorial/sample-problems/Statistics/BarGraphStatPlot.pg @@ -0,0 +1,105 @@ +## DESCRIPTION +## Find the mean and standard deviation of a list of numbers. +## ENDDESCRIPTION +## DBsubject(WeBWorK) +## DBchapter(WeBWorK tutorial) +## DBsection(WeBWorK tutorial) +## Institution(Fitchburg State University) +## Author(Peter Staab) +## Date(06/25/2026) +## KEYWORDS('statistics', 'bar graph') + +#:% name = Bar Graph (Stat Plot) +#:% subject = [statistics] +#:% type = sample +#:% categories = [graph, statistics] + +#:% section = preamble +#: This is a updated version of PROBLINK('BarGraph.pg') to use the +#: PODLINK('StatisticalPlots.pl') macro, which provides some convenience methods +#: for creating Statistical plots. This also uses the +#: PODLINK('contextPercent.pl') to provide the Percent context. +DOCUMENT(); + +loadMacros( + 'PGstandard.pl', 'PGML.pl', + 'StatisticalPlots.pl', 'contextPercent.pl', + 'PGcourse.pl' +); + +#:% section = setup +#: The 'Percent' context allow students to enter percents. +#: +#: To create a graph, start with a `StatPlot`. The `min` and `max` options in +#: the two directions give the bounding box of the plot. The `tick_delta` for +#: each direction gives the distance between tick marks and the `minor` option is +#: the number of minor ticks between each tick. +#: +#: Use `xtick_labels` to customize the tick labels and the resulting hashref +#: is a mapping from the old tick labels to the new labels. +#: +#: The bars are added with the `add_barplot` method with the xdata and ydata. +#: The `bar_width` give a relative width of the bar. If this option is 1 +#: there is no gap between bars. +#: See PODLINK('StatisticalPlots.pl') for other options to this method. +#: +#: Make sure that an alternate text is added to all graphs for accessibility. Be +#: detailed. + +Context('Percent'); + +@grades = map { random(1, 5) } (0 .. 3); + +$statPlot = StatPlot( + xmin => 0, + xmax => 5, + xtick_delta => 1, + xminor => 0, + ymin => 0, + ymax => 6, + yminor => 0, + ytick_delta => 1, + xtick_labels => { 1 => 'A', 2 => 'B', 3 => 'C', 4 => 'D' }, + show_grid => 0, + rounded_corners => 1 +); + +$statPlot->add_barplot( + [ 1 .. 4 ], ~~@grades, + fill_colors => ['Red,', 'Salmon', 'LightYellow', 'LightGreen'], + stroke_width => 1, + bar_width => 0.9 +); + +$alt_text = + "A bar graph with vertical bars. The height of the bar labelled A " + . "is $grades[0]. The height of the bar labelled B is $grades[1]. " + . " The height of the bar labelled C is $grades[2] and finally the " + . "bar labelled D is $grades[3]."; + +# This code adds up all the numbers in the @grades array. +$num_students = 0; +$num_students += $_ for @grades; + +$perA = Real($grades[0] / $num_students); + +#:% section = statement +#: In this case, we plot the bar graph and ask a question. +#: Since the context is `Percent`, the students can +#: answer the question with either a fraction, decimal or percent. +#: If only percent answers is desired, see the flags for the Percent context +#: in the macro. +BEGIN_PGML +The following is a distribution of grades on a Statistics quiz. +>>[! [$alt_text] !]{$statPlot}{400}<< + +What percentage of students earned an A? [_]{$perA} +END_PGML + +#:% section = solution +BEGIN_PGML_SOLUTION +The total number of students that took the quiz is [$total] so the percentage is +the number of A students or [`[$grades[0]]/[$num_students] = [$perA] `] +END_PGML_SOLUTION + +ENDDOCUMENT(); diff --git a/tutorial/sample-problems/Statistics/DonutPlot.pg b/tutorial/sample-problems/Statistics/DonutPlot.pg new file mode 100644 index 0000000000..e0a0dc66da --- /dev/null +++ b/tutorial/sample-problems/Statistics/DonutPlot.pg @@ -0,0 +1,82 @@ +## DESCRIPTION +## Produce a donut plot. +## ENDDESCRIPTION +## DBsubject(WeBWorK) +## DBchapter(WeBWorK tutorial) +## DBsection(WeBWorK tutorial) +## Institution(Fitchburg State University) +## Author(Peter Staab) +## Date(06/25/2026) +## KEYWORDS('statistics', 'bar graph') + +#:% name = Donut Plot +#:% subject = [statistics] +#:% type = sample +#:% categories = [graph, statistics] +#:% see_also = [PieChart.pg] + +#:% section = preamble +#: This uses the `add_piechart` method in the PODLINK('StatisticalPlots.pl') +#: macro with the option to make it a donut plot. +#: A pie chart version of this is available at PROBLINK('PieChart.pg'). +DOCUMENT(); + +loadMacros("PGstandard.pl", 'PGML.pl', 'StatisticalPlots.pl', 'PGcourse.pl'); + +#:% section = setup +#: In this example, the size of each wedge in the donut plot is given as an +#: array. +#: +#: The StatPlot is created with the axes and grid turned off. The +#: `add_piechart` method takes the number values in the `@values` array. The +#: values must be passed in as an arrayref (which is why the `~~` is needed). +#: +#: For the options, the `labels` also need to be passed in as an arrayref. +#: The `radius` and `inner_radius` gives the outer and inner radii of the donut. +#: The `angle_offset` is useful to get the labels in the proper position. +#: +#: The default colors come from the `rainbow` palette, and other options +#: exist. +#: +#: A detailed alt_text should accompany all plots/graphs. + +@values = (1, 5, 8, 2, 4); +@animals = ('Anteater', 'Bobcat', 'Camel', 'Dingo', 'Elephant'); + +$stat_plot = StatPlot(xvisible => 0, yvisible => 0, show_grid => 0); +$stat_plot->add_piechart( + ~~@values, + labels => ~~@animals, + radius => 3.25, + inner_radius => 2.5, + angle_offset => 30, + stroke_width => 1, + rounded_corners => 1 +); + +$total = 0; +$total += $_ for (@values); +$alt_text = 'A pie chart of animals. '; +for (0 .. 4) { + $alt_text .= + "The size of the $animals[$_] wedge is about " + . Round(100 * $values[$_] / $total, 1) + . '% of the total. '; +} + +#:% section = statement +#: In this case, only the plot is provided with the alt text. +BEGIN_PGML + +>> Donut Plot of the number of animals at the local zoo << + +>>[! [$alt_text] !]{$stat_plot}{400}<< + +END_PGML + +#:% section = solution +BEGIN_PGML_SOLUTION +Place a detailed solution here. +END_PGML_SOLUTION + +ENDDOCUMENT(); diff --git a/tutorial/sample-problems/Statistics/Histogram.pg b/tutorial/sample-problems/Statistics/Histogram.pg new file mode 100644 index 0000000000..3baa036a5f --- /dev/null +++ b/tutorial/sample-problems/Statistics/Histogram.pg @@ -0,0 +1,96 @@ +## DESCRIPTION +## Find the mean and standard deviation of a list of numbers. +## ENDDESCRIPTION +## DBsubject(WeBWorK) +## DBchapter(WeBWorK tutorial) +## DBsection(WeBWorK tutorial) +## Institution(Missouri Western) +## Author(Glenn Rice) +## Date(06/25/2026) +## KEYWORDS('statistics', 'histogram') + +#:% name = Histogram +#:% subject = [statistics] +#:% type = sample +#:% categories = [graph, statistics] + +#:% section = preamble +#: This produces a histogram using the PODLINK('StatisticalPlots.pl') macro, which +#: provides some convenience methods for creating Statistical plots. This also uses +#: PODLINK('PGstatisticsmacros.pl') to generate random values from the Normal +#: distribution. +DOCUMENT(); + +loadMacros( + "PGstandard.pl", "PGML.pl", + 'StatisticalPlots.pl', 'PGstatisticsmacros.pl', + "PGcourse.pl" +); + +#:% section = setup +#: +#: The `urand` function produces normally distributed random numbers. This will +#: generate an array of 50 with a mean of 30 and standard deviation of 9. See +#: PODLINK('PGstatisticsmacros.pl') for more information. +#: +#: To get nice bin sizes (in this case of width 5), the min and max are found and then +#: are passed into the `add_histogram` method. The vertical maximum is calculated from the +#: data and then the ymax is set with the `->axes->yaxis` method. +#: +#: See PODLINK('StatisticalPlots.pl') for other options to this method. +#: +#: Make sure that an alternate text is added to all graphs for accessibility. Be +#: detailed. + +$dx = 5; # bin size +@data = urand(30, 9, 50, 6); +$min = floor(min(@data) / $dx) * $dx; +$max = ceil(max(@data) / $dx) * $dx; + +$histogramVertical = StatPlot( + xmin => $min - $dx/2, + xmax => $max + $dx/2, + xtick_distance => $dx, + xminor => 0, + xmajor => 0, + xlabel => '', + xlocation => 'bottom', + ymin => 0, + ytick_distance => 2, + yminor => 1, + ylabel => '', + ylocation => 'left', + rounded_corners => 1 +); +(undef, $vFrequencies) = $histogramVertical->add_histogram( + ~~@data, + min => $min, + max => $max, + bins => ($max - $min) / $dx, + fill_color => 'blue', + fill_opacity => 0.4, + stroke_width => 1 +); +$histogramVertical->axes->yaxis(max => max(@$vFrequencies) + 1); + +# use the $vFrequencies to generate the alt_text. +$alt_text = 'A histogram with ' . scalar(@$vFrequencies) . ' bins. '; +for $i (1 .. scalar(@$vFrequencies)) { + $alt_text .= "Bin $i is from " . ($min + ($i-1)*$dx) . ' to ' . ($min + $i*$dx) . + ' and the height is ' . $vFrequencies->[$i-1] . '. '; +} + +#:% section = statement +#: In this case, only the histogram is plotted. Make sure to add the alt text. +BEGIN_PGML + +>>[! [$alt_text] !]{$histogramVertical}{400}<< + +END_PGML + +#:% section = solution +BEGIN_PGML_SOLUTION +If a question was asked, provide a solution. +END_PGML_SOLUTION + +ENDDOCUMENT(); diff --git a/tutorial/sample-problems/Statistics/LinearRegression.pg b/tutorial/sample-problems/Statistics/LinearRegression.pg index 7fc05eef6c..7e95e8f14e 100644 --- a/tutorial/sample-problems/Statistics/LinearRegression.pg +++ b/tutorial/sample-problems/Statistics/LinearRegression.pg @@ -11,7 +11,7 @@ #:% name = Linear Regression #:% subject = [statistics] #:% type = sample -#:% categories = [graph, statistics] +#:% categories = [statistics] #:% section = preamble #: The PODLINK('PGstatisticsmacros.pl') macro provides the `sample_correlation` diff --git a/tutorial/sample-problems/Statistics/MultiBoxPlot.pg b/tutorial/sample-problems/Statistics/MultiBoxPlot.pg new file mode 100644 index 0000000000..85e0b6a5e8 --- /dev/null +++ b/tutorial/sample-problems/Statistics/MultiBoxPlot.pg @@ -0,0 +1,120 @@ +## DESCRIPTION +## Produce multiple box plots. +## ENDDESCRIPTION +## DBsubject(WeBWorK) +## DBchapter(WeBWorK tutorial) +## DBsection(WeBWorK tutorial) +## Institution(Fitchburg State University) +## Author(Peter Staab) +## Date(06/25/2026) +## KEYWORDS('statistics', 'box plot') + +#:% name = Boxplot (Multiple) +#:% subject = [statistics] +#:% type = sample +#:% categories = [graph, statistics] + +#:% section = preamble +#: The PODLINK('StatisticalPlots.pl') macro is included. This provides some +#: convenience methods for creating Statistical plots. +DOCUMENT(); + +loadMacros( + 'PGstandard.pl', 'PGML.pl', + 'StatisticalPlots.pl', 'PGcourse.pl' +); + +#:% section = setup +#: To create a graph, start with a `StatPlot`. The `min` and `max` options in +#: the two directions give the bounding box of the plot. The `tick_delta` for +#: each direction gives the distance between tick marks and the `minor` option is +#: the number of minor ticks between each tick. +#: +#: The three box plots are created by defining the `$params`, an array ref of the +#: needed parameters. For each set of parameters, the box plot is created +#: with the `add_boxplot`. The remaining arguments are options to the box plot. +#: Note that in this example, we have selected three different colors for the +#: box plots and each is given a whisker cap. See PODLINK('StatisticalPlots.pl') +#: for other options to this method. +#: +#: Make sure that an alternate text is added to all graphs for accessibility. Be +#: detailed. + +$params = [ + { + min => 700, + q1 => 1100, + median => 1300, + q3 => 1700, + max => 2150 + + }, + { + min => 400, + q1 => 850, + median => 1150, + q3 => 1350, + max => 2000 + }, + { + min => 600, + q1 => 950, + median => 1175, + q3 => 1250, + max => 1600 + } +]; + +$statPlot = StatPlot( + ymin => -2, + ymax => 8, + ytick_delta => 2, + yminor => 0, + yvisible => 1, + ytick_labels => { 6=> 'Fri.', 4=> 'Sat.', 2 => 'Sun.'}, + show_grid => 0, + xmin => -400, + xmax => 2600, + xtick_delta => 500 +); +@fill_colors = ('LightBlue', 'LightGreen', 'Magenta'); +@box_centers = (2, 4, 6); +for (0 .. $#box_centers) { + $statPlot->add_boxplot( + $params->[$_], + orientation => 'horizontal', + box_center => $box_centers[$_], + box_width => 1, + fill_color => $fill_colors[$_], + whisker_cap => 1 + ); +} +$alt_text = 'Three horizontal box plots. ' . + "The topmost one is labeled 'Fri.' with left most whisker $params->[0]{min} " . + "the left side of the box is at $params->[0]{q1} and vertical line at " . + "$params->[0]{median}, right side of the box at $params->[0]{q3} " . + "and rightmost whisker at $params->[0]{max}." . + "The box plot in the middle is labelled 'Sat.' with left most whisker " . + "$params->[1]{min} the left side of the box is at $params->[1]{q1} and " . + "vertical line at $params->[1]{median}, right side of the box at " . + " $params->[1]{q3} and rightmost whisker at $params->[1]{max}." . + "The box plot toward the bottom is labelled 'Sun.' with left most whisker " . + "$params->[2]{min} the left side of the box is at $params->[2]{q1} and " . + "vertical line at $params->[2]{median}, right side of the box at " . + " $params->[2]{q3} and rightmost whisker at $params->[2]{max}." ; + +#:% section = statement +#: In this case, only the plot is shown. Generally here is where questions are +#: asked. Also note that the alt test is provided to the plot. +BEGIN_PGML + +>>[! [$alt_text] !]{$statPlot}{400}<< + +END_PGML + +#:% section = solution +BEGIN_PGML_SOLUTION +Place a detailed solution here. +END_PGML_SOLUTION + +ENDDOCUMENT(); diff --git a/tutorial/sample-problems/Statistics/PieChart.pg b/tutorial/sample-problems/Statistics/PieChart.pg new file mode 100644 index 0000000000..9e542434f5 --- /dev/null +++ b/tutorial/sample-problems/Statistics/PieChart.pg @@ -0,0 +1,78 @@ +## DESCRIPTION +## Produce a pie chart. +## ENDDESCRIPTION +## DBsubject(WeBWorK) +## DBchapter(WeBWorK tutorial) +## DBsection(WeBWorK tutorial) +## Institution(Fitchburg State University) +## Author(Peter Staab) +## Date(06/25/2026) +## KEYWORDS('statistics', 'bar graph') + +#:% name = Pie Chart +#:% subject = [statistics] +#:% type = sample +#:% categories = [graph, statistics] +#:% see_also = [DonutPlot.pg] + +#:% section = preamble +#: This uses the `add_piechart` method in the PODLINK('StatisticalPlots.pl') +#: macro. A donut plot version of this is available at PROBLINK('DonutPlot.pg'). +DOCUMENT(); + +loadMacros("PGstandard.pl", 'PGML.pl', 'StatisticalPlots.pl', 'PGcourse.pl'); + +#:% section = setup +#: In this example, the size of each wedge in the pie chart is given as an +#: array. +#: +#: The StatPlot is created with the axes and grid turned off. The +#: `add_piechart` method takes the values and number of options. In this +#: case, the labels are included as an arrayref (which is why the `~~` is needed) +#: and are placed outside the piechart. The `radius` gives the size of the +#: circle and the `angle_offset` is useful to get the labels in the proper position. +#: +#: The default colors come from the `rainbow` palette, and other options +#: exist. +#: +#: A detailed alt_text should accompany all plots/graphs. + +@values = (1, 5, 8, 2, 4); +@animals = ('Anteater', 'Bobcat', 'Camel', 'Dingo', 'Elephant'); + +$stat_plot = StatPlot(xvisible => 0, yvisible => 0, show_grid => 0); +$stat_plot->add_piechart( + ~~@values, + labels => ~~@animals, + radius => 3.25, + angle_offset => 30, + stroke_width => 1, + rounded_corners => 1 +); + +$total = 0; +$total += $_ for (@values); +$alt_text = 'A pie chart of animals. '; +for (0 .. 4) { + $alt_text .= + "The size of the $animals[$_] wedge is about " + . Round(100 * $values[$_] / $total, 1) + . '% of the total. '; +} + +#:% section = statement +#: In this case, we just plot the box plots. +BEGIN_PGML + +>> Pie Chart of the number of animals at the local zoo << + +>>[! [$alt_text] !]{$stat_plot}{400}<< + +END_PGML + +#:% section = solution +BEGIN_PGML_SOLUTION +Place a detailed solution here. +END_PGML_SOLUTION + +ENDDOCUMENT(); diff --git a/tutorial/sample-problems/Statistics/ScatterPlot.pg b/tutorial/sample-problems/Statistics/ScatterPlot.pg index 737a9b2ae0..b36c7cf324 100644 --- a/tutorial/sample-problems/Statistics/ScatterPlot.pg +++ b/tutorial/sample-problems/Statistics/ScatterPlot.pg @@ -15,9 +15,12 @@ #:% type = Sample #:% subject = [statistics, plot] #:% categories = [plots] +#:% see_also = [ScatterPlotStatPlot.pg] #:% section = preamble -#: Make sure that PODLINK('plots.pl') is loaded. +#: This produces a scatter plot with PODLINK('plots.pl'). A version of this +#: with the PODLINK('StatisticalPlots.pl') is available in +#: PROBLINK('ScatterPlotStatPlot.pg'). DOCUMENT(); loadMacros( diff --git a/tutorial/sample-problems/Statistics/ScatterPlotStatPlot.pg b/tutorial/sample-problems/Statistics/ScatterPlotStatPlot.pg new file mode 100644 index 0000000000..fd97d1a225 --- /dev/null +++ b/tutorial/sample-problems/Statistics/ScatterPlotStatPlot.pg @@ -0,0 +1,95 @@ +## DESCRIPTION +## Scatter Plot with best-fit line using StatPlots +## ENDDESCRIPTION + +## DBsubject(WeBWorK) +## DBchapter(WeBWorK tutorial) +## DBsection(PGML tutorial 2015) +## Date(06/01/2026) +## Institution(Boise State University) +## Author(Jaimos Skriletz) +## MO(1) +## KEYWORDS('riemann sum', 'plot') + +#:% name = Scatter Plot (Statistical Plots) +#:% type = Sample +#:% subject = [statistics, plot] +#:% categories = [plots] + +#:% section = preamble +#: Make sure that PODLINK('StatisticalPlots.pl') is loaded. This is a version +#: of the problem in LINK('ScatterPlot.pg') that uses the scatter plot feature +#: in StatisticalPlots.pl. +DOCUMENT(); + +loadMacros( + 'PGstandard.pl', 'PGML.pl', + 'StatisticalPlots.pl', 'PGstatisticsmacros.pl', + 'PGcourse.pl' +); + +#:% section = setup +#: For this problem, we have a data set stored as an array of array +#: references. First we find the best-fit line using the `linear_regression` +#: function of PODLINK('PGstatisticsmacros.pl'). Note: the x and y data +#: needs to be array references and the 2nd and 3rd lines extract the +#: components. +#: +#: For the plot, first set up the `Plot` with a plotting window that will +#: capture the data. The `add_dataset` method adds the data to the plot +#: with circles and color blue. +#: +#: The line is added to the dataset with the `add_function` method. The 3rd +#: and 4th arguments are the xmin and xmax of the domain. + +@data = ([ 5, 40 ], [ 7, 120 ], [ 12, 180 ], [ 16, 210 ], [ 20, 240 ]); + +$x = [ map { $_->[0] } @data ]; +$y = [ map { $_->[1] } @data ]; +($m, $b) = linear_regression($x, $y); + +$plot = Plot( + xmin => 0, + xmax => 24, + xtick_delta => 4, + ymin => 0, + ymax => 300, + aria_label => 'Linear Regression' +); +$plot->add_scatterplot( + @data, + marks => 'circle', + color => 'blue' +); +$plot->add_function( + "$m x + $b", + 'x', + 1, + 23, + color => 'black', + width => 1, +); + +#:% section = statement +#: The data is formatted by putting the array data in a nice format using both +#: join (which joins an array separated by ', ') and map (which creates a new +#: array that formats as (x1, y1)). The [@ @] is used to run perl. +#: +#: The `[! !]{}{}` is a PGML format for inserting an image. +BEGIN_PGML +A scatter plot of the data: + +[``` [@ join(', ', map { "($_->[0], $_->[1])" } @data) @] ```] + +together with the best-fit line is given by the following plot. + +>> [! Scatter Plot with best fit line !]{$plot}{500} << + +END_PGML + +#:% section = solution +BEGIN_PGML_SOLUTION +Solution explanation goes here. +END_PGML_SOLUTION + +ENDDOCUMENT(); diff --git a/tutorial/sample-problems/Statistics/boxplot.pg b/tutorial/sample-problems/Statistics/boxplot.pg new file mode 100644 index 0000000000..f59ebae369 --- /dev/null +++ b/tutorial/sample-problems/Statistics/boxplot.pg @@ -0,0 +1,132 @@ +## DESCRIPTION +## Produce a box plot from both a set of numbers and from the quartiles. +## ENDDESCRIPTION +## DBsubject(WeBWorK) +## DBchapter(WeBWorK tutorial) +## DBsection(WeBWorK tutorial) +## Institution(Fitchburg State University) +## Author(Peter Staab) +## Date(06/25/2026) +## KEYWORDS('statistics', 'box plot') + +#:% name = Boxplot +#:% subject = [statistics] +#:% type = sample +#:% categories = [graph, statistics] + +#:% section = preamble +#: Use the PODLINK('StatisticalPlots.pl') macro, which provides some convenience methods +#: for creating Statistical plots. This also uses the +#: PODLINK('PGstatisticsmacros.pl') for access to random normally-distributed numbers. +DOCUMENT(); + +loadMacros( + 'PGstandard.pl', 'PGML.pl', + 'StatisticalPlots.pl', 'PGstatisticsmacros.pl', + 'PGcourse.pl' +); + +#:% section = setup +#: +#: The `urand` function is used for normally distributed random numbers. This example +#: generates an array of length 75 with mean 100 and standard deviation 25. +# +#: To create a graph, start with a `StatPlot`. The `min` and `max` options in +#: the two directions give the bounding box of the plot. The `tick_delta` for +#: each direction gives the distance between tick marks and the `minor` option is +#: the number of minor ticks between each tick. +#: +#: The box plot is created with the `add_boxplot` method in which the data is the +#: first argument (it is an array ref, which is why the `~~` is needed). The +#: remaining arguments are options to the box plot. See PODLINK('StatisticalPlots.pl') +#: for other options to this method. +#: +#: The second box plot shows that the min/max, median and quartiles can be passed in +#: as an alternative to an arrayref of numbers. + +#: Make sure that an alternate text is added to all graphs for accessibility. Be +#: detailed. + +@data = urand(100, 25, 75, 6); + +$boxplotA = StatPlot( + xmin => 0, + xmax => 200, + xtick_delta => 25, + show_grid => 0, + ymin => -5, + ymax => 25, + yvisible => 0, + aspect_ratio => 4, + rounded_corners => 1 +); + +$boxplotA->add_boxplot(~~@data, fill_color => 'lightblue', stroke_width => 1); + +# the five point summary is needed for the alt text: + +@five_point = five_point_summary(@data); +$minA = Round($five_point[0]); +$q1A = Round($five_point[1]); +$medA = Round($five_point[2]); +$q3A = Round($five_point[3]); +$maxA = Round($five_point[4]); + +$alt_textA = + "A horizontal box plot with the box between $q1A and $q3A and vertical " + . "line at $medA. The is line from the box to the left from $q1A to $minA and a " + . "line to the right from $q3A to $maxA."; + +# Alternatively, we can set these value for the plot. + +$boxplotB = StatPlot( + xmin => 100, + xmax => 400, + ymin => -5, + ymax => 25, + xtick_delta => 50, + yvisible => 0, + show_grid => 0, + aspect_ratio => 4, + rounded_corners => 1 +); + +$minB = random(150, 175, 5); +$q1B = random(180, 225, 5); +$medB = random(250, 275, 5); +$q3B = random(280, 320, 10); +$maxB = random(325, 350, 5); + +$boxplotB->add_boxplot({ + min => $minB, + q1 => $q1B, + median => $medB, + q3 => $q3B, + max => $maxB, + outliers => [ 115, 130, 375 ] +}); + +$alt_textB = + "A horizontal box plot with the box betwen $q1B and $q3B and vertical " + . "line at $medA. The is line from the box to the left from $q1B to $minB and a " + . "line to the right from $q3B to $maxB. There are also 3 small plus signs at " + . "115, 130 and 375."; + +#:% section = statement +#: For this problem, only the plots are shown. Generally the questions would go +#: here. Again, add a detailed alt text for each plot. +BEGIN_PGML + +>>[! [$alt_textA] !]{$boxplotA}{400}<< + +>>[! [$alt_textB] !]{$boxplotB}{400}<< + + +END_PGML + +#:% section = solution +BEGIN_PGML_SOLUTION +Add solution here. +END_PGML_SOLUTION + +ENDDOCUMENT();