Module: Charty::PlotMethods

Included in:
Charty
Defined in:
lib/charty/plot_methods.rb

Instance Method Summary collapse

Instance Method Details

#bar_plot(x: nil, y: nil, color: nil, data: nil, order: nil, color_order: nil, estimator: :mean, ci: 95, n_boot: 1000, units: nil, random: nil, orient: nil, key_color: nil, palette: nil, saturation:, error_color: [0.26, 0.26, 0.26], error_width: nil, cap_size: nil, dodge: true, log: false, x_label: nil, y_label: nil, title: nil, **options, &block) ⇒ Object

Show the given data as rectangular bars.

Parameters:

  • x (defaults to: nil)

    x-dimension input for plotting long-form data.

  • y (defaults to: nil)

    y-dimension input for plotting long-form data.

  • color (defaults to: nil)

    color-dimension input for plotting long-form data.

  • data (defaults to: nil)

    Dataset for plotting.

  • order (defaults to: nil)

    Order of the categorical dimension to plot the categorical levels in.

  • color_order (defaults to: nil)

    Order of the color dimension to plot the categorical levels in.

  • estimator (defaults to: :mean)

    Statistical function to estimate withint each categorical bin.

  • ci (defaults to: 95)

    Size of confidence intervals to draw around estimated values.

  • n_boot (defaults to: 1000)

    The size of bootstrap sample to use when computing confidence intervals.

  • units (defaults to: nil)

    Identifier of sampling unit.

  • random (defaults to: nil)

    Random seed or random number generator for reproducible bootstrapping.

  • orient (defaults to: nil)

    Orientation of the plot (:v for vertical, or :h for horizontal).

  • key_color (defaults to: nil)

    Color for all of the elements, or seed for a gradient palette.

  • palette (defaults to: nil)

    Colors to use for the different levels of the color-dimension variable.

  • saturation

    Propotion of the original saturation to draw colors.

  • error_color (defaults to: [0.26, 0.26, 0.26])

    Color for the lines that represent the confidence intervals.

  • error_width (defaults to: nil)

    Thickness of error bar lines (and caps).

  • cap_size (defaults to: nil)

    Width of the caps on error bars.

  • dodge (true, false) (defaults to: true)

    If true, bar position is shifted along the categorical axis for avoid overlapping when the color-dimension is used.

  • log (true, false) (defaults to: false)

    Set the value-axis (e.g. Y-axis if orient is :v) to be log scale.

  • x_label (String, Symbol, #to_str, nil) (defaults to: nil)

    X-axis label.

  • y_label (String, Symbol, #to_str, nil) (defaults to: nil)

    Y-axis label.

  • title (String, Symbol, #to_str, nil) (defaults to: nil)

    Title text.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/charty/plot_methods.rb', line 30

def bar_plot(x: nil, y: nil, color: nil, data: nil,
             order: nil, color_order: nil,
             estimator: :mean, ci: 95, n_boot: 1000, units: nil, random: nil,
             orient: nil, key_color: nil, palette: nil, saturation: 1r,
             error_color: [0.26, 0.26, 0.26], error_width: nil, cap_size: nil,
             dodge: true, log: false, x_label: nil, y_label: nil, title: nil,
             **options, &block)
  Plotters::BarPlotter.new(
    data: data, variables: { x: x, y: y, color: color },
    order: order, orient: orient,
    estimator: estimator, ci: ci, n_boot: n_boot, units: units, random: random,
    color_order: color_order, key_color: key_color, palette: palette, saturation: saturation,
    error_color: error_color, error_width: error_width, cap_size: cap_size,
    dodge: dodge, log: log, x_label: x_label, y_label: y_label, title: title,
    **options, &block
  )
end

#box_plot(x: nil, y: nil, color: nil, data: nil, order: nil, color_order: nil, orient: nil, key_color: nil, palette: nil, saturation:, width:, dodge: true, flier_size: 5, line_width: nil, whisker: 1.5, x_label: nil, y_label: nil, title: nil, **options, &block) ⇒ Object

Show the distributions of the given data by boxes and whiskers.

Parameters:

  • x (defaults to: nil)

    X-dimension input for plotting long-Form data.

  • y (defaults to: nil)

    Y-dimension input for plotting long-form data.

  • color (defaults to: nil)

    Color-dimension input for plotting long-form data.

  • data (defaults to: nil)

    Dataset for plotting.

  • order (defaults to: nil)

    Order of the categorical dimension to plot the categorical levels in.

  • color_order (defaults to: nil)

    Order of the color dimension to plot the categorical levels in.

  • orient (defaults to: nil)

    Orientation of the plot (:v for vertical, or :h for horizontal).

  • key_color (defaults to: nil)

    Color for all of the elements, or seed for a gradient palette.

  • palette (defaults to: nil)

    Colors to use for the different levels of the color-dimension variable.

  • saturation

    Propotion of the original saturation to draw colors.

  • width

    Width of a full element when not using the color-dimension, or width of all the elements for one level of the major grouping variable.

  • dodge (true, false) (defaults to: true)

    If true, bar position is shifted along the categorical axis for avoid overlapping when the color-dimension is used.

  • flier_size (defaults to: 5)

    Size of the markers used to indicate outlier observations.

  • line_width (defaults to: nil)

    Width of the gray lines that frame the plot elements.

  • whisker (defaults to: 1.5)

    Propotion of the IQR past the low and high quartiles to extend the plot whiskers. Points outside of this range will be treated as outliers.

  • x_label (String, Symbol, #to_str, nil) (defaults to: nil)

    X-axis label.

  • y_label (String, Symbol, #to_str, nil) (defaults to: nil)

    Y-axis label.

  • title (String, Symbol, #to_str, nil) (defaults to: nil)

    Title text.



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/charty/plot_methods.rb', line 121

def box_plot(x: nil, y: nil, color: nil, data: nil,
             order: nil, color_order: nil,
             orient: nil, key_color: nil, palette: nil, saturation: 1r,
             width: 0.8r, dodge: true, flier_size: 5, line_width: nil,
             whisker: 1.5, x_label: nil, y_label: nil, title: nil,
             **options, &block)
  Plotters::BoxPlotter.new(
    data: data,
    variables: { x: x, y: y, color: color },
    order: order,
    color_order: color_order,
    orient: orient,
    key_color: key_color,
    palette: palette,
    saturation: saturation,
    width: width,
    dodge: dodge,
    flier_size: flier_size,
    line_width: line_width,
    whisker: whisker,
    x_label: x_label,
    y_label: y_label,
    title: title,
    **options,
    &block
  )
end

#count_plot(x: nil, y: nil, color: nil, data: nil, order: nil, color_order: nil, orient: nil, key_color: nil, palette: nil, saturation:, dodge: true, log: false, x_label: nil, y_label: nil, title: nil, **options, &block) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/charty/plot_methods.rb', line 48

def count_plot(x: nil, y: nil, color: nil, data: nil,
               order: nil, color_order: nil,
               orient: nil, key_color: nil, palette: nil, saturation: 1r,
               dodge: true, log: false, x_label: nil, y_label: nil, title: nil,
               **options, &block)
  case
  when x.nil? && !y.nil?
    x = y
    orient = :h
  when y.nil? && !x.nil?
    y = x
    orient = :v
  when !x.nil? && !y.nil?
    raise ArgumentError,
          "Unable to pass both x and y to count_plot"
  end

  Plotters::CountPlotter.new(
    data: data,
    variables: { x: x, y: y, color: color },
    order: order,
    orient: orient,
    estimator: :count,
    ci: nil,
    units: nil,
    random: nil,
    color_order: color_order,
    key_color: key_color,
    palette: palette,
    saturation: saturation,
    dodge: dodge,
    x_label: x_label,
    y_label: y_label,
    title: title,
    **options
  ) do |plotter|
    plotter.value_label = "count"
    block.(plotter) unless block.nil?
  end
end

#hist_plot(data: nil, x: nil, y: nil, color: nil, weights: nil, stat: :count, bins: :auto, bin_range: nil, common_bins: true, key_color: nil, palette: nil, color_order: nil, color_norm: nil, legend: true, x_label: nil, y_label: nil, title: nil, **options, &block) ⇒ Object



282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# File 'lib/charty/plot_methods.rb', line 282

def hist_plot(data: nil, x: nil, y: nil, color: nil, weights: nil,
              stat: :count, bins: :auto,
              bin_range: nil, common_bins: true,
              key_color: nil, palette: nil, color_order: nil, color_norm: nil,
              legend: true, x_label: nil, y_label: nil, title: nil,
              **options, &block)
  # TODO: support following arguments
  # - wiehgts
  # - binwidth
  # - discrete
  # - cumulative
  # - common_norm
  # - multiple
  # - element
  # - fill
  # - shrink
  # - kde
  # - kde_params
  # - line_params
  # - thresh
  # - pthresh
  # - pmax
  # - cbar
  # - cbar_params
  # - x_log_scale
  # - y_log_scale
  Plotters::HistogramPlotter.new(
    data: data,
    variables: { x: x, y: y, color: color },
    weights: weights,
    stat: stat,
    bins: bins,
    bin_range: bin_range,
    common_bins: common_bins,
    key_color: key_color,
    palette: palette,
    color_order: color_order,
    color_norm: color_norm,
    legend: legend,
    x_label: x_label,
    y_label: y_label,
    title: title,
    **options,
    &block)
end

#line_plot(x: nil, y: nil, color: nil, style: nil, size: nil, data: nil, key_color: nil, palette: nil, color_order: nil, color_norm: nil, sizes: nil, size_order: nil, size_norm: nil, markers: nil, dashes: true, style_order: nil, units: nil, estimator: :mean, n_boot: 1000, random: nil, sort: true, err_style: :band, err_params: nil, error_bar: [:ci, 95], x_scale: :linear, y_scale: :linear, legend: :auto, x_label: nil, y_label: nil, title: nil, **options, &block) ⇒ Object

Line plot

Parameters:

  • x (vector-like object, key in data) (defaults to: nil)
  • y (vector-like object, key in data) (defaults to: nil)
  • color (vector-like object, key in data) (defaults to: nil)
  • style (vector-like object, key in data) (defaults to: nil)
  • size (vector-like object, key in data) (defaults to: nil)
  • data (table-like object) (defaults to: nil)
  • key_color (color object) (defaults to: nil)
  • palette (String, Array<Numeric>, Palette) (defaults to: nil)
  • color_order (Array<String>, Array<Symbol>) (defaults to: nil)
  • color_norm (defaults to: nil)
  • sizes (Array, Hash) (defaults to: nil)
  • size_order (Array) (defaults to: nil)
  • size_norm (defaults to: nil)
  • dashes (true, false, Array, Hash) (defaults to: true)
  • markers (true, false, Array, Hash) (defaults to: nil)
  • style_order (Array) (defaults to: nil)
  • units (vector-like object, key in data) (defaults to: nil)
  • estimator (:mean) (defaults to: :mean)
  • n_boot (Integer) (defaults to: 1000)
  • random (Integer, Random, nil) (defaults to: nil)
  • sort (true, false) (defaults to: true)
  • err_style (:band, :bars) (defaults to: :band)
  • err_params (Hash) (defaults to: nil)
  • error_bar (defaults to: [:ci, 95])
  • x_scale (:linear, :log10) (defaults to: :linear)
  • y_scale (:linear, :log10) (defaults to: :linear)
  • legend (:auto, :brief, :full, false) (defaults to: :auto)

    How to draw legend. If :brief, numeric color and size variables will be represented with a sample of evenly spaced values. If :full, every group will get an entry in the legend. If :auto, choose between brief or full representation based on number of levels. If false, no legend data is added and no legend is drawn.

  • x_label (String, Symbol, #to_str, nil) (defaults to: nil)

    X-axis label.

  • y_label (String, Symbol, #to_str, nil) (defaults to: nil)

    Y-axis label.

  • title (String, Symbol, #to_str, nil) (defaults to: nil)

    Title text.



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/charty/plot_methods.rb', line 186

def line_plot(x: nil, y: nil, color: nil, style: nil, size: nil,
              data: nil, key_color: nil, palette: nil, color_order: nil,
              color_norm: nil, sizes: nil, size_order: nil, size_norm: nil,
              markers: nil, dashes: true, style_order: nil,
              units: nil, estimator: :mean, n_boot: 1000, random: nil,
              sort: true, err_style: :band, err_params: nil, error_bar: [:ci, 95],
              x_scale: :linear, y_scale: :linear, legend: :auto,
              x_label: nil, y_label: nil, title: nil,
              **options, &block)
  Plotters::LinePlotter.new(
    data: data,
    variables: { x: x, y: y, color: color, style: style, size: size },
    key_color: key_color,
    palette: palette,
    color_order: color_order,
    color_norm: color_norm,
    sizes: sizes,
    size_order: size_order,
    size_norm: size_norm,
    markers: markers,
    dashes: dashes,
    style_order: style_order,
    units: units,
    estimator: estimator,
    n_boot: n_boot,
    random: random,
    sort: sort,
    err_style: err_style,
    err_params: err_params,
    error_bar: error_bar,
    x_scale: x_scale,
    y_scale: y_scale,
    legend: legend,
    x_label: x_label,
    y_label: y_label,
    title: title,
    **options,
    &block
  )
end

#scatter_plot(x: nil, y: nil, color: nil, style: nil, size: nil, data: nil, key_color: nil, palette: nil, color_order: nil, color_norm: nil, sizes: nil, size_order: nil, size_norm: nil, markers: true, style_order: nil, alpha: nil, legend: :auto, x_label: nil, y_label: nil, title: nil, **options, &block) ⇒ Object

Scatter plot

Parameters:

  • x (vector-like object, key in data) (defaults to: nil)
  • y (vector-like object, key in data) (defaults to: nil)
  • color (vector-like object, key in data) (defaults to: nil)
  • style (vector-like object, key in data) (defaults to: nil)
  • size (vector-like object, key in data) (defaults to: nil)
  • data (table-like object) (defaults to: nil)
  • key_color (color object) (defaults to: nil)
  • palette (String, Array<Numeric>, Palette) (defaults to: nil)
  • color_order (Array<String>, Array<Symbol>) (defaults to: nil)
  • color_norm (defaults to: nil)
  • sizes (Array, Hash) (defaults to: nil)
  • size_order (Array) (defaults to: nil)
  • size_norm (defaults to: nil)
  • markers (true, false, Array, Hash) (defaults to: true)
  • style_order (Array) (defaults to: nil)
  • alpha (scalar number) (defaults to: nil)

    Propotional opacity of the points.

  • legend (:auto, :brief, :full, false) (defaults to: :auto)

    How to draw legend. If :brief, numeric color and size variables will be represented with a sample of evenly spaced values. If :full, every group will get an entry in the legend. If :auto, choose between brief or full representation based on number of levels. If false, no legend data is added and no legend is drawn.

  • x_label (String, Symbol, #to_str, nil) (defaults to: nil)

    X-axis label.

  • y_label (String, Symbol, #to_str, nil) (defaults to: nil)

    Y-axis label.

  • title (String, Symbol, #to_str, nil) (defaults to: nil)

    Title text.



255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/charty/plot_methods.rb', line 255

def scatter_plot(x: nil, y: nil, color: nil, style: nil, size: nil,
                 data: nil, key_color: nil, palette: nil, color_order: nil,
                 color_norm: nil, sizes: nil, size_order: nil, size_norm: nil,
                 markers: true, style_order: nil, alpha: nil, legend: :auto,
                 x_label: nil, y_label: nil, title: nil, **options, &block)
  Plotters::ScatterPlotter.new(
    data: data,
    variables: { x: x, y: y, color: color, style: style, size: size },
    key_color: key_color,
    palette: palette,
    color_order: color_order,
    color_norm: color_norm,
    sizes: sizes,
    size_order: size_order,
    size_norm: size_norm,
    markers: markers,
    style_order: style_order,
    alpha: alpha,
    legend: legend,
    x_label: x_label,
    y_label: y_label,
    title: title,
    **options,
    &block
  )
end