ggplot#

New in version 0.7:

pip install jupysql --upgrade

Note

ggplot API requires matplotlib: pip install matplotlib

The ggplot API is structured around the principles of the grammar of graphics, and allows you to build any graph using the same components: a data set, a coordinate system, and geoms (geometric objects).

To make it suitble for JupySQL, specifically for the purpose of running SQL and plotting larger-than-memory datasets on any laptop, we made a small modification from the original ggplot2 API. Rather than providing a dataset, we now provide a SQL table name.

Other than that, at this point we support:

Aes:

  • x - a SQL column mapping

  • color and fill to apply edgecolor and fill colors to plot shapes

Geoms:

  • geom_boxplot

  • geom_histogram

Facet:

  • facet_wrap to display multiple plots in 1 layout

Please note that each geom has its own unique attributes, e.g: number of bins in geom_histogram. We’ll cover all the possible parameters in this tutorial.

Building a graph#

To build a graph, we first should initialize a ggplot instance with a reference to our SQL table using the table parameter, and a mapping object. Here’s is the complete template to build any graph.

(
    ggplot(table='sql_table_name', mapping=aes(x='table_column_name'))
    +
    geom_func() # geom_histogram or geom_boxplot (required)
    +
    facet_func() # facet_wrap (optional)
)

Note

Please note this is the 1st release of ggplot API. We highly encourage you to provide us with your feedback through our Slack channel to assist us in improving the API and addressing any issues as soon as possible.

Examples#

First, establish the connection, import necessary functions and prepare the data.

Setup#

%load_ext sql
%sql duckdb://
from sql.ggplot import ggplot, aes, geom_boxplot, geom_histogram, facet_wrap
from pathlib import Path
from urllib.request import urlretrieve

url = "https://d37ci6vzurychx.cloudfront.net/trip-data/yellow_tripdata_2021-01.parquet"

if not Path("yellow_tripdata_2021-01.parquet").is_file():
    urlretrieve(url, "yellow_tripdata_2021-01.parquet")

Boxplot#

(ggplot("yellow_tripdata_2021-01.parquet", aes(x="trip_distance")) + geom_boxplot())
<sql.ggplot.ggplot.ggplot at 0x7f7de2f5df30>
../_images/c22ed97070224b999501ca063fc98adb17941a1b3576f5323bd24d131e964b45.png

Histogram#

To make it more interesting, let’s create a query that filters by the 90th percentile. Note that we’re using the --save, and --no-execute functions. This tells JupySQL to store the query, but skips execution. We’ll reference it in our next plotting calls using the with_ parameter.

%%sql --save short_trips --no-execute
select * from 'yellow_tripdata_2021-01.parquet'
WHERE trip_distance < 6.3
*  duckdb://
Skipping execution...
(
    ggplot(table="short_trips", with_="short_trips", mapping=aes(x="trip_distance"))
    + geom_histogram(bins=10)
)
<sql.ggplot.ggplot.ggplot at 0x7f7de2e5b820>
../_images/c765ff7200e4e8713cbd44dab395f9e755bfa0f70abbb70ff31fbd619950b062.png

Custom Style#

By modifying the fill and color attributes, we can apply our custom style

(
    ggplot(
        table="short_trips",
        with_="short_trips",
        mapping=aes(x="trip_distance", fill="#69f0ae", color="#fff"),
    )
    + geom_histogram(bins=10)
)
<sql.ggplot.ggplot.ggplot at 0x7f7dd92cef80>
../_images/bc3e3d61bf0dc32193c2ff36069cc768ef9b02ed0a45ed0b2546e1d9b00eb566.png

When using multiple columns we can apply color on each column

(
    ggplot(
        table="short_trips",
        with_="short_trips",
        mapping=aes(
            x=["PULocationID", "DOLocationID"],
            fill=["#d500f9", "#fb8c00"],
            color="white",
        ),
    )
    + geom_histogram(bins=10)
)
<sql.ggplot.ggplot.ggplot at 0x7f7dd92abbe0>
../_images/507afb5a0fedfe2ac6628d8dcde3329ab5dd50b8714ad1909e304f6d9776ec01.png

Categorical histogram#

To make it easier to demonstrate, let’s use ggplot2 diamonds dataset.

from pathlib import Path
from urllib.request import urlretrieve

if not Path("diamonds.csv").is_file():
    urlretrieve(
        "https://raw.githubusercontent.com/tidyverse/ggplot2/main/data-raw/diamonds.csv",  # noqa
        "diamonds.csv",
    )
%%sql
CREATE TABLE diamonds AS SELECT * FROM diamonds.csv
*  duckdb://
Done.
Count
53940

Now, let’s create a histogram of the different cuts of the diamonds by setting x='cut'. Please note, since the values of cut are strings, we don’t need the bins attribute here.

(ggplot("diamonds", aes(x="cut")) + geom_histogram())
<sql.ggplot.ggplot.ggplot at 0x7f7dd9c1da20>
../_images/bd41ff1ab694382911bfe16a67eeba397cf0f384714bb154b89f409437ec1a07.png

We can show a histogram of multiple columns by setting x=['cut', 'color']

(ggplot("diamonds", aes(x=["cut", "color"])) + geom_histogram())
<sql.ggplot.ggplot.ggplot at 0x7f7dd9c85fc0>
../_images/6a9e7fd18cf233508af60f3898895629229741dda986cee2696630f6a46c1d99.png

Apply a custom color with color and fill

(
    ggplot("diamonds", aes(x="price", fill="green", color="white"))
    + geom_histogram(bins=10, fill="cut")
)
<sql.ggplot.ggplot.ggplot at 0x7f7dd915feb0>
../_images/5c9b577fff427a71ed3992535f1065e2c38067769a522f47fa8fdf8aeb29ebe7.png

If we map the fill attribute to a different variable such as cut, the bars will stack automatically. Each colored rectangle on the stacked bars will represent a unique combination of price and cut.

(ggplot("diamonds", aes(x="price")) + geom_histogram(bins=10, fill="cut"))
<sql.ggplot.ggplot.ggplot at 0x7f7dd839d5d0>
../_images/77a191d0c252a8701d7b1bf169c80ce026eec46644b57fdb6c73db3d28b19686.png

We can apply a different coloring using cmap

(
    ggplot("diamonds", aes(x="price"))
    + geom_histogram(bins=10, fill="cut", cmap="plasma")
)
<sql.ggplot.ggplot.ggplot at 0x7f7dd83313c0>
../_images/dc742d5639b3e3108a445230542f36190dfd5858fb295d678701f9d7d8dad59f.png

Facet wrap#

facet_wrap() arranges a sequence of panels into a 2D grid, which is beneficial when dealing with a single variable that has multiple levels, and you want to arrange the plots in a more space efficient manner.

Let’s see an example of how we can arrange the diamonds price histogram for each different color

(ggplot("diamonds", aes(x="price")) + geom_histogram(bins=10) + facet_wrap("color"))
<sql.ggplot.ggplot.ggplot at 0x7f7dd83126e0>
../_images/6d2904d7aa3955b0403de9eeb72878e9a6acd7230b72e7e81d85662e79d74060.png

We can even examine the stacked histogram of price by cut, for each different color. Let’s also hide legend with legend=False to see each plot clearly.

(
    ggplot("diamonds", aes(x="price"))
    + geom_histogram(bins=10, fill="cut")
    + facet_wrap("color", legend=False)
)
<sql.ggplot.ggplot.ggplot at 0x7f7dd91b92a0>
../_images/909303d00e423d2aac10aa27ac9298ede1533589fd00754f0478a73100f85ee2.png