package ocaml-r

  1. Overview
  2. Docs

Source file OCamlR_graphics.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
open OCamlR
open OCamlR_base

let () = ignore (eval_string "require(graphics, quietly=TRUE)")

module Symbol = struct

  let hist = symbol "hist"
  let plot = symbol ~generic:true "plot"
  let boxplot = symbol "boxplot"
  let par = symbol "par"

end

let float_tup (x, y) = Enc.floats [| x ; y |]

let int_tup (x, y) = Enc.ints [| x ; y |]


class hist o = object
  method breaks = List_.subset2_exn o "breaks" Dec.floats
  method counts = List_.subset2_exn o "counts" Dec.floats
  method density = List_.subset2_exn o "density" Dec.floats
  method mids = List_.subset2_exn o "mids" Dec.floats
  method xname = List_.subset2_exn o "xname" Dec.string
  method equidist = List_.subset2_exn o "equidist" Dec.bool
end

let r_breaks =
  let open Enc in
  function
  | `n n -> int n
  | `l v -> floats v
  | `m `Sturges -> string "Sturges"
  | `m `Scott -> string "Scott"
  | `m `FD -> string "FD"

let hist ?breaks ?freq ?include_lowest ?right ?(main = "") ?(xlab = "") ?ylab ?xlim ?ylim ?plot x =
  call Symbol.hist Enc.[
      arg floats x ;
      opt_arg r_breaks "breaks" breaks ;
      opt_arg bool "freq" freq ;
      opt_arg bool "include_lowest" include_lowest ;
      opt_arg bool "right"right ;
      arg string ~name:"main" main;
      arg string ~name:"xlab" xlab ;
      opt_arg string "ylab" ylab ;
      opt_arg float_tup "xlim" xlim ;
      opt_arg float_tup "ylim" ylim ;
      opt_arg bool "plot" plot ;
    ]
  |> List_.unsafe_of_sexp
  |> new hist

type plot_type = [
  | `Points
  | `Lines
  | `Both
  | `Overplotted
  | `Histogram
  | `Stair_steps
  | `Other_steps
  | `Nothing
]

type log_scale = [ `X | `Y | `XY ]

module E = struct
  let plot_type t =
    Enc.string (
      match t with
      | `Points -> "p"
      | `Lines -> "l"
      | `Both -> "b"
      | `Overplotted -> "o"
      | `Histogram -> "h"
      | `Stair_steps -> "s"
      | `Other_steps -> "S"
      | `Nothing -> "n"
    )

  let log_scale t =
    Enc.string (
      match t with
      | `X -> "x"
      | `Y -> "y"
      | `XY -> "xy"
    )
end

let plot ?main ?(xlab = "") ?(ylab = "") ?xlim ?ylim ?plot_type ?lwd ?col ?log ~x ?y () =
  call Symbol.plot Enc.[
      arg floats x ;
      opt_arg floats "y" y ;
      opt_arg string "main" main ;
      arg string ~name:"xlab" xlab ;
      arg string ~name:"ylab" ylab ;
      opt_arg float_tup "xlim" xlim ;
      opt_arg float_tup "ylim" ylim ;
      opt_arg E.plot_type "type" plot_type ;
      opt_arg int "lwd" lwd ;
      opt_arg string "col" col ;
      opt_arg E.log_scale "log" log ;
    ]
  |> ignore

type line_type = [
  | `blank
  | `solid
  | `dashed
  | `dotted
  | `dotdash
  | `longdash
  | `twodash
]

let int_of_line_type = function
  | `blank -> 0
  | `solid -> 1
  | `dashed -> 2
  | `dotted -> 3
  | `dotdash -> 4
  | `longdash -> 5
  | `twodash -> 6

let line_type_arg x = Enc.int (int_of_line_type x)

let lines_symbol = symbol "lines"

let lines ?lty ?lwd ?col ~x ?y () =
  call lines_symbol Enc.[
      arg floats x ;
      opt_arg floats "y" y ;
      opt_arg line_type_arg "lty" lty ;
      opt_arg int "lwd" lwd ;
      opt_arg string "col" col ;
    ]
  |> ignore

let points_symbol = symbol "points"

let points ?pch ?col ~x ?y () =
  call points_symbol Enc.[
      arg floats x ;
      opt_arg floats "y" y ;
      opt_arg int "pch" pch ;
      opt_arg string "col" col ;
    ]
  |> ignore

let string_of_position = function
  | `bottomright -> "bottomright"
  | `bottom -> "bottom"
  | `bottomleft -> "bottomleft"
  | `left -> "left"
  | `topleft -> "topleft"
  | `top -> "top"
  | `topright -> "topright"
  | `right -> "right"
  | `center -> "center"

let legend_symbol = symbol "legend"

let legend ?col ?lty ?lwd ?pch x legend =
  call legend_symbol Enc.[
      arg (fun x -> string (string_of_position x)) x ;
      arg strings legend ;
      opt_arg strings "col" col ;
      opt_arg (fun x -> ints (Array.map int_of_line_type x)) "lty" lty ;
      opt_arg floats "lwd" lwd ;
      opt_arg ints "pch" pch ;
    ]
  |> ignore

let par ?mfrow () =
  call Symbol.par [
      opt_arg int_tup "mfrow" mfrow ;
    ]
  |> ignore

let dataframe_boxplot ?main ?xlab ?ylab formula data =
  call Symbol.boxplot Enc.[
      arg Formula.to_sexp formula ;
      arg Dataframe.to_sexp ~name:"data" data ;
      opt_arg string "main" main ;
      opt_arg string "xlab" xlab ;
      opt_arg string "ylab" ylab ;
    ]
  |> ignore

let list_boxplot ?main ?xlab ?ylab data =
  call Symbol.boxplot Enc.[
      arg List_.to_sexp ~name:"x" data ;
      opt_arg string "main" main ;
      opt_arg string "xlab" xlab ;
      opt_arg string "ylab" ylab ;
    ]
  |> ignore

let abline_symbol = symbol "abline"

let abline ?a ?b ?h ?v ?lty ?lwd ?col () =
  call abline_symbol Enc.[
      opt_arg float "a" a ;
      opt_arg float "b" b ;
      opt_arg float "h" h ;
      opt_arg float "v" v ;
      opt_arg line_type_arg "lty" lty ;
      opt_arg int "lwd" lwd ;
      opt_arg string "col" col ;
    ]
  |> ignore

let smooth_scatter_symbol = symbol "smoothScatter"

let smooth_scatter ?main ?xlab ?ylab ~x ?y () =
  call smooth_scatter_symbol Enc.[
      arg floats x ;
      opt_arg floats "y" y ;
      opt_arg string "main" main ;
      opt_arg string "xlab" xlab ;
      opt_arg string "ylab" ylab ;
    ]
  |> ignore

let text_symbol = symbol "text"

let text ?adj ?pos ?cex ?col ~x ?y ~labels () =
  call text_symbol Enc.[
      arg ~name:"x" floats x ;
      arg ~name:"labels" strings labels ;
      opt_arg floats "y" y ;
      opt_arg string "col" col ;
      opt_arg float "cex" cex ;
      opt_arg int "pos" pos ;
      opt_arg (fun (h, v) -> float_opts [|h ; v|]) "adj" adj ;
    ]
  |> ignore

let side_arg x =
  Enc.int (
    match x with
    | `below -> 1
    | `left -> 2
    | `above -> 3
    | `right -> 4
  )

let logical_or_character_arg = function
  | `Yes -> Enc.bool true
  | `No  -> Enc.bool false
  | `Custom xs -> Enc.strings xs

let axis_symbol = symbol "axis"

let axis ?at ?labels ?tick ?line ?pos ?outer ?font ?lty ?lwd ?lwd'ticks side =
  call axis_symbol Enc.[
      arg ~name:"side" side_arg side ;
      opt_arg floats "at" at ;
      opt_arg logical_or_character_arg "labels" labels ;
      opt_arg logical_or_character_arg "tick" tick ;
      opt_arg int "line" line ;
      opt_arg float "pos" pos ;
      opt_arg bool "outer" outer ;
      opt_arg int "font" font ;
      opt_arg line_type_arg "lty" lty ;
      opt_arg float "lwd" lwd ;
      opt_arg float "lwd'ticks" lwd'ticks ;
    ]
  |> ignore

module Enc = E
OCaml

Innovation. Community. Security.