lets_plot.ggplot

lets_plot.ggplot(data=None, mapping=None)

Create a new ggplot plot.

Parameters
  • data (dict or DataFrame) – Default dataset to use for the plot. If not specified, must be supplied in each layer added to the plot.

  • mapping (FeatureSpec) – Default list of aesthetic mappings to use for the plot. If not specified, must be supplied in each layer added to the plot.

Returns

Plot specification.

Return type

PlotSpec

Note

ggplot() initializes a ggplot object. It can be used to declare the input data frame for a graphic and to specify the set of plot aesthetics intended to be common throughout all subsequent layers unless specifically overridden.

ggplot() is typically used to construct a plot incrementally, using the + operator to add layers to the existing ggplot object. This is advantageous in that the code is explicit about which layers are added and the order in which they are added. For complex graphics with multiple layers, initialization with ggplot() is recommended.

There are three common ways to invoke ggplot (see examples below):

  • ggplot(data, aes(x, y)): This method is recommended if all layers use the same data and the same set of aesthetics, although this method can also be used to add a layer using data from another data frame.

  • ggplot(data): This method specifies the default data frame to use for the plot, but no aesthetics are defined up front. This is useful when one data frame is used predominantly as layers are added, but the aesthetics may vary from one layer to another.

  • ggplot(): This method initializes a skeleton ggplot object which is fleshed out as layers are added. This method is useful when multiple data frames are used to produce different layers, as is often the case in complex graphics.

ggplot() with no layers defined will produce an error message: “No layers in plot”.

Examples

 1import numpy as np
 2from lets_plot import *
 3LetsPlot.setup_html()
 4np.random.seed(42)
 5n = 100
 6x = np.random.uniform(-1, 1, size=n)
 7y = np.random.normal(size=n)
 8data = {'x': x, 'y': 25 * x ** 2 + y}
 9# three ways to invoke ggplot, producing the same output:
10# (1)
11ggplot(data, aes(x='x', y='y')) + geom_point()
12# (2)
13ggplot(data) + geom_point(aes(x='x', y='y'))
14# (3)
15ggplot() + geom_point(aes(x='x', y='y'), data=data)