Introduction
In knowledge evaluation, a well-designed graph may help make clear your insights however a poorly annotated one can confuse and distract your viewers. That’s why correct annotation, together with legends, is crucial to creating efficient graphs.
Legends play an important function in making graphs extra readable by distinguishing between totally different teams, classes, or knowledge sequence. A well-placed legend helps be sure that your message comes throughout clearly.
On this weblog, we’ll stroll by the best way to add and customise legends in GAUSS graphics, protecting:
Robotically Including Legends with the by Key phrase
When utilizing a system string with the by key phrase, GAUSS mechanically generates a legend primarily based on the categorical variable.
For instance, let’s create a scatter plot utilizing the built-in crabs.dta dataset:
// Load knowledge
fname = getGAUSSHome("examples/crabs.dta");
crabs = loadd(fname);
// Create scatter plot with computerized legend
plotScatter(crabs, "rear_width ~ body_depth + by(intercourse)");
When the by key phrase is used with the specific variable, intercourse GAUSS:
- Plots a separate shade for every group.
- Robotically creates a legend indicating totally different teams.
- Features a title on the legend.
These legends are helpful once we simply want a fast look at our knowledge. Nonetheless, they do not permit for customized formatting. To make use of customized formatting we have to use a plotControl construction.
Setting Up a plotControl Construction
To customise a GAUSS plot, step one is to declare and initialize a plotControl construction. This construction is used for all plot-related settings, together with axis labels, colours, fonts, legends, and extra.
Why Use a plotControl Construction?
The plotControl construction offers a versatile and arranged strategy to modify a plot’s look. As a substitute of manually formatting the plot after it’s created, we will programmatically set all customizations upfront. This protects us effort and time when we have to reproduce our graphs.
To make use of this construction we:
- Declare a
plotControlconstruction. - Fill it with default settings utilizing plotGetDefaults.
- Modify the construction’s properties as wanted.
- Cross the construction when calling our GAUSS plotting perform.
Declaring and Initializing the plotControl Construction
Each plot customization begins with the next setup:
// Declare plot management construction
struct plotControl myPlot;
// Fill with default settings for an XY plot
myPlot = plotGetDefaults("xy");
Discover that the defaults are particular to the plot sort we’re making. For instance, if we have been making a bar or scatter plot, we might use “bar” or “scatter” as a substitute.
As soon as the plotControl construction is initialized, we will customise all graph properties—resembling including a legend.
Including a Fundamental Legend
After declaring and initializing our plotControl construction, we will use the plotSetLegend perform so as to add a default styled legend to any plot.
The perform takes two required inputs:
- A pointer to a plot management construction.
- A string array containing legend labels.
Moreover, two non-compulsory arguments could also be used:
- A string specifying the legend location.
- A scalar indicating vertical or horizontal orientation.
Including a Default Legend
Let us take a look at including a easy legend to an XY plot with the default location and orientation:
// Declare plot management construction
struct plotControl myPlot;
// Fill with default settings for xy plot
myPlot = plotGetDefaults("xy");
// Pattern knowledge
x = seqa(1, 1, 10);
y1 = x + rndn(10, 1);
y2 = x - 0.5 + rndn(10, 1);
// Specify legend labels
// utilizing '$|" to concatenate
// particular person labels
label = "Group 1"$|"Group 2";
// Arrange primary legend
plotSetLegend(&myPlot, label);
// Create XY plot
plotXY(myPlot, x, y1~y2);
Altering the Legend Location
By default our legend is within the high, proper nook of our plot canvas. This may increasingly not at all times be the best location, as we will see within the plot above.
Happily, the location enter permits us to specify a distinct location. The location enter can both be the xy coordinates for the highest left of the legend, or a string. Setting xy coordinates permits for exact placement, however can typically be extra cumbersome.
When specifying the legend location utilizing a string, you might use a number of of the next:
- Vertical location:
"high"(default),"vcenter", or"backside". - Horizontal location:
"left","hcenter", or"proper"(default). - Inside/outdoors location:
"inside"(default) or"outdoors"
For instance, let’s change the legend location to the underside, proper nook of the plot:
// Specify legend labels
// utilizing '$|" to concatenate
// particular person labels
label = "Group 1"$|"Group 2";
// Place in backside proper nook
location = "backside proper";
// Set legend
plotSetLegend(&myPlot, label, location);
// Create XY plot
plotXY(myPlot, x, y1~y2);
These location parts could be laid out in any order. For instance, we might get the identical outcomes specifying the placement like this:
// Place in backside proper nook
location = "proper backside";
We might create a really comparable graph by specifying the highest left of the legend to be at x=7.5 and y=2 like this:
// Specify xy coordinates for the highest left nook of the legend.
location = { 7.5, 2 };
Altering the Legend Orientation
The plotSetLegend process additionally permits us to specify if the sequence are listed horizontally or vertically utilizing the non-compulsory orientation enter.
The orientation enter is ready to:
- 1 for a vertical sequence record (default).
- 0 for a horizontal sequence record.
// Specify legend labels
// utilizing '$|" to concatenate
// particular person labels
label = "Group 1"$|"Group 2";
// Place in backside proper nook
location = "backside proper";
// Set to horizontal record
orientation = 0;
// Set legend
plotSetLegend(&myPlot, label, location, orientation);
// Create XY plot
plotXY(myPlot, x, y1~y2);
Superior Legend Formatting
Along with the fundamental legend, GAUSS offers a number of features to customise legend look.
GAUSS Legend Customization Capabilities |
||
|---|---|---|
| Perform Identify | Description | Instance |
| plotSetLegend | Defines a legend for the plot with customized labels. | plotSetLegend(&myPlot, label [, location, orientation]); |
| plotSetLegendBkd | Units the opacity and shade for the background of a graph legend. | plotSetLegendBkd(&myPlot, opacity [, bkd_clr]); |
| plotSetLegendBorder | Controls the colour and thickness of the legend border. | plotSetLegendBorder(&myPlot, clr [, thickness]); |
| plotSetLegendFont | Customizes the font fashion, dimension, and shade of legend textual content. | plotSetLegendFont(&myPlot, font [, font_size, font_color]); |
| plotSetLegendTitle | Controls the legend title. | plotSetLegendTitle(&myPlot, title) |
| plotSetTextInterpreter | Controls the textual content interpreter settings for a graph. | plotSetTextInterpreter(&myPlot, interpreter [, location]); |
Instance: Superior Legend Formatting
Let us take a look at one other plotting instance and discover a few of the superior legend formatting choices.
To get began, we are going to simulate some knowledge:
/*
** Create the sequence 0.25, 0.5, 0.75...3
*/
x = seqa(0.25, 0.25, 12);
y = sin(x);
and setup our plotControl construction
// Declare plotControl construction
// and fill with default settings for XY plots
struct plotControl myPlot;
myPlot = plotGetDefaults("xy");
We wish the legend for this plot to:
- Be horizontally centered and positioned outdoors the underside of the plot.
- Use 14 pt., “darkish blue”, Arial font.
- Have a “mild grey” border with a thickness of two pixels.
- Render and interpret labels utilizing latex.
Labels and Location
We set the labels and placement utilizing the plotSetLegend process:
/*
** Fundamental legend settings
*/
// Set label
label = "sin{x}";
// Set location
location = "backside hcenter outdoors";
// Set legend
plotSetLegend(&myPlot, label, location);
Legend Font Properties
The plotSetLegendFont perform permits us to regulate the font fashion, dimension, and shade of the legend textual content.
/*
** Legend font
*/
// Set font
font_style = "Arial";
// Set font dimension
font_size = 14;
// Set font shade
font_clr = "darkish blue";
// Set all legend font properties
plotSetLegendFont(&myPlot, font_style, font_size, font_clr);
Customizing The Legend Border
The plotSetLegendBorder process units the colour and width of the border.
/*
** Legend border
*/
// Set border shade
border_clr = "mild grey";
// Border width
border_width = 2;
// Set the legend border
plotSetLegendBorder(&myPlot, border_clr, border_width);
Altering Textual content Interpretation
By default, GAUSS treats legend textual content as plain textual content. Nonetheless, we will allow LaTeX-style formatting utilizing plotSetTextInterpreter:
/*
** Set textual content interpret to interpret
** latex for legend labels
*/
plotSetTextInterpreter(&myPlot, "latex", "legend");
Producing Our Plot
// Create XY plot
plotXY(myPlot, x, y);
Conclusion
On this weblog, we lined other ways to customise legends in GAUSS plots:
- Including a legend utilizing
plotSetLegend. - Modifying fonts, backgrounds, and borders for higher visualization.
- Using LaTeX formatting and including legend titles.
- Robotically producing legends utilizing the
bykey phrase.
These strategies improve the readability of your visualizations, making it simpler to interpret outcomes.
Additional Studying
- Find out how to combine, match and magnificence totally different graph sorts
- Find out how to Interactively Create Reusable Graphics Profiles
- Find out how to Create Tiled Graphs in GAUSS
- Visualizing COVID-19 Panel Knowledge With GAUSS 22
- Superior Formatting Methods for Creating AER High quality Plots
- Introduction to Environment friendly Creation of Detailed Plots
Eric has been working to construct, distribute, and strengthen the GAUSS universe since 2012. He’s an economist expert in knowledge evaluation and software program growth. He has earned a B.A. and MSc in economics and engineering and has over 18 years of mixed business and educational expertise in knowledge evaluation and analysis.





