Syntax
Main Clauses
ggsql augments the standard SQL syntax with a number of new clauses to describe a visualisation:
VISUALISEinitiates the visualisation part of the queryDRAWadds a new layer to the visualisationSCALEspecify how an aesthetic should be scaledFACETdescribes how data should be split into small multiplesPROJECTis used for selecting the coordinate system to useLABELis used to manually add titles to the plot or the various axes and legends
Layers
There are many different layers to choose from when visualising your data. Some are straightforward translations of your data into visual marks such as a point layer, while others perform more or less complicated calculations like e.g. the histogram layer. A layer is selected by providing the layer name after the DRAW clause
Layer types
pointis used to create a scatterplot layer.lineis used to produce lineplots with the data sorted along the x axis.pathis likelineabove but does not sort the data but plot it according to its own order.segmentconnects two points with a line segment.lineardraws a long line parameterised by a coefficient and intercept.ruledraws horizontal and vertical reference lines.areais used to display series as an area chart.ribbonis used to display series extrema.polygonis used to display arbitrary shapes as polygons.barcreates a bar chart, optionally calculating y from the number of records in each bar.densitycreates univariate kernel density estimates, showing the distribution of a variable.violindisplays a rotated kernel density estimate.histogrambins the data along the x axis and produces a bar for each bin showing the number of records in it.boxplotdisplays continuous variables as 5-number summaries.errorbara line segment with hinges at the endpoints.
Position adjustments
Scales
A scale is responsible for translating a data value to an aesthetic literal, e.g. a specific color for the fill aesthetic, or a radius in points for the size aesthetic. A scale is a combination of a specific aesthetic and a scale type
Aesthetics
- Position aesthetics are those aesthetics related to the spatial location of the data in the coordinate system.
- Color aesthetics are related to the color of fill and stroke
opacityis the aesthetic that determines the opacity of the colorlinetypegoverns the stroke pattern of strokeslinewidthdetermines the width of strokesshapedetermines the shape of pointssizegoverns the radius of points- Faceting aesthetics are used to determine which facet panel the data belongs to
Scale types
continuousscales translates a continuous input to a continuous outputdiscretescales translates discrete input to a discrete outputbinnedscales translate continuous input to an ordered discrete output by binning the dataordinalscales translate discrete input to an ordered discrete output by enforcing an ordering to the inputidentityscales passes the data through unchanged
Coordinate systems
The coordinate system defines how the abstract positional aesthetics are projected onto the screen or paper where the final plot appears. As such, it has great influence over the final look of the plot.
cartesianis the classic coordinate system consisting of two perpendicular axes, one being horizontal and one being verticalpolarinterprets the primary position as the angular location relative to the center and the secondary position as the distance (radius) from the center, and this creates a circular coordinate system