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, **options, &block) ⇒ Object

Show the given data as rectangular bars.

Parameters:

  • (defaults to: nil)

    x-dimension input for plotting long-form data.

  • (defaults to: nil)

    y-dimension input for plotting long-form data.

  • (defaults to: nil)

    color-dimension input for plotting long-form data.

  • (defaults to: nil)

    Dataset for plotting.

  • (defaults to: nil)

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

  • (defaults to: nil)

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

  • (defaults to: :mean)

    Statistical function to estimate withint each categorical bin.

  • (defaults to: 95)

    Size of confidence intervals to draw around estimated values.

  • (defaults to: 1000)

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

  • (defaults to: nil)

    Identifier of sampling unit.

  • (defaults to: nil)

    Random seed or random number generator for reproducible bootstrapping.

  • (defaults to: nil)

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

  • (defaults to: nil)

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

  • (defaults to: nil)

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

  • Propotion of the original saturation to draw colors.

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

    Color for the lines that represent the confidence intervals.

  • (defaults to: nil)

    Thickness of error bar lines (and caps).

  • (defaults to: nil)

    Width of the caps on error bars.

  • (defaults to: true)

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



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/charty/plot_methods.rb', line 26

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, **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,
    **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, **options, &block) ⇒ Object

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

Parameters:

  • (defaults to: nil)

    X-dimension input for plotting long-Form data.

  • (defaults to: nil)

    Y-dimension input for plotting long-form data.

  • (defaults to: nil)

    Color-dimension input for plotting long-form data.

  • (defaults to: nil)

    Dataset for plotting.

  • (defaults to: nil)

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

  • (defaults to: nil)

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

  • (defaults to: nil)

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

  • (defaults to: nil)

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

  • (defaults to: nil)

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

  • Propotion of the original saturation to draw colors.

  • 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.

  • (defaults to: true)

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

  • (defaults to: 5)

    Size of the markers used to indicate outlier observations.

  • (defaults to: nil)

    Width of the gray lines that frame the plot elements.

  • (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.



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/charty/plot_methods.rb', line 109

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, **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,
    **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, **options, &block) ⇒ Object



43
44
45
46
47
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
# File 'lib/charty/plot_methods.rb', line 43

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, **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,
    **options
  ) do |plotter|
    plotter.value_label = "count"
    block.(plotter) unless block.nil?
  end
end

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



252
253
254
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
281
282
283
284
285
286
287
288
289
290
# File 'lib/charty/plot_methods.rb', line 252

def hist_plot(data: nil, x: nil, y: nil, color: nil,
              stat: :count, bins: :auto,
              key_color: nil, palette: nil, color_order: nil, color_norm: nil,
              legend: true, **options, &block)
  # TODO: support following arguments
  # - wiehgts
  # - binwidth
  # - binrange
  # - discrete
  # - cumulative
  # - common_bins
  # - 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 },
    stat: stat,
    bins: bins,
    key_color: key_color,
    palette: palette,
    color_order: color_order,
    color_norm: color_norm,
    legend: legend,
    **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, **options, &block) ⇒ Object

Line plot

Parameters:

  • (defaults to: nil)
  • (defaults to: nil)
  • (defaults to: nil)
  • (defaults to: nil)
  • (defaults to: nil)
  • (defaults to: nil)
  • (defaults to: nil)
  • (defaults to: nil)
  • (defaults to: nil)
  • (defaults to: nil)
  • (defaults to: nil)
  • (defaults to: nil)
  • (defaults to: nil)
  • (defaults to: true)
  • (defaults to: nil)
  • (defaults to: nil)
  • (defaults to: nil)
  • (defaults to: :mean)
  • (defaults to: 1000)
  • (defaults to: nil)
  • (defaults to: true)
  • (defaults to: :band)
  • (defaults to: nil)
  • (defaults to: [:ci, 95])
  • (defaults to: :linear)
  • (defaults to: :linear)
  • (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.



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/charty/plot_methods.rb', line 167

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, **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,
    **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, **options, &block) ⇒ Object

Scatter plot

Parameters:

  • (defaults to: nil)
  • (defaults to: nil)
  • (defaults to: nil)
  • (defaults to: nil)
  • (defaults to: nil)
  • (defaults to: nil)
  • (defaults to: nil)
  • (defaults to: nil)
  • (defaults to: nil)
  • (defaults to: nil)
  • (defaults to: nil)
  • (defaults to: nil)
  • (defaults to: nil)
  • (defaults to: true)
  • (defaults to: nil)
  • (defaults to: nil)

    Propotional opacity of the points.

  • (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.



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/charty/plot_methods.rb', line 228

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,
                 **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,
    **options,
    &block
  )
end