galaaz 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +32 -0
- data/Rakefile +177 -0
- data/bin/galaaz +8 -0
- data/examples/50Plots_MasterList/scatter_plot.rb +51 -0
- data/examples/baseball.csv +1 -0
- data/examples/baseball.rb +16 -0
- data/examples/ggplot.rb +178 -0
- data/examples/islr/Figure.jpg +0 -0
- data/examples/islr/all.rb +32 -0
- data/examples/islr/ch2.spec.rb +148 -0
- data/examples/islr/ch3.spec.rb +28 -0
- data/examples/islr/ch3_boston.rb +77 -0
- data/examples/islr/ch3_multiple_regression.rb +36 -0
- data/examples/islr/ch6.spec.rb +64 -0
- data/examples/paper/paper.rb +36 -0
- data/examples/sthda_ggplot/README.md +38 -0
- data/examples/sthda_ggplot/all.rb +68 -0
- data/examples/sthda_ggplot/one_variable_continuous/density_gg.rb +52 -0
- data/examples/sthda_ggplot/one_variable_continuous/geom_area.rb +61 -0
- data/examples/sthda_ggplot/one_variable_continuous/geom_density.rb +77 -0
- data/examples/sthda_ggplot/one_variable_continuous/geom_dotplot.rb +69 -0
- data/examples/sthda_ggplot/one_variable_continuous/geom_freqpoly.rb +69 -0
- data/examples/sthda_ggplot/one_variable_continuous/geom_histogram.rb +62 -0
- data/examples/sthda_ggplot/one_variable_continuous/histogram_density.rb +55 -0
- data/examples/sthda_ggplot/one_variable_continuous/stat.rb +62 -0
- data/examples/sthda_ggplot/one_variable_discrete/bar.rb +54 -0
- data/examples/sthda_ggplot/qplots/box_violin_dot.rb +57 -0
- data/examples/sthda_ggplot/qplots/scatter_plots.rb +67 -0
- data/examples/sthda_ggplot/scatter_gg.rb +60 -0
- data/examples/sthda_ggplot/two_variables_cont_bivariate/geom_bin2d.rb +49 -0
- data/examples/sthda_ggplot/two_variables_cont_bivariate/geom_density2d.rb +64 -0
- data/examples/sthda_ggplot/two_variables_cont_bivariate/geom_hex.rb +52 -0
- data/examples/sthda_ggplot/two_variables_cont_cont/geom_point.rb +65 -0
- data/examples/sthda_ggplot/two_variables_cont_cont/geom_smooth.rb +66 -0
- data/examples/sthda_ggplot/two_variables_cont_cont/misc.rb +83 -0
- data/examples/sthda_ggplot/two_variables_cont_function/geom_area.rb +63 -0
- data/examples/sthda_ggplot/two_variables_disc_cont/geom_bar.rb +85 -0
- data/examples/sthda_ggplot/two_variables_disc_cont/geom_boxplot.rb +62 -0
- data/examples/sthda_ggplot/two_variables_disc_cont/geom_dotplot.rb +75 -0
- data/examples/sthda_ggplot/two_variables_disc_cont/geom_jitter.rb +74 -0
- data/examples/sthda_ggplot/two_variables_disc_cont/geom_line.rb +55 -0
- data/examples/sthda_ggplot/two_variables_disc_cont/geom_violin.rb +70 -0
- data/examples/sthda_ggplot/two_variables_disc_disc/geom_jitter.rb +40 -0
- data/examples/sthda_ggplot/two_variables_error/geom_crossbar.rb +108 -0
- data/examples/subsetting.rb +372 -0
- data/lib/expression.rb +45 -0
- data/lib/galaaz.rb +27 -0
- data/lib/r.rb +118 -0
- data/lib/r_methods.rb +89 -0
- data/lib/rbinary_operators.rb +226 -0
- data/lib/rclosure.rb +34 -0
- data/lib/rdata_frame.rb +63 -0
- data/lib/renvironment.rb +34 -0
- data/lib/rexpression.rb +34 -0
- data/lib/rindexed_object.rb +68 -0
- data/lib/rlanguage.rb +64 -0
- data/lib/rlist.rb +72 -0
- data/lib/rmatrix.rb +38 -0
- data/lib/rmd_indexed_object.rb +43 -0
- data/lib/robject.rb +297 -0
- data/lib/rpkg.rb +53 -0
- data/lib/rsupport.rb +292 -0
- data/lib/rsupport_scope.rb +77 -0
- data/lib/rsymbol.rb +57 -0
- data/lib/ruby_callback.rb +83 -0
- data/lib/ruby_extensions.rb +74 -0
- data/lib/runary_operators.rb +58 -0
- data/lib/rvector.rb +117 -0
- data/r_requires/ggplot.rb +31 -0
- data/specs/all.rb +45 -0
- data/specs/r_dataframe.spec.rb +181 -0
- data/specs/r_eval.spec.rb +164 -0
- data/specs/r_function.spec.rb +105 -0
- data/specs/r_language.spec.rb +135 -0
- data/specs/r_list.spec.rb +129 -0
- data/specs/r_list_apply.spec.rb +99 -0
- data/specs/r_matrix.spec.rb +83 -0
- data/specs/r_vector_creation.spec.rb +99 -0
- data/specs/r_vector_functions.spec.rb +59 -0
- data/specs/r_vector_object.spec.rb +94 -0
- data/specs/r_vector_operators.spec.rb +174 -0
- data/specs/r_vector_subsetting.spec.rb +136 -0
- data/specs/tmp.rb +134 -0
- data/version.rb +2 -0
- metadata +198 -0
@@ -0,0 +1,75 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
##########################################################################################
|
4
|
+
# @author Rodrigo Botafogo
|
5
|
+
#
|
6
|
+
# Copyright © 2018 Rodrigo Botafogo. All Rights Reserved. Permission to use, copy, modify,
|
7
|
+
# and distribute this software and its documentation, without fee and without a signed
|
8
|
+
# licensing agreement, is hereby granted, provided that the above copyright notice, this
|
9
|
+
# paragraph and the following two paragraphs appear in all copies, modifications, and
|
10
|
+
# distributions.
|
11
|
+
#
|
12
|
+
# IN NO EVENT SHALL RODRIGO BOTAFOGO BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL,
|
13
|
+
# INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF
|
14
|
+
# THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF RODRIGO BOTAFOGO HAS BEEN ADVISED OF THE
|
15
|
+
# POSSIBILITY OF SUCH DAMAGE.
|
16
|
+
#
|
17
|
+
# RODRIGO BOTAFOGO SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
18
|
+
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
|
19
|
+
# SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS".
|
20
|
+
# RODRIGO BOTAFOGO HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
|
21
|
+
# OR MODIFICATIONS.
|
22
|
+
##########################################################################################
|
23
|
+
|
24
|
+
require 'galaaz'
|
25
|
+
require 'ggplot'
|
26
|
+
|
27
|
+
tooth_growth = ~:ToothGrowth
|
28
|
+
tooth_growth.dose = tooth_growth.dose.as__factor
|
29
|
+
puts tooth_growth
|
30
|
+
|
31
|
+
e = tooth_growth.ggplot(E.aes(x: :dose, y: :len))
|
32
|
+
|
33
|
+
R.awt
|
34
|
+
|
35
|
+
# Default plot
|
36
|
+
print e + R.geom_dotplot(binaxis: "y", stackdir: "center")
|
37
|
+
|
38
|
+
sleep(2)
|
39
|
+
R.grid__newpage
|
40
|
+
|
41
|
+
# Dot plot with mean points (+/- SD)
|
42
|
+
# stat_summary requires library Hmisc that does not yet install in
|
43
|
+
# graalvm
|
44
|
+
# print e + R.geom_dotplot(binaxis: "y", stackdir: "center") +
|
45
|
+
# R.stat_summary(fun__data: "mean_sdl", fun__args: E.list(mult: 1),
|
46
|
+
# geom: "pointrange", color: "red")
|
47
|
+
|
48
|
+
sleep(2)
|
49
|
+
R.grid__newpage
|
50
|
+
|
51
|
+
# Combine with box plot
|
52
|
+
print e + R.geom_boxplot +
|
53
|
+
R.geom_dotplot(binaxis: "y", stackdir: "center")
|
54
|
+
|
55
|
+
sleep(2)
|
56
|
+
R.grid__newpage
|
57
|
+
|
58
|
+
# Add violin plot
|
59
|
+
print e + R.geom_violin(trim: false) +
|
60
|
+
R.geom_dotplot(binaxis: 'y', stackdir: 'center')
|
61
|
+
|
62
|
+
sleep(2)
|
63
|
+
R.grid__newpage
|
64
|
+
|
65
|
+
# Color and fill by group (dose)
|
66
|
+
print e + R.geom_dotplot(E.aes(color: :dose, fill: :dose),
|
67
|
+
binaxis: "y", stackdir: "center")
|
68
|
+
|
69
|
+
sleep(2)
|
70
|
+
R.grid__newpage
|
71
|
+
|
72
|
+
# a = gets.chomp
|
73
|
+
|
74
|
+
# removes the window
|
75
|
+
R.dev__off
|
@@ -0,0 +1,74 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
##########################################################################################
|
4
|
+
# @author Rodrigo Botafogo
|
5
|
+
#
|
6
|
+
# Copyright © 2018 Rodrigo Botafogo. All Rights Reserved. Permission to use, copy, modify,
|
7
|
+
# and distribute this software and its documentation, without fee and without a signed
|
8
|
+
# licensing agreement, is hereby granted, provided that the above copyright notice, this
|
9
|
+
# paragraph and the following two paragraphs appear in all copies, modifications, and
|
10
|
+
# distributions.
|
11
|
+
#
|
12
|
+
# IN NO EVENT SHALL RODRIGO BOTAFOGO BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL,
|
13
|
+
# INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF
|
14
|
+
# THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF RODRIGO BOTAFOGO HAS BEEN ADVISED OF THE
|
15
|
+
# POSSIBILITY OF SUCH DAMAGE.
|
16
|
+
#
|
17
|
+
# RODRIGO BOTAFOGO SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
18
|
+
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
|
19
|
+
# SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS".
|
20
|
+
# RODRIGO BOTAFOGO HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
|
21
|
+
# OR MODIFICATIONS.
|
22
|
+
##########################################################################################
|
23
|
+
|
24
|
+
require 'galaaz'
|
25
|
+
require 'ggplot'
|
26
|
+
|
27
|
+
tooth_growth = ~:ToothGrowth
|
28
|
+
tooth_growth.dose = tooth_growth.dose.as__factor
|
29
|
+
puts tooth_growth
|
30
|
+
|
31
|
+
e = tooth_growth.ggplot(E.aes(x: :dose, y: :len))
|
32
|
+
|
33
|
+
R.awt
|
34
|
+
|
35
|
+
# Default plot
|
36
|
+
print e + R.geom_jitter(position: R.position_jitter(0.2))
|
37
|
+
|
38
|
+
sleep(2)
|
39
|
+
R.grid__newpage
|
40
|
+
|
41
|
+
# Strip charts with mean points (+/- SD)
|
42
|
+
print e + R.geom_jitter(position: R.position_jitter(0.2)) +
|
43
|
+
R.stat_summary(fun__data: "mean_sdl", fun__args: R.list(mult: 1),
|
44
|
+
geom: "pointrange", color: "red")
|
45
|
+
|
46
|
+
sleep(2)
|
47
|
+
R.grid__newpage
|
48
|
+
|
49
|
+
# Combine with box plot
|
50
|
+
print e + R.geom_jitter(position: R.position_jitter(0.2)) +
|
51
|
+
R.geom_dotplot(binaxis: "y", stackdir: "center")
|
52
|
+
|
53
|
+
sleep(2)
|
54
|
+
R.grid__newpage
|
55
|
+
|
56
|
+
# Add violin plot
|
57
|
+
print e + R.geom_violin(trim: false) +
|
58
|
+
R.geom_jitter(position: R.position_jitter(0.2))
|
59
|
+
|
60
|
+
|
61
|
+
sleep(2)
|
62
|
+
R.grid__newpage
|
63
|
+
|
64
|
+
# Change color and shape by group (dose)
|
65
|
+
print e + R.geom_jitter(E.aes(color: :dose, shape: :dose),
|
66
|
+
position: R.position_jitter(0.2))
|
67
|
+
|
68
|
+
sleep(2)
|
69
|
+
R.grid__newpage
|
70
|
+
|
71
|
+
# a = gets.chomp
|
72
|
+
|
73
|
+
# removes the window
|
74
|
+
R.dev__off
|
@@ -0,0 +1,55 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
##########################################################################################
|
4
|
+
# @author Rodrigo Botafogo
|
5
|
+
#
|
6
|
+
# Copyright © 2018 Rodrigo Botafogo. All Rights Reserved. Permission to use, copy, modify,
|
7
|
+
# and distribute this software and its documentation, without fee and without a signed
|
8
|
+
# licensing agreement, is hereby granted, provided that the above copyright notice, this
|
9
|
+
# paragraph and the following two paragraphs appear in all copies, modifications, and
|
10
|
+
# distributions.
|
11
|
+
#
|
12
|
+
# IN NO EVENT SHALL RODRIGO BOTAFOGO BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL,
|
13
|
+
# INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF
|
14
|
+
# THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF RODRIGO BOTAFOGO HAS BEEN ADVISED OF THE
|
15
|
+
# POSSIBILITY OF SUCH DAMAGE.
|
16
|
+
#
|
17
|
+
# RODRIGO BOTAFOGO SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
18
|
+
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
|
19
|
+
# SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS".
|
20
|
+
# RODRIGO BOTAFOGO HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
|
21
|
+
# OR MODIFICATIONS.
|
22
|
+
##########################################################################################
|
23
|
+
|
24
|
+
require 'galaaz'
|
25
|
+
require 'ggplot'
|
26
|
+
|
27
|
+
df = R.data__frame(supp: R.rep(R.c("VC", "OJ"), each: 3),
|
28
|
+
dose: R.rep(R.c("D0.5", "D1", "D2"), 2),
|
29
|
+
len: R.c(6.8, 15, 33, 4.2, 10, 29.5))
|
30
|
+
puts df.head
|
31
|
+
|
32
|
+
|
33
|
+
R.awt
|
34
|
+
|
35
|
+
# Change line types by groups (supp)
|
36
|
+
print df.ggplot(E.aes(x: :dose, y: :len, group: :supp)) +
|
37
|
+
R.geom_line(E.aes(linetype: :supp)) +
|
38
|
+
R.geom_point
|
39
|
+
|
40
|
+
sleep(2)
|
41
|
+
R.grid__newpage
|
42
|
+
|
43
|
+
# Change line types, point shapes and colors
|
44
|
+
print df.ggplot(E.aes(x: :dose, y: :len, group: :supp)) +
|
45
|
+
R.geom_line(E.aes(linetype: :supp, color: :supp)) +
|
46
|
+
R.geom_point(E.aes(shape: :supp, color: :supp))
|
47
|
+
|
48
|
+
sleep(2)
|
49
|
+
R.grid__newpage
|
50
|
+
|
51
|
+
|
52
|
+
# a = gets.chomp
|
53
|
+
|
54
|
+
# removes the window
|
55
|
+
R.dev__off
|
@@ -0,0 +1,70 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
##########################################################################################
|
4
|
+
# @author Rodrigo Botafogo
|
5
|
+
#
|
6
|
+
# Copyright © 2018 Rodrigo Botafogo. All Rights Reserved. Permission to use, copy, modify,
|
7
|
+
# and distribute this software and its documentation, without fee and without a signed
|
8
|
+
# licensing agreement, is hereby granted, provided that the above copyright notice, this
|
9
|
+
# paragraph and the following two paragraphs appear in all copies, modifications, and
|
10
|
+
# distributions.
|
11
|
+
#
|
12
|
+
# IN NO EVENT SHALL RODRIGO BOTAFOGO BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL,
|
13
|
+
# INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF
|
14
|
+
# THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF RODRIGO BOTAFOGO HAS BEEN ADVISED OF THE
|
15
|
+
# POSSIBILITY OF SUCH DAMAGE.
|
16
|
+
#
|
17
|
+
# RODRIGO BOTAFOGO SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
18
|
+
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
|
19
|
+
# SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS".
|
20
|
+
# RODRIGO BOTAFOGO HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
|
21
|
+
# OR MODIFICATIONS.
|
22
|
+
##########################################################################################
|
23
|
+
|
24
|
+
require 'galaaz'
|
25
|
+
require 'ggplot'
|
26
|
+
|
27
|
+
# Package Hmisc does not yet install on graalvm because of
|
28
|
+
# problems with package data.table
|
29
|
+
# Polyglot.eval("R", "library('Hmisc')")
|
30
|
+
|
31
|
+
tooth_growth = ~:ToothGrowth
|
32
|
+
tooth_growth.dose = tooth_growth.dose.as__factor
|
33
|
+
puts tooth_growth
|
34
|
+
|
35
|
+
e = tooth_growth.ggplot(E.aes(x: :dose, y: :len))
|
36
|
+
|
37
|
+
R.awt
|
38
|
+
|
39
|
+
# Default plot
|
40
|
+
print e + R.geom_violin(trim: false)
|
41
|
+
|
42
|
+
sleep(2)
|
43
|
+
R.grid__newpage
|
44
|
+
|
45
|
+
# violin plot with mean points (+/- SD)
|
46
|
+
# R.stat_summary requires Hmisc
|
47
|
+
print e + R.geom_violin(trim: false) +
|
48
|
+
R.stat_summary(fun__data: "mean_sdl", fun__args: E.list(mult: 1),
|
49
|
+
geom: "pointrange", color: "red")
|
50
|
+
|
51
|
+
# sleep(2)
|
52
|
+
# R.grid__newpage
|
53
|
+
|
54
|
+
# Combine with box plot
|
55
|
+
print e + R.geom_violin(trim: false) +
|
56
|
+
R.geom_boxplot(width: 0.2)
|
57
|
+
|
58
|
+
sleep(2)
|
59
|
+
R.grid__newpage
|
60
|
+
|
61
|
+
# Color by group (dose)
|
62
|
+
print e + R.geom_violin(E.aes(color: :dose), trim: false)
|
63
|
+
|
64
|
+
sleep(2)
|
65
|
+
R.grid__newpage
|
66
|
+
|
67
|
+
# a = gets.chomp
|
68
|
+
|
69
|
+
# removes the window
|
70
|
+
R.dev__off
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
##########################################################################################
|
4
|
+
# @author Rodrigo Botafogo
|
5
|
+
#
|
6
|
+
# Copyright © 2018 Rodrigo Botafogo. All Rights Reserved. Permission to use, copy, modify,
|
7
|
+
# and distribute this software and its documentation, without fee and without a signed
|
8
|
+
# licensing agreement, is hereby granted, provided that the above copyright notice, this
|
9
|
+
# paragraph and the following two paragraphs appear in all copies, modifications, and
|
10
|
+
# distributions.
|
11
|
+
#
|
12
|
+
# IN NO EVENT SHALL RODRIGO BOTAFOGO BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL,
|
13
|
+
# INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF
|
14
|
+
# THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF RODRIGO BOTAFOGO HAS BEEN ADVISED OF THE
|
15
|
+
# POSSIBILITY OF SUCH DAMAGE.
|
16
|
+
#
|
17
|
+
# RODRIGO BOTAFOGO SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
18
|
+
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
|
19
|
+
# SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS".
|
20
|
+
# RODRIGO BOTAFOGO HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
|
21
|
+
# OR MODIFICATIONS.
|
22
|
+
##########################################################################################
|
23
|
+
|
24
|
+
require 'galaaz'
|
25
|
+
require 'ggplot'
|
26
|
+
|
27
|
+
diamonds = ~:diamonds
|
28
|
+
|
29
|
+
R.awt
|
30
|
+
|
31
|
+
print diamonds.ggplot(E.aes(:cut, :color)) +
|
32
|
+
R.geom_jitter(E.aes(color: :cut), size: 0.5)
|
33
|
+
|
34
|
+
sleep(2)
|
35
|
+
R.grid__newpage
|
36
|
+
|
37
|
+
# a = gets.chomp
|
38
|
+
|
39
|
+
# removes the window
|
40
|
+
R.dev__off
|
@@ -0,0 +1,108 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
##########################################################################################
|
4
|
+
# @author Rodrigo Botafogo
|
5
|
+
#
|
6
|
+
# Copyright © 2018 Rodrigo Botafogo. All Rights Reserved. Permission to use, copy, modify,
|
7
|
+
# and distribute this software and its documentation, without fee and without a signed
|
8
|
+
# licensing agreement, is hereby granted, provided that the above copyright notice, this
|
9
|
+
# paragraph and the following two paragraphs appear in all copies, modifications, and
|
10
|
+
# distributions.
|
11
|
+
#
|
12
|
+
# IN NO EVENT SHALL RODRIGO BOTAFOGO BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL,
|
13
|
+
# INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF
|
14
|
+
# THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF RODRIGO BOTAFOGO HAS BEEN ADVISED OF THE
|
15
|
+
# POSSIBILITY OF SUCH DAMAGE.
|
16
|
+
#
|
17
|
+
# RODRIGO BOTAFOGO SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
18
|
+
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
|
19
|
+
# SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS".
|
20
|
+
# RODRIGO BOTAFOGO HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
|
21
|
+
# OR MODIFICATIONS.
|
22
|
+
##########################################################################################
|
23
|
+
|
24
|
+
require 'galaaz'
|
25
|
+
require 'ggplot'
|
26
|
+
|
27
|
+
df = ~:ToothGrowth
|
28
|
+
df.dose = df.dose.as__factor
|
29
|
+
puts df.head
|
30
|
+
|
31
|
+
df2 = R.data__frame(
|
32
|
+
R.aggregate(df.len, by: R.list(df.dose), FUN: :mean),
|
33
|
+
R.aggregate(df.len, by: R.list(df.dose), FUN: :sd)[2]
|
34
|
+
)
|
35
|
+
|
36
|
+
df2.names = R.c("dose", "len", "sd")
|
37
|
+
puts df2.head
|
38
|
+
|
39
|
+
f = df2.ggplot(E.aes(x: :dose, y: :len,
|
40
|
+
ymin: :len - :sd,
|
41
|
+
ymax: :len + :sd))
|
42
|
+
|
43
|
+
df3 = R.data__frame(
|
44
|
+
R.aggregate(df.len, by: R.list(df.dose, df.supp), FUN: :mean),
|
45
|
+
R.aggregate(df.len, by: R.list(df.dose, df.supp), FUN: :sd)[3]
|
46
|
+
)
|
47
|
+
|
48
|
+
df3.names = R.c("dose", "supp", "len", "sd")
|
49
|
+
puts df3.head
|
50
|
+
|
51
|
+
R.awt
|
52
|
+
|
53
|
+
# Default plot
|
54
|
+
print f + R.geom_crossbar
|
55
|
+
|
56
|
+
sleep(2)
|
57
|
+
R.grid__newpage
|
58
|
+
|
59
|
+
# color by groups
|
60
|
+
print f + R.geom_crossbar(E.aes(color: :dose))
|
61
|
+
|
62
|
+
sleep(2)
|
63
|
+
R.grid__newpage
|
64
|
+
|
65
|
+
# Change color manually
|
66
|
+
print f + R.geom_crossbar(E.aes(color: :dose)) +
|
67
|
+
R.scale_color_manual(values: R.c("#999999", "#E69F00", "#56B4E9")) +
|
68
|
+
R.theme_minimal
|
69
|
+
|
70
|
+
sleep(2)
|
71
|
+
R.grid__newpage
|
72
|
+
|
73
|
+
# fill by groups and change color manually
|
74
|
+
print f + R.geom_crossbar(E.aes(fill: :dose)) +
|
75
|
+
R.scale_fill_manual(values: R.c("#999999", "#E69F00", "#56B4E9")) +
|
76
|
+
R.theme_minimal
|
77
|
+
|
78
|
+
sleep(2)
|
79
|
+
R.grid__newpage
|
80
|
+
|
81
|
+
f = df3.ggplot(E.aes(x: :dose, y: :len,
|
82
|
+
ymin: :len - :sd, ymax: :len + :sd))
|
83
|
+
|
84
|
+
# Default plot
|
85
|
+
print f + R.geom_crossbar(E.aes(color: :supp))
|
86
|
+
|
87
|
+
sleep(2)
|
88
|
+
R.grid__newpage
|
89
|
+
|
90
|
+
# Use position_dodge() to avoid overlap
|
91
|
+
print f + R.geom_crossbar(E.aes(color: :supp),
|
92
|
+
position: R.position_dodge(1))
|
93
|
+
|
94
|
+
sleep(2)
|
95
|
+
R.grid__newpage
|
96
|
+
|
97
|
+
f = df.ggplot(E.aes(x: :dose, y: :len, color: :supp))
|
98
|
+
|
99
|
+
# Use geom_crossbar()
|
100
|
+
print f + R.stat_summary(fun__data: "mean_sdl", fun__args: R.list(mult: 1),
|
101
|
+
geom: "crossbar", width: 0.6,
|
102
|
+
position: R.position_dodge(0.8))
|
103
|
+
|
104
|
+
# a = gets.chomp
|
105
|
+
|
106
|
+
# removes the window
|
107
|
+
sleep(2)
|
108
|
+
R.dev__off
|
@@ -0,0 +1,372 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
require 'galaaz'
|
4
|
+
|
5
|
+
# This examples were extracted from "Advanced R", by Hadley Wickham, available on the
|
6
|
+
# web at: http://adv-r.had.co.nz/Subsetting.html#applications
|
7
|
+
|
8
|
+
#------------------------------------------------------------------------------------------
|
9
|
+
# Lookup tables (character subsetting)
|
10
|
+
# Character matching provides a powerful way to make lookup tables.
|
11
|
+
# Say you want to convert abbreviations:
|
12
|
+
#------------------------------------------------------------------------------------------
|
13
|
+
|
14
|
+
x = R.c("m", "f", "u", "f", "f", "m", "m")
|
15
|
+
lookup = R.c(m: "Male", f: "Female", u: R::NA)
|
16
|
+
lookup[x].pp
|
17
|
+
print("\n")
|
18
|
+
|
19
|
+
# m f u f f m m
|
20
|
+
# "Male" "Female" NA "Female" "Female" "Male" "Male"
|
21
|
+
|
22
|
+
R.unname(lookup[x]).pp
|
23
|
+
print("\n")
|
24
|
+
|
25
|
+
# [1] "Male" "Female" NA "Female" "Female" "Male" "Male"
|
26
|
+
|
27
|
+
|
28
|
+
#------------------------------------------------------------------------------------------
|
29
|
+
# Matching and merging by hand (integer subsetting)
|
30
|
+
#------------------------------------------------------------------------------------------
|
31
|
+
|
32
|
+
# You may have a more complicated lookup table which has multiple columns of information.
|
33
|
+
# Suppose we have a vector of grades, and a table that describes their properties:
|
34
|
+
# In R a vector c(1, 2, 3) is a double vector, when using polyglot R.c(1, 2, 3) is an
|
35
|
+
# integer vector, the equivalent of doing c(1L, 2L, 3L) in R. Function 'match' does not
|
36
|
+
# work correctly with integer vector, it has to be a double.
|
37
|
+
grades = R.c(1.0, 2.0, 2.0, 3.0, 1.0)
|
38
|
+
|
39
|
+
info = R.data__frame(
|
40
|
+
grade: (3..1),
|
41
|
+
desc: R.c("Excellent", "Good", "Poor"),
|
42
|
+
fail: R.c(false, false, true)
|
43
|
+
)
|
44
|
+
|
45
|
+
# We want to duplicate the info table so that we have a row for each value in grades.
|
46
|
+
# We can do this in two ways, either using match() and integer subsetting,
|
47
|
+
# or rownames() and character subsetting:
|
48
|
+
|
49
|
+
# Using match
|
50
|
+
id = R.match(grades, info.grade)
|
51
|
+
info[id, :all].pp
|
52
|
+
print("\n")
|
53
|
+
|
54
|
+
# grade desc fail
|
55
|
+
# 3 1 Poor TRUE
|
56
|
+
# 2 2 Good FALSE
|
57
|
+
# 2.1 2 Good FALSE
|
58
|
+
# 1 3 Excellent FALSE
|
59
|
+
# 3.1 1 Poor TRUE
|
60
|
+
|
61
|
+
# Using rownames
|
62
|
+
info.rownames = info.grade
|
63
|
+
info[grades.as__character, :all].pp
|
64
|
+
print("\n")
|
65
|
+
|
66
|
+
# grade desc fail
|
67
|
+
# 1 3 Excellent FALSE
|
68
|
+
# 2 2 Good FALSE
|
69
|
+
# 2.1 2 Good FALSE
|
70
|
+
# 3 1 Poor TRUE
|
71
|
+
# 1.1 3 Excellent FALSE
|
72
|
+
|
73
|
+
#------------------------------------------------------------------------------------------
|
74
|
+
# Random samples/bootstrap (integer subsetting)
|
75
|
+
#------------------------------------------------------------------------------------------
|
76
|
+
|
77
|
+
# You can use integer indices to perform random sampling or bootstrapping
|
78
|
+
# of a vector or data frame. sample() generates a vector of indices, then
|
79
|
+
# subsetting to access the values:
|
80
|
+
df = R.data__frame(x: R.rep((1..3), each: 2), y: (6..1), z: R.letters[(1..6)])
|
81
|
+
|
82
|
+
# Set seed for reproducibility
|
83
|
+
R.set__seed(10)
|
84
|
+
|
85
|
+
# Randomly reorder
|
86
|
+
df[R.sample(df.nrow), :all].pp
|
87
|
+
print("\n")
|
88
|
+
|
89
|
+
# x y z
|
90
|
+
# 4 2 3 d
|
91
|
+
# 2 1 5 b
|
92
|
+
# 5 3 2 e
|
93
|
+
# 3 2 4 c
|
94
|
+
# 1 1 6 a
|
95
|
+
# 6 3 1 f
|
96
|
+
|
97
|
+
# Select 3 random rows
|
98
|
+
df[R.sample(df.nrow, 3), :all].pp
|
99
|
+
print("\n")
|
100
|
+
|
101
|
+
# x y z
|
102
|
+
# 2 1 5 b
|
103
|
+
# 6 3 1 f
|
104
|
+
# 3 2 4 c
|
105
|
+
|
106
|
+
# Select 6 bootstrap replicates
|
107
|
+
df[R.sample(df.nrow, 6, rep: true), :all].pp
|
108
|
+
print("\n")
|
109
|
+
|
110
|
+
# x y z
|
111
|
+
# 3 2 4 c
|
112
|
+
# 4 2 3 d
|
113
|
+
# 4.1 2 3 d
|
114
|
+
# 1 1 6 a
|
115
|
+
# 4.2 2 3 d
|
116
|
+
# 3.1 2 4 c
|
117
|
+
|
118
|
+
#------------------------------------------------------------------------------------------
|
119
|
+
# Ordering (integer subsetting)
|
120
|
+
#------------------------------------------------------------------------------------------
|
121
|
+
|
122
|
+
x = R.c("b", "c", "a")
|
123
|
+
x.order.pp
|
124
|
+
print("\n")
|
125
|
+
|
126
|
+
# [1] 3 1 2
|
127
|
+
|
128
|
+
x[x.order].pp
|
129
|
+
print("\n")
|
130
|
+
|
131
|
+
# [1] "a" "b" "c"
|
132
|
+
|
133
|
+
# Randomly reorder df
|
134
|
+
df2 = df[R.sample(df.nrow), (3..1)]
|
135
|
+
df2.pp
|
136
|
+
print("\n")
|
137
|
+
|
138
|
+
# z y x
|
139
|
+
# 3 c 4 2
|
140
|
+
# 1 a 6 1
|
141
|
+
# 2 b 5 1
|
142
|
+
# 4 d 3 2
|
143
|
+
# 6 f 1 3
|
144
|
+
# 5 e 2 3
|
145
|
+
|
146
|
+
df2[df2.x.order, :all].pp
|
147
|
+
print("\n")
|
148
|
+
|
149
|
+
# z y x
|
150
|
+
# 1 a 6 1
|
151
|
+
# 2 b 5 1
|
152
|
+
# 3 c 4 2
|
153
|
+
# 4 d 3 2
|
154
|
+
# 6 f 1 3
|
155
|
+
# 5 e 2 3
|
156
|
+
|
157
|
+
df2[:all, df2.names.order].pp
|
158
|
+
print("\n")
|
159
|
+
|
160
|
+
# x y z
|
161
|
+
# 3 2 4 c
|
162
|
+
# 1 1 6 a
|
163
|
+
# 2 1 5 b
|
164
|
+
# 4 2 3 d
|
165
|
+
# 6 3 1 f
|
166
|
+
# 5 3 2 e
|
167
|
+
|
168
|
+
#------------------------------------------------------------------------------------------
|
169
|
+
# Expanding aggregated counts (integer subsetting)
|
170
|
+
#
|
171
|
+
# Sometimes you get a data frame where identical rows have been collapsed into one and a
|
172
|
+
# count column has been added. rep() and integer subsetting make it easy to uncollapse
|
173
|
+
# the data by subsetting with a repeated row index:
|
174
|
+
#------------------------------------------------------------------------------------------
|
175
|
+
|
176
|
+
df = R.data__frame(x: R.c(2, 4, 1), y: R.c(9, 11, 6), n: R.c(3, 5, 1))
|
177
|
+
R.rep((1..df.nrow), df.n).pp
|
178
|
+
print("\n")
|
179
|
+
|
180
|
+
# [1] 1 1 1 2 2 2 2 2 3
|
181
|
+
|
182
|
+
df[R.rep((1..df.nrow), df.n), :all].pp
|
183
|
+
print("\n")
|
184
|
+
|
185
|
+
# x y n
|
186
|
+
# 1 2 9 3
|
187
|
+
# 1.1 2 9 3
|
188
|
+
# 1.2 2 9 3
|
189
|
+
# 2 4 11 5
|
190
|
+
# 2.1 4 11 5
|
191
|
+
# 2.2 4 11 5
|
192
|
+
# 2.3 4 11 5
|
193
|
+
# 2.4 4 11 5
|
194
|
+
# 3 1 6 1
|
195
|
+
|
196
|
+
#------------------------------------------------------------------------------------------
|
197
|
+
# Removing columns from data frames (character subsetting)
|
198
|
+
#
|
199
|
+
# There are two ways to remove columns from a data frame. You can set individual columns
|
200
|
+
# to nil:
|
201
|
+
#------------------------------------------------------------------------------------------
|
202
|
+
|
203
|
+
df = R.data__frame(x: (1..3), y: (3..1), z: R.letters[(1..3)])
|
204
|
+
# Not implemented yet
|
205
|
+
# df.z = nil
|
206
|
+
df.pp
|
207
|
+
print("\n")
|
208
|
+
|
209
|
+
df = R.data__frame(x: (1..3), y: (3..1), z: R.letters[(1..3)])
|
210
|
+
df[R.c("x", "y")].pp
|
211
|
+
print("\n")
|
212
|
+
|
213
|
+
# x y
|
214
|
+
# 1 1 3
|
215
|
+
# 2 2 2
|
216
|
+
# 3 3 1
|
217
|
+
|
218
|
+
df[df.names.setdiff("z")].pp
|
219
|
+
print("\n")
|
220
|
+
|
221
|
+
# x y
|
222
|
+
# 1 1 3
|
223
|
+
# 2 2 2
|
224
|
+
# 3 3 1
|
225
|
+
|
226
|
+
#------------------------------------------------------------------------------------------
|
227
|
+
# Selecting rows based on a condition (logical subsetting)
|
228
|
+
#
|
229
|
+
# Because it allows you to easily combine conditions from multiple columns, logical
|
230
|
+
# subsetting is probably the most commonly used technique for extracting rows out of
|
231
|
+
# a data frame.
|
232
|
+
#------------------------------------------------------------------------------------------
|
233
|
+
|
234
|
+
R.mtcars[R.mtcars.gear == 5, :all].pp
|
235
|
+
print("\n")
|
236
|
+
|
237
|
+
# mpg cyl disp hp drat wt qsec vs am gear carb
|
238
|
+
# Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.7 0 1 5 2
|
239
|
+
# Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.9 1 1 5 2
|
240
|
+
# Ford Pantera L 15.8 8 351.0 264 4.22 3.170 14.5 0 1 5 4
|
241
|
+
# Ferrari Dino 19.7 6 145.0 175 3.62 2.770 15.5 0 1 5 6
|
242
|
+
# Maserati Bora 15.0 8 301.0 335 3.54 3.570 14.6 0 1 5 8
|
243
|
+
|
244
|
+
R.mtcars[(R.mtcars.gear == 5) & (R.mtcars.cyl == 4), :all].pp
|
245
|
+
print("\n")
|
246
|
+
|
247
|
+
# mpg cyl disp hp drat wt qsec vs am gear carb
|
248
|
+
# Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.7 0 1 5 2
|
249
|
+
# Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.9 1 1 5 2
|
250
|
+
|
251
|
+
|
252
|
+
#------------------------------------------------------------------------------------------
|
253
|
+
# Boolean algebra vs. sets (logical & integer subsetting)
|
254
|
+
#
|
255
|
+
# It’s useful to be aware of the natural equivalence between set operations (integer
|
256
|
+
# subsetting) and boolean algebra (logical subsetting)
|
257
|
+
#------------------------------------------------------------------------------------------
|
258
|
+
|
259
|
+
x = R.sample(10) < 4
|
260
|
+
x.which.pp
|
261
|
+
print("\n")
|
262
|
+
|
263
|
+
# [1] 3 7 10
|
264
|
+
|
265
|
+
#===
|
266
|
+
x1 = R.c((1..10)) % 2 == 0
|
267
|
+
x1.pp
|
268
|
+
print("\n")
|
269
|
+
|
270
|
+
# [1] FALSE TRUE FALSE TRUE FALSE TRUE FALSE TRUE FALSE TRUE
|
271
|
+
|
272
|
+
#===
|
273
|
+
x2 = x1.which
|
274
|
+
x2.pp
|
275
|
+
print("\n")
|
276
|
+
|
277
|
+
# [1] 2 4 6 8 10
|
278
|
+
|
279
|
+
#===
|
280
|
+
y1 = R.c((1..10)) % 5 == 0
|
281
|
+
y1.pp
|
282
|
+
print("\n")
|
283
|
+
|
284
|
+
# [1] FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE TRUE
|
285
|
+
|
286
|
+
#===
|
287
|
+
y2 = y1.which
|
288
|
+
y2.pp
|
289
|
+
print("\n")
|
290
|
+
|
291
|
+
# [1] 5 10
|
292
|
+
|
293
|
+
#===
|
294
|
+
# X & Y <-> intersect(x, y)
|
295
|
+
(x1 & y1).pp
|
296
|
+
print("\n")
|
297
|
+
|
298
|
+
# [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE
|
299
|
+
|
300
|
+
#===
|
301
|
+
# This example shows the problem with having R objects returning either
|
302
|
+
# vector or scalar. We don't know the type of the result of applying
|
303
|
+
# intersect. If this is a vector, then we need to print it with pp
|
304
|
+
# but if this is a scalar, we need to print it with regular Ruby 'p' or
|
305
|
+
# 'print'
|
306
|
+
p R.intersect(x2, y2)
|
307
|
+
print("\n")
|
308
|
+
|
309
|
+
# 10
|
310
|
+
|
311
|
+
p x2.intersect y2
|
312
|
+
|
313
|
+
# 10
|
314
|
+
|
315
|
+
#===
|
316
|
+
# X | Y <-> union(x, y)
|
317
|
+
(x1 | y1).pp
|
318
|
+
print("\n")
|
319
|
+
|
320
|
+
# [1] FALSE TRUE FALSE TRUE TRUE TRUE FALSE TRUE FALSE TRUE
|
321
|
+
|
322
|
+
#===
|
323
|
+
R.union(x2, y2).pp
|
324
|
+
print("\n")
|
325
|
+
|
326
|
+
# [1] 2 4 6 8 10 5
|
327
|
+
|
328
|
+
(x2.union y2).pp
|
329
|
+
|
330
|
+
# [1] 2 4 6 8 10 5
|
331
|
+
|
332
|
+
#===
|
333
|
+
# X & !Y <-> setdiff(x, y)
|
334
|
+
(x1 & !y1).pp
|
335
|
+
print("\n")
|
336
|
+
|
337
|
+
# [1] FALSE TRUE FALSE TRUE FALSE TRUE FALSE TRUE FALSE FALSE
|
338
|
+
|
339
|
+
#===
|
340
|
+
R.setdiff(x2, y2).pp
|
341
|
+
print("\n")
|
342
|
+
|
343
|
+
# [1] 2 4 6 8
|
344
|
+
|
345
|
+
(x2.setdiff y2).pp
|
346
|
+
|
347
|
+
# [1] 2 4 6 8
|
348
|
+
|
349
|
+
|
350
|
+
#===
|
351
|
+
# xor(X, Y) <-> setdiff(union(x, y), intersect(x, y))
|
352
|
+
R.xor(x1, y1).pp
|
353
|
+
print("\n")
|
354
|
+
|
355
|
+
# [1] FALSE TRUE FALSE TRUE TRUE TRUE FALSE TRUE FALSE FALSE
|
356
|
+
|
357
|
+
# Writing the same as the last example in a Ruby style
|
358
|
+
(x1.xor y1).pp
|
359
|
+
|
360
|
+
# [1] FALSE TRUE FALSE TRUE TRUE TRUE FALSE TRUE FALSE FALSE
|
361
|
+
|
362
|
+
#===
|
363
|
+
R.setdiff(R.union(x2, y2), R.intersect(x2, y2)).pp
|
364
|
+
print("\n")
|
365
|
+
|
366
|
+
# [1] 2 4 6 8 5
|
367
|
+
|
368
|
+
# Writing the same as the last example in a Ruby style
|
369
|
+
((x2.union y2).setdiff (x2.intersect y2)).pp
|
370
|
+
print("\n")
|
371
|
+
|
372
|
+
# [1] 2 4 6 8 5
|