Class: Rust::Plots::GGPlot::PlotBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/rust/external/ggplot2/plot_builder.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = nil) ⇒ PlotBuilder

Returns a new instance of PlotBuilder.



11
12
13
14
15
16
17
18
19
20
# File 'lib/rust/external/ggplot2/plot_builder.rb', line 11

def initialize(data=nil)
    @data = data
    
    @aes_options   = {}
    @label_options = {}
    
    @current_context = :title
    
    @layers = []
end

Class Method Details

.for_dataframe(data_frame) ⇒ Object



7
8
9
# File 'lib/rust/external/ggplot2/plot_builder.rb', line 7

def self.for_dataframe(data_frame)
    return PlotBuilder.new(data_frame)
end

Instance Method Details

#buildObject



275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/rust/external/ggplot2/plot_builder.rb', line 275

def build
    plot = Plot.new(@data, Aes.new(**@aes_options))
    plot.theme = @theme if @theme
    plot << @layers if @layers.size > 0
    if @label_options.size > 0
        if @label_options.keys.include?(:group)
            value = @label_options.delete(:group)
            selected = [:x, :y] - @label_options.keys
            @label_options[selected.first] = value if selected.size == 1
        end
        
        plot << Labels.new(**@label_options)
    end
    
    return plot
end

#draw_bars(**options) ⇒ Object



177
178
179
180
181
182
183
# File 'lib/rust/external/ggplot2/plot_builder.rb', line 177

def draw_bars(**options)
    @layers << GeomBar.new(**options)
    
    @current_context = nil
    
    return self
end

#draw_boxplot(**options) ⇒ Object



193
194
195
196
197
198
199
# File 'lib/rust/external/ggplot2/plot_builder.rb', line 193

def draw_boxplot(**options)
    @layers << GeomBoxplot.new(**options)
    
    @current_context = nil
    
    return self
end

#draw_cols(**options) ⇒ Object



185
186
187
188
189
190
191
# File 'lib/rust/external/ggplot2/plot_builder.rb', line 185

def draw_cols(**options)
    @layers << GeomCol.new(**options)
    
    @current_context = nil
    
    return self
end

#draw_density(**options) ⇒ Object



209
210
211
212
213
214
215
# File 'lib/rust/external/ggplot2/plot_builder.rb', line 209

def draw_density(**options)
    @layers << GeomDensity.new(**options)
    
    @current_context = nil
    
    return self
end

#draw_histogram(**options) ⇒ Object



201
202
203
204
205
206
207
# File 'lib/rust/external/ggplot2/plot_builder.rb', line 201

def draw_histogram(**options)
    @layers << GeomHistogram.new(**options)
    
    @current_context = nil
    
    return self
end

#draw_lines(**options) ⇒ Object



169
170
171
172
173
174
175
# File 'lib/rust/external/ggplot2/plot_builder.rb', line 169

def draw_lines(**options)
    @layers << GeomLine.new(**options)
    
    @current_context = nil
    
    return self
end

#draw_points(**options) ⇒ Object



161
162
163
164
165
166
167
# File 'lib/rust/external/ggplot2/plot_builder.rb', line 161

def draw_points(**options)
    @layers << GeomPoint.new(**options)
    
    @current_context = nil
    
    return self
end

#flip_coordinatesObject



267
268
269
270
271
272
273
# File 'lib/rust/external/ggplot2/plot_builder.rb', line 267

def flip_coordinates
    @layers << FlipCoordinates.new
    
    @current_context = nil
    
    return self
end

#labeled(value) ⇒ Object



225
226
227
228
229
230
# File 'lib/rust/external/ggplot2/plot_builder.rb', line 225

def labeled(value)
    raise "No context for assigning a label" unless @current_context
    @label_options[@current_context] = value
    
    return self
end

#scale_continuous(**options) ⇒ Object



232
233
234
235
236
237
# File 'lib/rust/external/ggplot2/plot_builder.rb', line 232

def scale_continuous(**options)
    raise "No context for assigning a label" unless @current_context
    @layers << AxisScaler.new(@current_context, :continuous, **options)
    
    return self
end

#scale_discrete(**options) ⇒ Object



239
240
241
242
243
244
# File 'lib/rust/external/ggplot2/plot_builder.rb', line 239

def scale_discrete(**options)
    raise "No context for assigning a label" unless @current_context
    @layers << AxisScaler.new(@current_context, :discrete, **options)
    
    return self
end

#scale_log10(**options) ⇒ Object



246
247
248
249
250
251
# File 'lib/rust/external/ggplot2/plot_builder.rb', line 246

def scale_log10(**options)
    raise "No context for assigning a label" unless @current_context
    @layers << AxisScaler.new(@current_context, :log10, **options)
    
    return self
end

#scale_reverse(**options) ⇒ Object



253
254
255
256
257
258
# File 'lib/rust/external/ggplot2/plot_builder.rb', line 253

def scale_reverse(**options)
    raise "No context for assigning a label" unless @current_context
    @layers << AxisScaler.new(@current_context, :reverse, **options)
    
    return self
end

#scale_sqrt(**options) ⇒ Object



260
261
262
263
264
265
# File 'lib/rust/external/ggplot2/plot_builder.rb', line 260

def scale_sqrt(**options)
    raise "No context for assigning a label" unless @current_context
    @layers << AxisScaler.new(@current_context, :sqrt, **options)
    
    return self
end

#scale_x_continuous(**options) ⇒ Object



85
86
87
88
89
90
# File 'lib/rust/external/ggplot2/plot_builder.rb', line 85

def scale_x_continuous(**options)
    raise "No context for assigning a label" unless @current_context
    @layers << AxisScaler.new(:x, :continuous, **options)
    
    return self
end

#scale_x_discrete(**options) ⇒ Object



99
100
101
102
103
104
# File 'lib/rust/external/ggplot2/plot_builder.rb', line 99

def scale_x_discrete(**options)
    raise "No context for assigning a label" unless @current_context
    @layers << AxisScaler.new(:x, :discrete, **options)
    
    return self
end

#scale_x_log10(**options) ⇒ Object



113
114
115
116
117
118
# File 'lib/rust/external/ggplot2/plot_builder.rb', line 113

def scale_x_log10(**options)
    raise "No context for assigning a label" unless @current_context
    @layers << AxisScaler.new(:x, :log10, **options)
    
    return self
end

#scale_x_reverse(**options) ⇒ Object



127
128
129
130
131
132
# File 'lib/rust/external/ggplot2/plot_builder.rb', line 127

def scale_x_reverse(**options)
    raise "No context for assigning a label" unless @current_context
    @layers << AxisScaler.new(:x, :reverse, **options)
    
    return self
end

#scale_x_sqrt(**options) ⇒ Object



141
142
143
144
145
146
# File 'lib/rust/external/ggplot2/plot_builder.rb', line 141

def scale_x_sqrt(**options)
    raise "No context for assigning a label" unless @current_context
    @layers << AxisScaler.new(:x, :sqrt, **options)
    
    return self
end

#scale_y_continuous(**options) ⇒ Object



92
93
94
95
96
97
# File 'lib/rust/external/ggplot2/plot_builder.rb', line 92

def scale_y_continuous(**options)
    raise "No context for assigning a label" unless @current_context
    @layers << AxisScaler.new(:y, :continuous, **options)
    
    return self
end

#scale_y_discrete(**options) ⇒ Object



106
107
108
109
110
111
# File 'lib/rust/external/ggplot2/plot_builder.rb', line 106

def scale_y_discrete(**options)
    raise "No context for assigning a label" unless @current_context
    @layers << AxisScaler.new(:y, :discrete, **options)
    
    return self
end

#scale_y_log10(**options) ⇒ Object



120
121
122
123
124
125
# File 'lib/rust/external/ggplot2/plot_builder.rb', line 120

def scale_y_log10(**options)
    raise "No context for assigning a label" unless @current_context
    @layers << AxisScaler.new(:y, :log10, **options)
    
    return self
end

#scale_y_reverse(**options) ⇒ Object



134
135
136
137
138
139
# File 'lib/rust/external/ggplot2/plot_builder.rb', line 134

def scale_y_reverse(**options)
    raise "No context for assigning a label" unless @current_context
    @layers << AxisScaler.new(:y, :reverse, **options)
    
    return self
end

#scale_y_sqrt(**options) ⇒ Object



148
149
150
151
152
153
# File 'lib/rust/external/ggplot2/plot_builder.rb', line 148

def scale_y_sqrt(**options)
    raise "No context for assigning a label" unless @current_context
    @layers << AxisScaler.new(:y, :sqrt, **options)
    
    return self
end

#with_color(variable) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/rust/external/ggplot2/plot_builder.rb', line 49

def with_color(variable)
    variable = variable.to_sym if variable.is_a?(String)
    
    @aes_options[:color] = variable
    @current_context = :color
    
    return self
end

#with_color_label(value) ⇒ Object



79
80
81
82
83
# File 'lib/rust/external/ggplot2/plot_builder.rb', line 79

def with_color_label(value)
    @label_options[:color] = value
    
    return self
end

#with_fill(variable) ⇒ Object



58
59
60
61
62
63
64
65
# File 'lib/rust/external/ggplot2/plot_builder.rb', line 58

def with_fill(variable)
    variable = variable.to_sym if variable.is_a?(String)
    
    @aes_options[:fill] = variable
    @current_context = :fill
    
    return self
end

#with_group(variable) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/rust/external/ggplot2/plot_builder.rb', line 40

def with_group(variable)
    variable = variable.to_sym if variable.is_a?(String)
    
    @aes_options[:group] = variable
    @current_context = :group
    
    return self
end

#with_theme(theme) ⇒ Object



217
218
219
220
221
222
223
# File 'lib/rust/external/ggplot2/plot_builder.rb', line 217

def with_theme(theme)
    @layers << theme
    
    @current_context = nil
    
    return self
end

#with_title(value) ⇒ Object



155
156
157
158
159
# File 'lib/rust/external/ggplot2/plot_builder.rb', line 155

def with_title(value)
    @label_options[:title] = value
    
    return self
end

#with_x(variable, label = nil) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/rust/external/ggplot2/plot_builder.rb', line 22

def with_x(variable, label = nil)
    variable = variable.to_sym if variable.is_a?(String)
    
    @aes_options[:x] = variable
    @current_context = :x
    
    return self
end

#with_x_label(value) ⇒ Object



67
68
69
70
71
# File 'lib/rust/external/ggplot2/plot_builder.rb', line 67

def with_x_label(value)
    @label_options[:x] = value
    
    return self
end

#with_y(variable) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/rust/external/ggplot2/plot_builder.rb', line 31

def with_y(variable)
    variable = variable.to_sym if variable.is_a?(String)
    
    @aes_options[:y] = variable
    @current_context = :y
    
    return self
end

#with_y_label(value) ⇒ Object



73
74
75
76
77
# File 'lib/rust/external/ggplot2/plot_builder.rb', line 73

def with_y_label(value)
    @label_options[:y] = value
    
    return self
end