Skip to content

Introductory Example

no0p edited this page Oct 14, 2014 · 4 revisions

Data is plotted by passing an sql query to the plot() function.

Let's consider an example based on a table of air quality data from R's data library.

robert=# select * from airquality limit 10;
 id | ozone | solar_r | wind | temp | month | day 
----+-------+---------+------+------+-------+-----
  1 | 41    | 190     |  7.4 | 67   | 5     | 1
  2 | 36    | 118     |    8 | 72   | 5     | 2
  3 | 12    | 149     | 12.6 | 74   | 5     | 3
  4 | 18    | 313     | 11.5 | 62   | 5     | 4
  5 |       |         | 14.3 | 56   | 5     | 5
  6 | 28    |         | 14.9 | 66   | 5     | 6
  7 | 23    | 299     |  8.6 | 65   | 5     | 7
  8 | 19    | 99      | 13.8 | 59   | 5     | 8
  9 | 8     | 19      | 20.1 | 61   | 5     | 9
 10 |       | 194     |  8.6 | 69   | 5     | 10
(10 rows)


A scatter plot is generated by passing a query to the plot command which has two numeric columns in the result set.
robert=# select plot('select wind::numeric, 
                      temp::numeric from airquality limit 20'
                    )  as "air quality scatter plot";

             air quality scatter plot             
--------------------------------------------------
                                                 +
   74 ++A--+----+----+-A---+----+----+----+---++ +
      +    +    +    +     +    +    +    +    + +
   72 ++   A                                  ++ +
   70 ++                                      ++ +
      |      A A                               | +
   68 ++  A        AA                         ++ +
   66 ++      A      A       A                ++ +
      |      A                                 | +
   64 ++            A                         ++ +
   62 ++       A    A                         ++ +
      |                                   A    | +
   60 ++                                      ++ +
   58 ++                A A                   ++ +
      +    +    +    +     +    +    +A   +    + +
   56 ++---+----+----+-----A----+----+----+---++ +
      6    8    10   12    14   16   18   20   22+
                        wind                     
                                                 
 
(1 row)

Plotting for the Web

The same plot can be prepared for display on the web by changing the terminal parameter.

robert =# set plotpg.terminal = 'svg';
SET

robert =# select plot('select wind::numeric, 
                              temp::numeric 
                       from airquality limit 20'
                     ) as "air quality scatter plot";


<?xml version="1.0" encoding="utf-8"  standalone="no"?>
  <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" 
 ...

Air Quality Scatter

Terminal can also be set to 'png' for image data.

Clone this wiki locally