Skip to content

Plotting

atecon edited this page Jan 19, 2024 · 12 revisions

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

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:

Correlation matrix

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

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

This produces:

Density plot

Simple example

Call the kdplot command:

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

This produces:

Multiple densities in a single graph

To produce a kernel density plot drawing the density for two variables, you may use the following example 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)

Gridplot

Since Gretl version 2023c, there is built-in functionality for creating a grid of plots. 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:

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:

Histogram

Simple histogram

Use the freq command.

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:

Scatterplot

Basic plot

Draw a basic scatter plot:

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 plot

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. Here is an example.

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 plots

Suppose you want to plot a list of variables against a single series WE. Simply run the following:

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

This produces:

Manipulate plots

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