Quickstart: Creating Charts
One of the compelling features of R is its ability to create beautiful charts. With the R Type Provider, you can use all of R capabilities from F#, and create simple charts quickly to explore and visualize your data on-the-fly, as well as generate publication quality graphics that can be exported to virtually any format.
Charts Basics
Basic charts can be found in the graphics package. Assuming you installed the R Type Provider in your project from NuGet, you can reference the required libraries and packages this way:
1: 2: 3: 4: 5: 6: 7: |
|
Once the libraries and packages have been loaded, producing basic charts is as simple as this:
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: |
|
Exporting and Saving Charts
Charts can be exported and saved to various formats; once you have opened the grDevices package, you can save a chart like this:
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: |
|
Advanced Charts Options
The graphic functions exposed by the R Type Provider come in two flavors;
they either have optional named arguments,
followed by a ParamArray for extended arguments,
or they take named parameters, an IDictionary
Named Arguments
Consider for instance the following example:
1: 2: |
|
R.title has 2 signatures, one of them with optional arguments, demonstrated above to set the main title as well as the labels for the x and y axis, ignoring some of the other available options. You can see another example in the previous section in the R.png call.
Named Parameters
Named parameters allow you to specify every argument supported by R, as an IDictionary of string, object. The string is the name of the argument, and the object its value.
Finding the available arguments for a R function can be tricky; the full list of arguments can usually be found in the R developer documentation, navigating in the correct package. For instance, R.plot belongs to graphics, and can be found here.
The easiest way to use that feature is to leverage the built-in function namedParams, like in this example:
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: |
|
The first call specifies what to plot (widgets), what type of line to use, the color, and the scale of the axis. The second call adds sprockets, specifying lty (the line type), and pch (the plotting character).
box is used to reduce all elements to objects, so that the lists have consistent types.
A possibly more elegant way to use namedParams is to follow the pattern below:
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: |
|
Full name: Charts-QuickStart.widgets
Full name: Charts-QuickStart.sprockets
static member Axis : ?x: obj * ?at: obj * ?___: obj * ?side: obj * ?labels: obj * ?paramArray: obj [] -> SymbolicExpression + 1 overload
static member abline : ?a: obj * ?b: obj * ?h: obj * ?v: obj * ?reg: obj * ?coef: obj * ?untf: obj * ?___: obj * ?paramArray: obj [] -> SymbolicExpression + 1 overload
static member arrows : ?x0: obj * ?y0: obj * ?x1: obj * ?y1: obj * ?length: obj * ?angle: obj * ?code: obj * ?col: obj * ?lty: obj * ?lwd: obj * ?___: obj * ?paramArray: obj [] -> SymbolicExpression + 1 overload
static member assocplot : ?x: obj * ?col: obj * ?space: obj * ?main: obj * ?xlab: obj * ?ylab: obj -> SymbolicExpression + 1 overload
static member axTicks : ?side: obj * ?axp: obj * ?usr: obj * ?log: obj * ?nintLog: obj -> SymbolicExpression + 1 overload
static member axis : ?side: obj * ?at: obj * ?labels: obj * ?tick: obj * ?line: obj * ?pos: obj * ?outer: obj * ?font: obj * ?lty: obj * ?lwd: obj * ?lwd_ticks: obj * ?col: obj * ?col_ticks: obj * ?hadj: obj * ?padj: obj * ?___: obj * ?paramArray: obj [] -> SymbolicExpression + 1 overload
static member axis_Date : ?side: obj * ?x: obj * ?at: obj * ?format: obj * ?labels: obj * ?___: obj * ?paramArray: obj [] -> SymbolicExpression + 1 overload
static member axis_POSIXct : ?side: obj * ?x: obj * ?at: obj * ?format: obj * ?labels: obj * ?___: obj * ?paramArray: obj [] -> SymbolicExpression + 1 overload
static member barplot : ?height: obj * ?___: obj * ?paramArray: obj [] -> SymbolicExpression + 1 overload
static member barplot_default : ?height: obj * ?width: obj * ?space: obj * ?names_arg: obj * ?legend_text: obj * ?beside: obj * ?horiz: obj * ?density: obj * ?angle: obj * ?col: obj * ?border: obj * ?main: obj * ?sub: obj * ?xlab: obj * ?ylab: obj * ?xlim: obj * ?ylim: obj * ?xpd: obj * ?log: obj * ?axes: obj * ?axisnames: obj * ?cex_axis: obj * ?cex_names: obj * ?inside: obj * ?plot: obj * ?axis_lty: obj * ?offset: obj * ?add: obj * ?args_legend: obj * ?___: obj * ?paramArray: obj [] -> SymbolicExpression + 1 overload
...
Full name: RProvider.graphics.R
R functions for base graphics.
R.plot(?x: obj, ?y: obj, ?___: obj, ?paramArray: obj []) : SymbolicExpression
Generic X-Y Plotting
R.barplot(?height: obj, ?___: obj, ?paramArray: obj []) : SymbolicExpression
Bar Plots
R.hist(?x: obj, ?___: obj, ?paramArray: obj []) : SymbolicExpression
Histograms
R.pie(?x: obj, ?labels: obj, ?edges: obj, ?radius: obj, ?clockwise: obj, ?init_angle: obj, ?density: obj, ?angle: obj, ?col: obj, ?border: obj, ?lty: obj, ?main: obj, ?___: obj, ?paramArray: obj []) : SymbolicExpression
Pie Charts
Full name: Charts-QuickStart.desktop
active recognizer Environment: SymbolicExpression -> REnvironment option
Full name: RDotNet.ActivePatterns.( |Environment|_| )
--------------------
type Environment =
static member CommandLine : string
static member CurrentDirectory : string with get, set
static member Exit : exitCode:int -> unit
static member ExitCode : int with get, set
static member ExpandEnvironmentVariables : name:string -> string
static member FailFast : message:string -> unit + 1 overload
static member GetCommandLineArgs : unit -> string[]
static member GetEnvironmentVariable : variable:string -> string + 1 overload
static member GetEnvironmentVariables : unit -> IDictionary + 1 overload
static member GetFolderPath : folder:SpecialFolder -> string + 1 overload
...
nested type SpecialFolder
nested type SpecialFolderOption
Full name: System.Environment
Environment.GetFolderPath(folder: Environment.SpecialFolder, option: Environment.SpecialFolderOption) : string
| ApplicationData = 26
| CommonApplicationData = 35
| LocalApplicationData = 28
| Cookies = 33
| Desktop = 0
| Favorites = 6
| History = 34
| InternetCache = 32
| Programs = 2
| MyComputer = 17
...
Full name: System.Environment.SpecialFolder
Full name: Charts-QuickStart.path
static member CIDFont : ?family: obj * ?cmap: obj * ?cmapEncoding: obj * ?pdfresource: obj -> SymbolicExpression + 1 overload
static member Hershey : SymbolicExpression
static member Type1Font : ?family: obj * ?metrics: obj * ?encoding: obj -> SymbolicExpression + 1 overload
static member X11 : ?width: obj * ?height: obj * ?pointsize: obj * ?bg: obj * ?gamma: obj * ?xpos: obj * ?ypos: obj * ?title: obj -> SymbolicExpression + 1 overload
static member adjustcolor : ?col: obj * ?alpha_f: obj * ?red_f: obj * ?green_f: obj * ?blue_f: obj * ?offset: obj * ?transform: obj -> SymbolicExpression + 1 overload
static member as_graphicsAnnot : ?x: obj -> SymbolicExpression + 1 overload
static member as_raster : ?x: obj * ?___: obj * ?paramArray: obj [] -> SymbolicExpression + 1 overload
static member axisTicks : ?usr: obj * ?log: obj * ?axp: obj * ?nint: obj -> SymbolicExpression + 1 overload
static member bitmap : ?file: obj * ?type: obj * ?height: obj * ?width: obj * ?res: obj * ?units: obj * ?pointsize: obj * ?taa: obj * ?gaa: obj * ?___: obj * ?paramArray: obj [] -> SymbolicExpression + 1 overload
static member blues9 : SymbolicExpression
...
Full name: RProvider.grDevices.R
Graphics devices and support for base and grid graphics.
R.png(?filename: obj, ?width: obj, ?height: obj, ?units: obj, ?pointsize: obj, ?bg: obj, ?res: obj, ?family: obj, ?restoreConsole: obj, ?type: obj, ?antialias: obj) : SymbolicExpression
BMP, JPEG, PNG and TIFF graphics devices
R.dev_off(?which: obj) : SymbolicExpression
No documentation available
R.title(?main: obj, ?sub: obj, ?xlab: obj, ?ylab: obj, ?line: obj, ?outer: obj, ?___: obj, ?paramArray: obj []) : SymbolicExpression
Plot Annotation
Full name: RProvider.Helpers.namedParams
Full name: Microsoft.FSharp.Core.Operators.box
R.lines(?x: obj, ?___: obj, ?paramArray: obj []) : SymbolicExpression
Add Connected Line Segments to a Plot