WeBWorK Problems

How do I use ggplot2 or ggformula for figures?

How do I use ggplot2 or ggformula for figures?

by Randall Pruim -
Number of replies: 0
I'm posting this mainly to save time for others who might be trying to figure this out. The main issue is that an explicit print() is required for ggplot2 graphics. (LIkely for lattice as well.)

Here's a not-quite-minimal example:

DOCUMENT(); loadMacros("PGstandard.pl", "MathObjects.pl", "RserveClient.pl", "PGML.pl"); Context("Numeric"); # indicate the Context (this is MathObjects) #--------------------------------------- # problem set-up section #--------------------------------------- $shape = random(3,7,1); $rate = random (6,10,2); rserve_eval("library(ggformula)"); $img = rserve_start_plot('png'); # use white background, larger fonts rserve_eval("theme_set(theme_bw(base_size = 30))"); # note use of print() here rserve_eval("print(gf_dist('gamma', shape = $shape, rate = $rate))"); $image_path = rserve_finish_plot($img); #---------------------------------------- # text section #--------------------------------------- TEXT(beginproblem()); BEGIN_PGML >> [@ image($image_path, width=>400, height=>200) @]* << END_PGML ENDDOCUMENT();