galaaz 2.1.7 → 2.1.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +39 -0
- data/Rakefile +20 -2
- data/bin/check_gemfile_lock_version +46 -0
- data/bin/release_bump +26 -0
- data/blogs/README.md +4 -0
- data/blogs/galaaz_2_0/galaaz_2_0.Rmd +385 -0
- data/blogs/galaaz_2_0/galaaz_2_0.md +409 -0
- data/blogs/galaaz_2_0/galaaz_2_0.tex +756 -0
- data/blogs/galaaz_2_0/images/galaaz-header.png +0 -0
- data/blogs/galaaz_2_0/images/galaaz-lockup-stacked.png +0 -0
- data/blogs/galaaz_ggplot/galaaz_ggplot.Rmd +76 -58
- data/blogs/galaaz_ggplot/galaaz_ggplot.md +188 -161
- data/blogs/galaaz_ggplot/galaaz_ggplot.tex +185 -117
- data/blogs/galaaz_ggplot/galaaz_ggplot_files/figure-html/midwest_rb.png +0 -0
- data/blogs/galaaz_ggplot/galaaz_ggplot_files/figure-html/scatter_plot_rb.png +0 -0
- data/blogs/galaaz_ggplot/galaaz_ggplot_files/figure-markdown_github/midwest_rb.png +0 -0
- data/blogs/galaaz_ggplot/galaaz_ggplot_files/figure-markdown_github/scatter_plot_rb.png +0 -0
- data/blogs/galaaz_ggplot/images/galaaz-lockup-stacked.png +0 -0
- data/blogs/gknit/gknit.Rmd +48 -28
- data/blogs/gknit/gknit.md +60 -43
- data/blogs/gknit/gknit.tex +1409 -0
- data/blogs/gknit/gknit_files/figure-html/bubble-1.png +0 -0
- data/blogs/gknit/gknit_files/figure-html/diverging_bar.png +0 -0
- data/blogs/gknit/gknit_files/figure-latex/bubble-1.png +0 -0
- data/blogs/gknit/images/galaaz-lockup-stacked.png +0 -0
- data/blogs/manual/images/galaaz-lockup-stacked.png +0 -0
- data/blogs/manual/manual.Rmd +161 -71
- data/blogs/manual/manual.md +573 -482
- data/blogs/manual/manual.tex +1113 -517
- data/blogs/manual/manual_files/figure-html/bubble-1.png +0 -0
- data/blogs/manual/manual_files/figure-latex/bubble-1.png +0 -0
- data/blogs/manual/manual_files/figure-markdown_github/bubble-1.png +0 -0
- data/blogs/manual/manual_files/figure-markdown_github/diverging_bar.png +0 -0
- data/blogs/nse_dplyr/images/galaaz-lockup-stacked.png +0 -0
- data/blogs/nse_dplyr/nse_dplyr.Rmd +42 -8
- data/blogs/nse_dplyr/nse_dplyr.md +69 -50
- data/blogs/nse_dplyr/nse_dplyr.tex +1626 -0
- data/blogs/oh_my/images/galaaz-lockup-stacked.png +0 -0
- data/blogs/oh_my/oh_my.Rmd +206 -55
- data/blogs/oh_my/oh_my.md +247 -96
- data/blogs/oh_my/oh_my.tex +2038 -93
- data/blogs/r_on_rails_ledger/images/00_portfolio_page.png +0 -0
- data/blogs/r_on_rails_ledger/images/01_results_panel.png +0 -0
- data/blogs/r_on_rails_ledger/images/02_density_tail_risk.png +0 -0
- data/blogs/r_on_rails_ledger/images/03_mc_cone.png +0 -0
- data/blogs/r_on_rails_ledger/images/04_rolling_var.png +0 -0
- data/blogs/r_on_rails_ledger/images/galaaz-lockup-stacked.png +0 -0
- data/blogs/r_on_rails_ledger/r_on_rails_ledger.Rmd +354 -0
- data/blogs/r_on_rails_ledger/r_on_rails_ledger.md +365 -0
- data/blogs/r_on_rails_ledger/r_on_rails_ledger.tex +670 -0
- data/blogs/ruby_plot/images/galaaz-lockup-stacked.png +0 -0
- data/blogs/ruby_plot/ruby_plot.Rmd +56 -35
- data/blogs/ruby_plot/ruby_plot.md +63 -47
- data/blogs/ruby_plot/ruby_plot.tex +194 -125
- data/blogs/ruby_plot/ruby_plot_files/figure-html/dose_len.png +0 -0
- data/blogs/ruby_plot/ruby_plot_files/figure-html/facet_by_delivery.png +0 -0
- data/blogs/ruby_plot/ruby_plot_files/figure-html/facet_by_dose.png +0 -0
- data/blogs/ruby_plot/ruby_plot_files/figure-html/facets_by_delivery_color.png +0 -0
- data/blogs/ruby_plot/ruby_plot_files/figure-html/facets_by_delivery_color2.png +0 -0
- data/blogs/ruby_plot/ruby_plot_files/figure-html/facets_with_decorations.png +0 -0
- data/blogs/ruby_plot/ruby_plot_files/figure-html/facets_with_jitter.png +0 -0
- data/blogs/ruby_plot/ruby_plot_files/figure-html/facets_with_points.png +0 -0
- data/blogs/ruby_plot/ruby_plot_files/figure-html/final_box_plot.png +0 -0
- data/blogs/ruby_plot/ruby_plot_files/figure-html/final_violin_plot.png +0 -0
- data/blogs/ruby_plot/ruby_plot_files/figure-html/violin_with_jitter.png +0 -0
- data/blogs/ruby_plot/ruby_plot_files/figure-latex/dose_len.png +0 -0
- data/blogs/ruby_plot/ruby_plot_files/figure-latex/facet_by_delivery.png +0 -0
- data/blogs/ruby_plot/ruby_plot_files/figure-latex/facet_by_dose.png +0 -0
- data/blogs/ruby_plot/ruby_plot_files/figure-latex/facets_by_delivery_color.png +0 -0
- data/blogs/ruby_plot/ruby_plot_files/figure-latex/facets_by_delivery_color2.png +0 -0
- data/blogs/ruby_plot/ruby_plot_files/figure-latex/facets_with_decorations.png +0 -0
- data/blogs/ruby_plot/ruby_plot_files/figure-latex/facets_with_jitter.png +0 -0
- data/blogs/ruby_plot/ruby_plot_files/figure-latex/facets_with_points.png +0 -0
- data/blogs/ruby_plot/ruby_plot_files/figure-latex/final_box_plot.png +0 -0
- data/blogs/ruby_plot/ruby_plot_files/figure-latex/final_violin_plot.png +0 -0
- data/blogs/ruby_plot/ruby_plot_files/figure-latex/violin_with_jitter.png +0 -0
- data/blogs/ruby_plot/ruby_plot_files/ruby_plot_files/figure-latex/dose_len.png +0 -0
- data/blogs/ruby_plot/ruby_plot_files/ruby_plot_files/figure-latex/facet_by_delivery.png +0 -0
- data/blogs/ruby_plot/ruby_plot_files/ruby_plot_files/figure-latex/facet_by_dose.png +0 -0
- data/blogs/ruby_plot/ruby_plot_files/ruby_plot_files/figure-latex/facets_by_delivery_color.png +0 -0
- data/blogs/ruby_plot/ruby_plot_files/ruby_plot_files/figure-latex/facets_by_delivery_color2.png +0 -0
- data/blogs/ruby_plot/ruby_plot_files/ruby_plot_files/figure-latex/facets_with_decorations.png +0 -0
- data/blogs/ruby_plot/ruby_plot_files/ruby_plot_files/figure-latex/facets_with_jitter.png +0 -0
- data/blogs/ruby_plot/ruby_plot_files/ruby_plot_files/figure-latex/facets_with_points.png +0 -0
- data/blogs/ruby_plot/ruby_plot_files/ruby_plot_files/figure-latex/final_box_plot.png +0 -0
- data/blogs/ruby_plot/ruby_plot_files/ruby_plot_files/figure-latex/final_violin_plot.png +0 -0
- data/blogs/ruby_plot/ruby_plot_files/ruby_plot_files/figure-latex/violin_with_jitter.png +0 -0
- data/lib/galaaz/cli.rb +98 -13
- data/logos/icon-font/README.md +27 -0
- data/logos/icon-font/build_font.py +130 -0
- data/logos/icon-font/galaaz-mark.svg +34 -0
- data/script/omarchy/README.md +9 -2
- data/script/omarchy/fonts/galaaz.ttf +0 -0
- data/script/omarchy/galaaz-guide.sh +1 -1
- data/script/omarchy/install-galaaz.sh +8 -1
- data/script/omarchy/omarchy-menu.jsonc +21 -9
- data/sty/galaaz-header.png +0 -0
- data/sty/galaaz-headers-from-p3.tex +4 -0
- data/sty/galaaz.sty +76 -23
- data/version.rb +1 -1
- metadata +46 -7
- data/blogs/galaaz_ggplot/galaaz_ggplot.log +0 -754
- data/blogs/manual/manual.log +0 -1786
- data/blogs/nse_dplyr/nse_dplyr.log +0 -928
- data/blogs/oh_my/oh_my.log +0 -804
- data/blogs/ruby_plot/ruby_plot.log +0 -885
|
@@ -1,36 +1,65 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "Ruby Plotting with Galaaz"
|
|
3
|
+
subtitle: "An example of tightly coupling Ruby and R (JRuby or CRuby + GNU R, Galaaz 2.0)"
|
|
4
|
+
author: "Rodrigo Botafogo"
|
|
5
|
+
tags: [Galaaz, Ruby, R, JRuby, CRuby, "GNU R", ggplot2]
|
|
6
|
+
date: "16 October 2018 (narrative updated for Galaaz 2.0, 2026)"
|
|
7
|
+
output:
|
|
8
|
+
html_document:
|
|
9
|
+
self_contained: true
|
|
10
|
+
keep_md: true
|
|
11
|
+
toc: true
|
|
12
|
+
toc_float: true
|
|
13
|
+
toc_depth: 2
|
|
14
|
+
number_sections: true
|
|
15
|
+
includes:
|
|
16
|
+
before_body: _logo_before_body.html
|
|
17
|
+
pdf_document:
|
|
18
|
+
includes:
|
|
19
|
+
in_header:
|
|
20
|
+
- "../../sty/galaaz.sty"
|
|
21
|
+
- "../../sty/galaaz-headers-from-p3.tex"
|
|
22
|
+
keep_tex: yes
|
|
23
|
+
number_sections: yes
|
|
24
|
+
toc: true
|
|
25
|
+
toc_depth: 2
|
|
26
|
+
md_document:
|
|
27
|
+
variant: markdown_github
|
|
28
|
+
fontsize: 11pt
|
|
29
|
+
---
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
|
|
1
35
|
# Introduction
|
|
2
36
|
|
|
3
|
-
Galaaz is a system for tightly coupling Ruby and R.
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
**
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
An **earlier** Galaaz prototype used Oracle’s **GraalVM** with
|
|
19
|
-
**TruffleRuby** and **FastR** in one JVM. That stack is **historical**;
|
|
20
|
-
today’s documentation and tooling assume **NewBridge on JRuby or CRuby**
|
|
37
|
+
Galaaz is a system for tightly coupling Ruby and R. Ruby is a powerful language, with
|
|
38
|
+
a large community, a very large set of libraries and great for web development. However,
|
|
39
|
+
it lacks libraries for data science, statistics, scientific plotting and machine learning.
|
|
40
|
+
On the other hand, R is considered one of the most powerful languages for solving all of the
|
|
41
|
+
above problems. **Python** is a strong competitor: NumPy, pandas, SciPy, and scikit-learn are
|
|
42
|
+
widely used examples among **many thousands** of packages on PyPI for numerical and ML work.
|
|
43
|
+
|
|
44
|
+
With Galaaz we do not intend to re-implement any of the scientific libraries in R; we allow
|
|
45
|
+
for very tight coupling between the two languages to the point that the Ruby developer does
|
|
46
|
+
not need to think about R syntax for every call. **Galaaz 2.0** does this with **[JRuby](https://www.jruby.org/)**
|
|
47
|
+
or **CRuby** and **GNU R**: a **bridge** evaluates R from Ruby and exchanges data between processes.
|
|
48
|
+
|
|
49
|
+
An **earlier** Galaaz prototype used Oracle’s **GraalVM** with **TruffleRuby** and **FastR** in one JVM.
|
|
50
|
+
That stack is **historical**; today’s documentation and tooling assume **NewBridge on JRuby or CRuby**
|
|
21
51
|
(see the project manual and **`bin/galaaz-ruby`** / **`bin/gknit`**).
|
|
22
52
|
|
|
23
53
|
For background on the old stack:
|
|
24
54
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
FastR](https://medium.com/graalvm/faster-r-with-fastr-4b8db0e0dceb)
|
|
55
|
+
* [GraalVM Home](https://www.graalvm.org/)
|
|
56
|
+
* [TruffleRuby](https://github.com/oracle/truffleruby)
|
|
57
|
+
* [FastR](https://github.com/oracle/fastr)
|
|
58
|
+
* [Faster R with FastR](https://medium.com/graalvm/faster-r-with-fastr-4b8db0e0dceb)
|
|
30
59
|
|
|
31
60
|
## What does Galaaz mean
|
|
32
61
|
|
|
33
|
-
Galaaz is the Portuguese name for
|
|
62
|
+
Galaaz is the Portuguese name for "Galahad". From Wikipedia:
|
|
34
63
|
|
|
35
64
|
Sir Galahad (sometimes referred to as Galeas or Galath),
|
|
36
65
|
in Arthurian legend, is a knight of King Arthur's Round Table and one
|
|
@@ -47,57 +76,53 @@ Galaaz is the Portuguese name for “Galahad”. From Wikipedia:
|
|
|
47
76
|
|
|
48
77
|
## Prerequisites (Galaaz 2.0)
|
|
49
78
|
|
|
50
|
-
|
|
51
|
-
|
|
79
|
+
* **JRuby** and a compatible **JDK**, *or* **CRuby 3.3+**
|
|
80
|
+
* **GNU R** on your `PATH`
|
|
52
81
|
|
|
53
|
-
The following R packages will be automatically installed when necessary,
|
|
54
|
-
|
|
82
|
+
The following R packages will be automatically installed when necessary, but could be installed prior
|
|
83
|
+
to the demo if desired:
|
|
55
84
|
|
|
56
|
-
|
|
57
|
-
|
|
85
|
+
* ggplot2
|
|
86
|
+
* gridExtra
|
|
58
87
|
|
|
59
|
-
Installation of R packages requires a development environment.
|
|
60
|
-
|
|
61
|
-
needed on the Mac.
|
|
88
|
+
Installation of R packages requires a development environment. On Linux, a typical build toolchain
|
|
89
|
+
(GCC, headers) is usually enough. On macOS, Apple’s **Xcode Command Line Tools** are commonly required.
|
|
62
90
|
|
|
63
|
-
In order to run the
|
|
91
|
+
In order to run the 'specs' the following Ruby package is necessary:
|
|
64
92
|
|
|
65
|
-
|
|
93
|
+
* gem install rspec
|
|
66
94
|
|
|
67
95
|
## Preparation
|
|
68
96
|
|
|
69
|
-
|
|
97
|
+
* gem install galaaz
|
|
70
98
|
|
|
71
99
|
## Running the demo
|
|
72
100
|
|
|
73
|
-
The ggplot for this
|
|
74
|
-
<http://r-statistics.co/Top50-Ggplot2-Visualizations-MasterList-R-Code.html>.
|
|
101
|
+
The ggplot examples for this demo were adapted from: http://r-statistics.co/Top50-Ggplot2-Visualizations-MasterList-R-Code.html.
|
|
75
102
|
|
|
76
|
-
|
|
103
|
+
At the shell, from a suitable Galaaz environment:
|
|
77
104
|
|
|
78
|
-
|
|
105
|
+
galaaz master_list:scatter_plot
|
|
79
106
|
|
|
80
107
|
## Running other demos
|
|
81
108
|
|
|
82
|
-
|
|
109
|
+
Running
|
|
83
110
|
|
|
84
|
-
|
|
111
|
+
galaaz -T
|
|
85
112
|
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
example just do ‘galaaz sthda:bar’. Doing ‘galaaz sthda:all’ will run
|
|
90
|
-
all demos in the sthda cathegory. Some of the examples require ‘rspec’
|
|
91
|
-
do be available. To install ‘rspec’ just do ‘gem install rspec’.
|
|
113
|
+
lists available demo tasks. To run a demo, use **`galaaz`** where you would otherwise invoke **`rake`**.
|
|
114
|
+
For example, if the list shows `rake sthda:bar`, run `galaaz sthda:bar`. To run every demo in the **sthda**
|
|
115
|
+
category, use `galaaz sthda:all`. Some examples require **rspec**; install it with `gem install rspec`.
|
|
92
116
|
|
|
93
117
|
# The demo code
|
|
94
118
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
If the example is
|
|
98
|
-
generate an
|
|
99
|
-
|
|
100
|
-
|
|
119
|
+
|
|
120
|
+
The following is the Ruby code and plot for the above example. There is a small difference between
|
|
121
|
+
the code in the example and the code below. If the example is **run**, the plot will appear on the
|
|
122
|
+
screen; below, we generate an SVG image and then include it in this document. In order to
|
|
123
|
+
generate and image, the R.svg device is used. To generate the plot on the screen, use the R.awt
|
|
124
|
+
device, as commented on the code.
|
|
125
|
+
|
|
101
126
|
|
|
102
127
|
``` ruby
|
|
103
128
|
require 'galaaz'
|
|
@@ -124,10 +149,13 @@ gg = midwest.ggplot(E.aes(x: :area, y: :poptotal)) +
|
|
|
124
149
|
puts gg
|
|
125
150
|
```
|
|
126
151
|
|
|
127
|
-
|
|
152
|
+
|
|
153
|
+

|
|
154
|
+
|
|
128
155
|
|
|
129
156
|
In R, the code to generate this plot is the following
|
|
130
157
|
|
|
158
|
+
|
|
131
159
|
``` r
|
|
132
160
|
# install.packages("ggplot2")
|
|
133
161
|
# load package and data
|
|
@@ -152,87 +180,88 @@ gg <- ggplot(midwest, aes(x=area, y=poptotal)) +
|
|
|
152
180
|
plot(gg)
|
|
153
181
|
```
|
|
154
182
|
|
|
155
|
-
Note that both codes are very similar.
|
|
156
|
-
|
|
157
|
-
becomes
|
|
158
|
-
size = popdensity), become in Ruby (col: :state, size: :popdensity).
|
|
159
|
-
|
|
160
|
-
One last
|
|
161
|
-
function.
|
|
162
|
-
explanation of why E.aes is needed is an advanced topic in R and
|
|
163
|
-
on what is know as Non-standard Evaluation (NSE) in R.
|
|
164
|
-
|
|
165
|
-
geom_point
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
order to delay the evaluation of function aes we need to use E.aes. The
|
|
171
|
-
interested reader on NSE in R is directed to
|
|
172
|
-
<http://adv-r.had.co.nz/Computing-on-the-language.html>.
|
|
183
|
+
Note that both codes are very similar. The Ruby code requires the use of "R." before calling
|
|
184
|
+
any functions,
|
|
185
|
+
for instance R function 'geom_point' becomes 'R.geom_point' in Ruby. R named parameters such as
|
|
186
|
+
(col = state, size = popdensity), become in Ruby (col: :state, size: :popdensity).
|
|
187
|
+
|
|
188
|
+
One last
|
|
189
|
+
point that needs to be observed is the call to the 'aes' function. In Ruby instead of doing
|
|
190
|
+
'R.aes', we use 'E.aes'. The explanation of why E.aes is needed is an advanced topic in R and
|
|
191
|
+
depends on what is know as Non-standard Evaluation (NSE) in R. In short, function 'aes' is lazily
|
|
192
|
+
evaluated in R, i.e., in R when calling geom_point(aes(col=state, size=popdensity)), function
|
|
193
|
+
geom_point receives as argument something similar to a string containing
|
|
194
|
+
'aes(col=state, size=popdensity)', and the aes function will be evaluated inside the geom_point
|
|
195
|
+
function. In Ruby, there is no Lazy evaluation and doing R.aes would try to evaluate aes
|
|
196
|
+
immediately. In order to delay the evaluation of function aes we need to use E.aes. The
|
|
197
|
+
interested reader on NSE in R is directed to http://adv-r.had.co.nz/Computing-on-the-language.html.
|
|
173
198
|
|
|
174
199
|
# An extension to the example
|
|
175
200
|
|
|
176
|
-
If both codes are so similar, then why would one use Ruby instead of R
|
|
177
|
-
|
|
201
|
+
If both codes are so similar, then why would one use Ruby instead of R and what good is galaaz
|
|
202
|
+
after all?
|
|
178
203
|
|
|
179
|
-
Ruby is a modern OO language with numerous very useful constructs such
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
constructs.
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
204
|
+
Ruby is a modern OO language with numerous very useful constructs such as classes, modules, blocks,
|
|
205
|
+
procs, etc. The example above focus on the coupling of both languages, and does not show the
|
|
206
|
+
use of other Ruby constructs. In the following example, we will show a more complex example using
|
|
207
|
+
other Ruby constructs. This is certainly not a very well written and robust Ruby code, but
|
|
208
|
+
it give the idea of how Ruby and R are strongly coupled.
|
|
209
|
+
|
|
210
|
+
Let's imagine that we work in a corporation that has its plot themes. So, it has defined a
|
|
211
|
+
'CorpTheme' module. Plots in this corporation should not have grids, numbers in labels should
|
|
212
|
+
not use scientific notation and the preferred color is blue.
|
|
186
213
|
|
|
187
|
-
Let’s imagine that we work in a corporation that has its plot themes.
|
|
188
|
-
So, it has defined a ‘CorpTheme’ module. Plots in this corporation
|
|
189
|
-
should not have grids, numbers in labels should not use scientific
|
|
190
|
-
notation and the preferred color is blue.
|
|
191
214
|
|
|
192
215
|
``` ruby
|
|
193
216
|
# corp_theme.rb
|
|
194
217
|
# defines the corporate theme for all plots
|
|
195
|
-
|
|
218
|
+
|
|
196
219
|
module CorpTheme
|
|
197
220
|
|
|
198
|
-
|
|
199
|
-
#
|
|
200
|
-
#
|
|
201
|
-
|
|
202
|
-
|
|
221
|
+
# ---------------------------------------------------------------
|
|
222
|
+
# Plot theme: no major/minor grids, borders, or background;
|
|
223
|
+
# turn off scientific notation.
|
|
224
|
+
# ---------------------------------------------------------------
|
|
225
|
+
|
|
203
226
|
def self.global_theme
|
|
204
|
-
|
|
227
|
+
|
|
205
228
|
R.options(scipen: 999) # turn-off scientific notation like 1e+48
|
|
206
|
-
|
|
229
|
+
|
|
207
230
|
# remove major grids
|
|
208
|
-
global_theme =
|
|
231
|
+
global_theme =
|
|
232
|
+
R.theme(panel__grid__major: E.element_blank())
|
|
209
233
|
# remove minor grids
|
|
210
|
-
global_theme = global_theme +
|
|
234
|
+
global_theme = global_theme +
|
|
235
|
+
R.theme(panel__grid__minor: E.element_blank)
|
|
211
236
|
# remove border
|
|
212
|
-
global_theme = global_theme +
|
|
237
|
+
global_theme = global_theme +
|
|
238
|
+
R.theme(panel__border: E.element_blank)
|
|
213
239
|
# remove background
|
|
214
|
-
global_theme = global_theme +
|
|
240
|
+
global_theme = global_theme +
|
|
241
|
+
R.theme(panel__background: E.element_blank)
|
|
215
242
|
# Change axis font
|
|
216
243
|
global_theme = global_theme +
|
|
217
|
-
|
|
244
|
+
R.theme(axis__text: E.element_text(
|
|
245
|
+
size: 8, color: "#000080"))
|
|
218
246
|
# change color of axis titles
|
|
219
247
|
global_theme = global_theme +
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
248
|
+
R.theme(axis__title: E.element_text(
|
|
249
|
+
color: "#000080",
|
|
250
|
+
face: "bold",
|
|
251
|
+
size: 8,
|
|
252
|
+
hjust: 1))
|
|
225
253
|
end
|
|
226
|
-
|
|
254
|
+
|
|
227
255
|
end
|
|
228
256
|
```
|
|
229
257
|
|
|
230
258
|
We now define a ScatterPlot class:
|
|
231
259
|
|
|
260
|
+
|
|
232
261
|
``` ruby
|
|
233
262
|
# ScatterPlot.rb
|
|
234
263
|
# creates a scatter plot and allow some configuration
|
|
235
|
-
|
|
264
|
+
|
|
236
265
|
class ScatterPlot
|
|
237
266
|
|
|
238
267
|
attr_accessor :title
|
|
@@ -240,55 +269,55 @@ class ScatterPlot
|
|
|
240
269
|
attr_accessor :caption
|
|
241
270
|
attr_accessor :x_label
|
|
242
271
|
attr_accessor :y_label
|
|
243
|
-
|
|
244
|
-
|
|
272
|
+
|
|
273
|
+
# ---------------------------------------------------------------
|
|
245
274
|
# Initialize the plot with the data and the x and y variables
|
|
246
|
-
|
|
275
|
+
# ---------------------------------------------------------------
|
|
247
276
|
|
|
248
277
|
def initialize(data, x:, y:)
|
|
249
278
|
@data = data
|
|
250
279
|
@x = x
|
|
251
280
|
@y = y
|
|
252
281
|
end
|
|
253
|
-
|
|
254
|
-
|
|
282
|
+
|
|
283
|
+
# ---------------------------------------------------------------
|
|
255
284
|
# Define groupings by color and size
|
|
256
|
-
|
|
285
|
+
# ---------------------------------------------------------------
|
|
257
286
|
|
|
258
287
|
def group_by(color:, size:)
|
|
259
288
|
@color_by = color
|
|
260
289
|
@size_by = size
|
|
261
290
|
end
|
|
262
291
|
|
|
263
|
-
|
|
264
|
-
# Add a smoothing line
|
|
265
|
-
#
|
|
266
|
-
#---------------------------------------------------------------------------------
|
|
292
|
+
# ---------------------------------------------------------------
|
|
293
|
+
# Add a smoothing line; confidence: true draws a CI band.
|
|
294
|
+
# ---------------------------------------------------------------
|
|
267
295
|
|
|
268
296
|
def add_smoothing_line(method:, confidence: true)
|
|
269
297
|
@method = method
|
|
270
298
|
@confidence = confidence
|
|
271
299
|
end
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
#
|
|
275
|
-
# @param title [String]
|
|
276
|
-
# @return
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
def graph_params(title: "", subtitle: "", caption: "",
|
|
300
|
+
|
|
301
|
+
# ---------------------------------------------------------------
|
|
302
|
+
# Graph title / labels, formatted for this theme.
|
|
303
|
+
# @param title [String] title to add to the graph
|
|
304
|
+
# @return labs() result that can be included in a graph
|
|
305
|
+
# ---------------------------------------------------------------
|
|
306
|
+
|
|
307
|
+
def graph_params(title: "", subtitle: "", caption: "",
|
|
308
|
+
x_label: "", y_label: "")
|
|
280
309
|
R.labs(
|
|
281
|
-
title: title,
|
|
282
|
-
subtitle: subtitle,
|
|
310
|
+
title: title,
|
|
311
|
+
subtitle: subtitle,
|
|
283
312
|
caption: caption,
|
|
284
|
-
y_label: y_label,
|
|
285
|
-
x_label: x_label,
|
|
313
|
+
y_label: y_label,
|
|
314
|
+
x_label: x_label,
|
|
286
315
|
)
|
|
287
316
|
end
|
|
288
317
|
|
|
289
|
-
|
|
318
|
+
# ---------------------------------------------------------------
|
|
290
319
|
# Prepare the plot's points
|
|
291
|
-
|
|
320
|
+
# ---------------------------------------------------------------
|
|
292
321
|
|
|
293
322
|
def points
|
|
294
323
|
params = {}
|
|
@@ -296,33 +325,32 @@ class ScatterPlot
|
|
|
296
325
|
params[:size] = @size_by if @size_by
|
|
297
326
|
R.geom_point(E.aes(params))
|
|
298
327
|
end
|
|
299
|
-
|
|
300
|
-
|
|
328
|
+
|
|
329
|
+
# ---------------------------------------------------------------
|
|
301
330
|
# Plots the scatterplot
|
|
302
|
-
|
|
331
|
+
# ---------------------------------------------------------------
|
|
303
332
|
|
|
304
333
|
def plot
|
|
305
334
|
gg = @data.ggplot(E.aes(x: @x, y: @y)) +
|
|
306
|
-
points +
|
|
335
|
+
points +
|
|
307
336
|
R.geom_smooth(method: @method, se: @confidence) +
|
|
308
337
|
R.xlim(R.c(0, 0.1)) +
|
|
309
|
-
R.ylim(R.c(0, 500000)) +
|
|
338
|
+
R.ylim(R.c(0, 500000)) +
|
|
310
339
|
graph_params(title: @title,
|
|
311
|
-
subtitle: @subtitle,
|
|
312
|
-
y_label: @y_label,
|
|
313
|
-
x_label: @x_label,
|
|
340
|
+
subtitle: @subtitle,
|
|
341
|
+
y_label: @y_label,
|
|
342
|
+
x_label: @x_label,
|
|
314
343
|
caption: @caption) +
|
|
315
344
|
CorpTheme.global_theme
|
|
316
345
|
|
|
317
346
|
puts gg
|
|
318
|
-
|
|
319
347
|
end
|
|
320
|
-
|
|
348
|
+
|
|
321
349
|
end
|
|
322
350
|
```
|
|
323
351
|
|
|
324
|
-
And this is the final code for making the scatter plot with the midwest
|
|
325
|
-
|
|
352
|
+
And this is the final code for making the scatter plot with the midwest data
|
|
353
|
+
|
|
326
354
|
|
|
327
355
|
``` ruby
|
|
328
356
|
require 'galaaz'
|
|
@@ -334,31 +362,30 @@ sp.subtitle = "Area Vs Population"
|
|
|
334
362
|
sp.caption = "Source: midwest"
|
|
335
363
|
sp.x_label = "Area"
|
|
336
364
|
sp.y_label = "Population"
|
|
337
|
-
|
|
365
|
+
# try: sp.group_by(color: :state)
|
|
366
|
+
sp.group_by(color: :state, size: :popdensity)
|
|
338
367
|
# available methods: "lm", "glm", "loess", "gam"
|
|
339
|
-
sp.add_smoothing_line(method: "glm")
|
|
368
|
+
sp.add_smoothing_line(method: "glm")
|
|
340
369
|
sp.plot
|
|
341
370
|
```
|
|
342
371
|
|
|
343
|
-
|
|
372
|
+
|
|
373
|
+

|
|
344
374
|
|
|
345
375
|
# Conclusion
|
|
346
376
|
|
|
347
|
-
R is a very powerful language for statistical analysis, data analytics,
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
for statistics and **ggplot2**. Further, by using Galaaz the R developer
|
|
357
|
-
can start (slowly if needed) using Ruby’s constructs and libraries that
|
|
377
|
+
R is a very powerful language for statistical analysis, data analytics, machine learning, plotting
|
|
378
|
+
and many other scientific applications with a very large package ecosystem. However R is often
|
|
379
|
+
considered hard to learn and lacking modern language features such as object-oriented
|
|
380
|
+
classes, modules, and first-class functions. For that reason, many teams have standardized on
|
|
381
|
+
Python (or stayed entirely inside R) rather than mixing ecosystems.
|
|
382
|
+
|
|
383
|
+
With Galaaz, R programmers can almost transparently migrate from R to Ruby, since syntax is
|
|
384
|
+
almost identical and **GNU R** remains the engine for statistics and **ggplot2**. Further, by using
|
|
385
|
+
Galaaz the R developer can start (slowly if needed) using Ruby’s constructs and libraries that
|
|
358
386
|
nicely complement R packages.
|
|
359
387
|
|
|
360
|
-
For the Ruby developer, Galaaz allows the immediate use of R functions
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
to the JVM ecosystem; **CRuby** works equally for the Galaaz bridge itself.
|
|
388
|
+
For the Ruby developer, Galaaz allows the immediate use of R functions with minimal ceremony. As
|
|
389
|
+
shown in the second example above, class `ScatterPlot` hides most R call details from the Ruby
|
|
390
|
+
developer. Prefer **JRuby** when you want **real parallel threads** on the Ruby side and access to
|
|
391
|
+
the JVM ecosystem; **CRuby** works equally for the Galaaz bridge itself.
|