class Charty::Plotters::DistributionPlotter

Attributes

color_norm[R]
input_format[R]
legend[R]
plot_data[R]
var_types[R]
variables[R]
weights[R]

Public Class Methods

new(data:, variables:, **options, &block) click to toggle source
Calls superclass method Charty::Plotters::AbstractPlotter::new
# File lib/charty/plotters/distribution_plotter.rb, line 17
def initialize(data:, variables:, **options, &block)
  x, y, color = variables.values_at(:x, :y, :color)
  super(x, y, color, data: data, **options, &block)

  setup_variables
end

Public Instance Methods

color_norm=(val) click to toggle source
# File lib/charty/plotters/distribution_plotter.rb, line 34
def color_norm=(val)
  unless val.nil?
    raise NotImplementedError,
          "Specifying color_norm is not supported yet"
  end
end
flat_structure() click to toggle source
# File lib/charty/plotters/distribution_plotter.rb, line 4
def flat_structure
  {
    x: :@values
  }
end
legend=(val) click to toggle source
# File lib/charty/plotters/distribution_plotter.rb, line 43
def legend=(val)
  @legend = check_legend(val)
end
weights=(val) click to toggle source
# File lib/charty/plotters/distribution_plotter.rb, line 26
def weights=(val)
  @weights = check_dimension(val, :weights)
end
wide_structure() click to toggle source
# File lib/charty/plotters/distribution_plotter.rb, line 10
def wide_structure
  {
    x: :@values,
    color: :@columns
  }
end

Private Instance Methods

check_legend(val) click to toggle source
# File lib/charty/plotters/distribution_plotter.rb, line 47
        def check_legend(val)
  check_boolean(val, :legend)
end
map_color(palette: nil, order: nil, norm: nil) click to toggle source
# File lib/charty/plotters/distribution_plotter.rb, line 167
        def map_color(palette: nil, order: nil, norm: nil)
  @color_mapper = ColorMapper.new(self, palette, order, norm)
end
map_size(sizes: nil, order: nil, norm: nil) click to toggle source
# File lib/charty/plotters/distribution_plotter.rb, line 171
        def map_size(sizes: nil, order: nil, norm: nil)
  @size_mapper = SizeMapper.new(self, sizes, order, norm)
end
map_style(markers: nil, dashes: nil, order: nil) click to toggle source
# File lib/charty/plotters/distribution_plotter.rb, line 175
        def map_style(markers: nil, dashes: nil, order: nil)
  @style_mapper = StyleMapper.new(self, markers, dashes, order)
end
setup_variables() click to toggle source

This should be the same as one in RelationalPlotter TODO: move this to AbstractPlotter and refactor with CategoricalPlotter

# File lib/charty/plotters/distribution_plotter.rb, line 55
        def setup_variables
  if x.nil? && y.nil?
    @input_format = :wide
    setup_variables_with_wide_form_dataset
  else
    @input_format = :long
    setup_variables_with_long_form_dataset
  end

  @var_types = @plot_data.columns.map { |k|
    [k, variable_type(@plot_data[k], :categorical)]
  }.to_h
end
setup_variables_with_long_form_dataset() click to toggle source
# File lib/charty/plotters/distribution_plotter.rb, line 128
        def setup_variables_with_long_form_dataset
  if data.nil? || data.empty?
    @plot_data = Charty::Table.new({})
    @variables = {}
    return
  end

  plot_data = {}
  variables = {}

  {
    x: self.x,
    y: self.y,
    color: self.color,
    weights: self.weights
  }.each do |key, val|
    next if val.nil?

    if data.column?(val)
      plot_data[key] = data[val]
      variables[key] = val
    else
      case val
      when Charty::Vector
        plot_data[key] = val
        variables[key] = val.name
      else
        raise ArgumentError,
              "Could not interpret value %p for parameter %p" % [val, key]
      end
    end
  end

  @plot_data = Charty::Table.new(plot_data)
  @variables = variables.select do |var, name|
    @plot_data[var].notnull.any?
  end
end
setup_variables_with_wide_form_dataset() click to toggle source
# File lib/charty/plotters/distribution_plotter.rb, line 69
        def setup_variables_with_wide_form_dataset
  unless color.nil?
    raise ArgumentError,
          "Unable to assign the following variables in wide-form data: color"
  end

  if data.nil? || data.empty?
    @plot_data = Charty::Table.new({})
    @variables = {}
    return
  end

  flat = data.is_a?(Charty::Vector)
  if flat
    @plot_data = {}
    @variables = {}

    [:x, :y].each do |var|
      case self.flat_structure[var]
      when :@index
        @plot_data[var] = data.index.to_a
        @variables[var] = data.index.name
      when :@values
        @plot_data[var] = data.to_a
        @variables[var] = data.name
      end
    end

    @plot_data = Charty::Table.new(@plot_data)
  else
    numeric_columns = @data.column_names.select do |cn|
      @data[cn].numeric?
    end
    wide_data = @data[numeric_columns]

    melt_params = {var_name: :@columns, value_name: :@values }
    if self.wide_structure.include?(:index)
      melt_params[:id_vars] = :@index
    end

    @plot_data = wide_data.melt(**melt_params)
    @variables = {}
    self.wide_structure.each do |var, attr|
      @plot_data[var] = @plot_data[attr]

      @variables[var] = case attr
                        when :@columns
                          wide_data.columns.name
                        when :@index
                          wide_data.index.name
                        else
                          nil
                        end
    end

    @plot_data = @plot_data[self.wide_structure.keys]
  end
end