Skip to content

Plotting

atecon edited this page Apr 23, 2024 · 12 revisions

Here you find everything around plotting using gretl in combination with gnuplot.

Histogram

Simple histogram

Use the freq command.

Here is a simple example:

open mroz87.gdt --quiet
freq FAMINC --plot=display

Including a fitted Gamma distribution

Use again the freq command and add the option --gamma (--normal would also be available):

open mroz87.gdt --quiet
freq FAMINC --gamma --plot=display

This produces:

Boxplot

Simple example

For creating a simple boxplot, run the boxplot command:

open mroz87.gdt --quiet
boxplot FAMINC --output=display

This produces:

Factorized boxplot

For creating boxplots for each category of some discrete variable, do the following (the example also involves some tweaking):

open mroz87.gdt --quiet
boxplot FAMINC WE --factorized --output=display \
{ set grid;\
  set title "Foo" font ’,15’;\
  set xlabel "Ausbildungsjahre" font ’,14’;}

This returns:

Density plot

Simple example

For a simple plot, you can make use of the kdplot command:

set verbose off
open mroz87.gdt
kdplot FAMINC --output=display

which produces:

Grouped density plot

The fdensity package

The user-written package fdensity provides a simple interface for creating a grouped (factorized) kernel density plot.

To install the package from the package serve, simply execute in the command line or via script:

pkg install fdensity

The following example plots the density for lwage for each category of variable choice:

include fdensity.gfn   # load the package into memory
open keane.gdt --quiet
fdensity(lwage, choice)

The plot looks as:

The manual way to produce such a graph

To produce a kernel density plot drawing the density for two variables, you may use the following example involving a function which produces the shown plot.

function void double_density(series x, series d)
    /* Plots a double density plot, which compares the densities of two series
    x and d. The function first computes the kernel densities of x and d using
    the kdensity() function, then combines these two densities into a single matrix
    for plotting using GNUplot.

   :param x: A gretl series containing the first variable to be compared in the double density plot.
   :param d: A gretl series containing the second variable to be compared in the double density plot.
    */
    string s = argname(d)
    
    matrix d0 = kdensity(d ? NA : x)
    matrix d1 = kdensity(d ? x : NA)
    
    matrix dd = (d0 ~ NA) | (d1[,1] ~ NA ~ d1[,2])
    
    strings column_label = strsplit(sprintf("x %s=0 %s=1", s, s), " ")
    cnameset(dd, column_label)
    
    gnuplot 2 3 1 --matrix=dd --with-lines --output=display
end function

open australia.gdt --quiet
series dum = t < 1980:1
double_density(IAU, dum)

open mroz87.gdt --quiet
double_density(WA, LFP)

Empirical density function plot

By means of the ecdf() function, one can draw an empirical density function:

open mroz87.gdt --quiet
matrix m = ecdf(FAMINC)

gnuplot 2 1 --matrix=m --with-lines --fit=none --output=display \
  { set ylabel "Probability";\
    set xlabel "Familiy income";\
    set linetype 1 lw 2;\
    set grid;}

The corresponding plot is:

Correlation matrix

The corr command allows you to create a correlation-matrix plot:

open mroz87.gdt --quiet
list L = 1..5
corr L --triangle --plot=display

This produces:

Scatterplot

Basic plot

Draw a basic scatter plot by means of the gnuplot command

open mroz87.gdt --quiet
gnuplot WE log(FAMINC) --output=display

This produces:

Plot with markers (data labels)

Assuming that your dataset already includes so called markers, do the following:

open mrw.gdt --quiet
gnuplot gdp60 school --output=display

Then right-click on the appearing plot and select 'all data labels' or touch an observation point which will show the marker for this specific point.

Factorized scatterplot

Suppose your data comprises different categories. You would like to create a scatter plot between WE log(FAMINC) and make the relationship visible for discrete value of the dummy variable CIT. That's simple!

The following script also involves tweaking the plot to make it more informative:

open mroz87.gdt
gnuplot WE log(FAMINC) CIT --dummy --output=display
{ set title "Some cool title" font ’,15’;\
  set linetype 1 lc rgb ’orange’ ps 1;\
  set linetype 2 lc rgb ’blue’ ps 0.5;\
  set xtics font ’,15’;\
  set grid;}

This produces:

PairPlot package for further options

There also exists the user-contributed package named PairPlot. It allows you to create a matrix of scatterplots for each combination of some list of series.

First download the package from the Gretl package server (only once needed):

pkg install PairPlot

Now, we can use the package to create a matrix of factorized scatter plots:

include PairPlot.gfn
#help PairPlot

open abdata --quiet
list y = n k
series factor = IND

# Plotting options
bundle opts = _(transparency_level = 175,
                centroid = "median", tics = FALSE,
                pointsize = 1.5,
                centroid_pointsize = 3,
                centroid_linewidth = 3,
                height = 600,
                width = 600)

PairPlot(y, factor, opts)

This produces:

Matrix of scatterplots

Suppose you want to plot a list of variables against a single series WE. Simply execute the scatters command:

open mroz87.gdt
list Y = WHRS WW HHRS
scatters WE ; Y --output=display

This produces:

Categorical scatterplots

Suppose one of the two variables is categorical. In this case, a scatterplot might still be of interest. Jittering the values a little bit may reveal interesting patterns. This is type of plot is sometimes called "Swarmplot". For details about the gnuplot options, we refer to the gnuplot help text.

Here is an example script:

open mroz87.gdt --quiet

gnuplot FAMINC KL6 --fit=none --output=display \
  { set jitter over 0.1 spread 0.5;\
  set title "Family income by number of kids under 6";\
  set xrange[-1:];}

The created plot is:

Gridplot

Since Gretl version 2023c, there is built-in functionality for creating a grid of plots. The relevant commands are gpbuild and gridplot

Here is some illustration:

set verbose off
open data4-10
strings MyPlots
gpbuild MyPlots
  gnuplot ENROLL CATHOL
  gnuplot ENROLL INCOME
  gnuplot ENROLL COLLEGE
  boxplot INCOME REGION --factorized
end gpbuild
gridplot MyPlots --output=display

This produces:

Using the --outbuf option

It is possible to prepare an array of plot specifications for use with gridplot without using a gpbuild block, as in the following example:

open data4-10 --quiet
strings MyPlots = array(3)  # empty array of length three for storing the plot-output

gnuplot ENROLL CATHOL --outbuf=MyPlots[1]
gnuplot ENROLL INCOME --outbuf=MyPlots[2]
gnuplot ENROLL COLLEGE --outbuf=MyPlots[3]

gridplot MyPlots --cols=3 --output=display

Some caveats

For some plotting commands, Gretl actually does not support (as of Gretl 2024a) the --outbufoption. In these cases, however, you can still create a gridplot. Here follows an example using the panplot command:

set verbose off
open grunfeld.gdt --quiet

list L = invest value   # Variables to plot as a panelplot
strings MyPlots = array(nelem(L))

# Loop over each variable in L
loop foreach i L
    panplot $i --overlay --output="mytemp$i.plt"   # Store the panplot as a gnuplot file
    MyPlots[i] = readfile("mytemp$i.plt")          # Store the content of the gnuplot file as a string
endloop 

# Use the array of gnuplot commands
gridplot MyPlots --output=display

The resulting plot is:

Heatmap

Gretl has (currently) no built-in support for heatmaps. However, you may want to use the user-contributed package heatmap. Here is an example of how it works:

First, download the package from the Gretl package server by executing the command (only once necessary):

pkg install heatmap

Here is some elaborated example on how to produce the plot:

include heatmap.gfn  # Load the package
# help heatmap

# Restrict sample to the first 4 panel units
smpl $unit < 5 --restrict

# Add information to the plot
bundle Options = _(title = "Matrix A with contour lines",
                   quiet = TRUE, xlabel = "Time dimension",
                   ylabel = "Company")

# Create a suitable matrix from the dataset
scalar T = $pd
scalar N = $nobs / $pd
matrix m = {value}   # cast series as matrix
matrix M = mshape(m, T, N)’
heatmap(M, Options)

This produces:

Modify plots

Set a grid in the background

Gnuplot -- the actual drawing engine in Gretl -- allows you to add a grid. Here is an example for a boxplot but it works the same way for other plots:

open mroz87.gdt --quiet
boxplot FAMINC WE --factorized --output=display { set grid; }

One can also tweak the grid, for instance the color and linewidth, as described here.

The following example increase the width of the grid lines to 3 and the color becomes blue:

open mroz87.gdt --quiet
boxplot FAMINC WE --factorized --output=display { set grid lw 3 lc rgb 'blue'; }

Add titles and labels

Adding or changing a title and labels of axis is simple. Here is an example for a boxplot even though it works the same for other built-in types of plots:

open mroz87.gdt --quiet

boxplot FAMINC WE --factorized --output=display \
{ set title "Some title" font ",15";\
  set xlabel "Some xlabel" font ",14";\
  set ylabel "Some ylabel" font ",10";}

Set linewidth and dashed lines

Gnuplot offers the way to set both the linewidth as well as different types of dashes globally by a single command, respectively.

Here is the gretl plot command block where two literals were added to a simple line plot.

open denmark --quiet
list L = log(dataset)

plot L
    options with-lines time-series
    literal set termoption lw 2        # set linewidth globally
    # set dashtype for each series
    literal set for [i=1:8] linetype i dashtype i
end plot --output=display

This produces the following graph.

This produces:

Clone this wiki locally