Basic graph with programmatic customization

Introduction

Minimum GAUSS version: 14.0

In this tutorial, we will draw a scatter plot of some data from an Excel® file containing measurements of nuclear site exposure and cancer mortality for a few counties in Washington state. You can download the Excel® file here.

The contents of the data file look like this:

  A B C
1 County Exposure Mortality
2 Umatilla 2.49 147.1
3 Morrow 2.57 130.1
4 Gilliam 3.41 129.9
5 Sherman 1.25 113.5
6 Wasco 1.62 137.5
7 HoodRiver 3.83 162.3
8 Portland 11.64 207.5
9 Columbia 6.41 177.9
10 Clatsop 8.34 210.3

Step 1: Load the data

We will start by loading the exposure and mortality columns into a GAUSS matrix. First save the hanford.xls file that you downloaded from the link above to your computer and set your GAUSS working directory to the folder in which you saved the Excel® file. Then load the data with the following command:

// Load all data from the variables
// 'Exposure' and 'Mortality'
data = loadd("hanford.xls", "Exposure + Mortality");
// Load all data from columns B2:C10
// into a GAUSS matrix named 'data'
data = xlsReadM("hanford.xls", "B2:C10");
// Load all data from columns B2:C10
// into a GAUSS matrix named 'data'
data = xlsReadM("hanford.xls", "B2:C10", 1, "");

Now data should be a 9x2 GAUSS matrix where the first column is the exposure data and the second column is the mortality data. Let's create two new GAUSS matrices, one named exposure and one named mortality, to make the code more readable.

// Create a new matrix 'exposure' containing
// all observations from the first column of 'data'
exposure = data[.,1];

// Create a new matrix 'mortality' containing
// all observations from the second column of 'data'
mortality = data[.,2];

Step 2: Create a basic scatter plot

We are wondering if our data shows a relationship between nuclear exposure and cancer mortality. A scatter plot is a good way to attempt to visualize this relationship. We can create a simple scatter plot of our two variables like this:

// Create scatter plot
plotScatter(exposure, mortality);

After running the code above you should get an image that looks similar to this:

Basic graph in GAUSS.

Step 3: Customize the plot

Our initial graph seems to show a relationship, which makes the data more interesting. Next, we need to customize this graph a bit so that when we share this graph with others, they can understand what it represents.

Our first graph used the default settings for scatter plots. You can view and modify those graphically by selecting Tools > Preferences > Graphics from the main GAUSS menu. It is often desirable, however, to customize your graph as part of a program so that it can be easily replicated. That is our focus for this tutorial.

The plotControl structure

A plotControl structure holds all of the settings for creating a GAUSS graph. To use a plotControl structure we need to:

  1. Declare a variable to be an instance of a plotControl structure.
  2. Fill this plotControl structure with the default values, using the function plotGetDefaults.
  3. Apply our desired changes with the plotSet functions.
  4. Pass the plotControl structure to the plotting function.
/*
** Step 1:
** Declare 'myPlot' to be an instance of a 'plotControl' structure
*/

struct plotControl myPlot;

/*
** Step 2:
** Fill 'myPlot' with the default values for a scatter plot.
** Valid options include: bar, box, contour, hist, scatter,
**                        surface and xy.
*/

myPlot = plotGetDefaults("scatter");

/*
** Step 3:
** Apply our desired plot attributes with the 'plotSet' functions
*/

// Set the title and title font
plotSetTitle(&myPlot, "Nuclear Exposure vs Cancer Mortality", "Arial", 16);

// Set the label for the X-axis
plotSetXLabel(&myPlot, "Exposure");

// Set the label for the Y-axis
plotSetYLabel(&myPlot, "Mortality");

/*
** Step 4:
** Pass the 'plotControl' structure to 'plotScatter'
*/

// Draw scatter plot with settings from 'myPlot'
plotScatter(myPlot, exposure, mortality);

After the code above, our updated plot should look more like this:

Scatter plot in GAUSS.

The plotSet functions

The plotSet functions modify the settings inside of the plotControl structure that you are using.

The first argument to each of the plotSet functions is going to be a pointer to your plotControl structure. This allows the internal elements of the plotControl structure to be modified inside of the plotSet function.

While a pointer to a structure may sound difficult or arcane‐‐all you need to know is that you need to place an ampersand & in front of the structure name. Fortunately, the function tooltips will remind you.

blank

The remaining inputs should be more straightforward and will be documented in the Command Reference entry for each function. Let's use plotSetTitle as an example. It takes the following inputs:


&myPlot
A pointer to a plotControl structure.
title
String to display as the graph title.
font
Optional input. String, the name of the font to use for the title.
fontSize
Optional input. Scalar, the size of the font in points.
fontColor
Optional input. String, the name of font color.

Note that the inputs inside of the square brackets [] are optional inputs.

Conclusion

In this tutorial, we learned

  1. How to draw a basic scatter plot, passing in only our X and Y vectors to plotScatter.
  2. How to programmatically customize a scatter plot by using a plotControl structure.

Have a Specific Question?

Get a real answer from a real person

Need Support?

Get help from our friendly experts.