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 are using an F# script, you can reference the required libraries and packages this way:

#r "nuget: RProvider,2.0.2"
open RProvider
open RProvider.Operators
open RProvider.graphics

Once the libraries and packages have been loaded, producing basic charts is as simple as this:

let widgets = [ 3; 8; 12; 15; 19; 18; 18; 20; ]
let sprockets = [ 5; 4; 6; 7; 12; 9; 5; 6; ]
R.plot(widgets)

R.plot(widgets, sprockets)

R.barplot(widgets)

R.hist(sprockets)

R.pie(widgets)

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:

// Required package to save charts
open RProvider.grDevices

// Create path to an image testimage.png on the Desktop
let desktop = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop)  
let path = desktop + @"\testimage.png"

// Open the device and create the file as a png.
// R.bmp, R.jpeg, R.pdf, ... will generate other formats.
R.png(filename=path, height=200, width=300, bg="white")
// Create the chart into the file
R.barplot(widgets)
// Close the device once the chart is complete
R.dev_off ()

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 which contains all the arguments passed to the function.

Named Arguments

Consider for instance the following example:

R.barplot(widgets)
R.title(main="Widgets", xlab="Period", ylab="Quantity")

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:

R.plot([
    "x" => widgets
    "type" => "o"
    "col" => "blue"
    "ylim" => [0; 25] ])

R.lines([
    "x" => sprockets
    "type" => "o"
    "pch" => 22
    "lty" => 2
    "col" => "red" ])

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:

[   
    "x" => widgets
    "type" => "o"
    "col" => "blue"
    "ylim" => [0; 25] ]
|> R.plot

[   
    "x" => sprockets
    "type" => "o"
    "pch" => 22
    "lty" => 2
    "col" => "red" ]
|> R.lines
namespace RProvider
Multiple items
namespace RProvider

--------------------
type RProvider = inherit TypeProviderForNamespaces new : cfg:TypeProviderConfig -> RProvider

--------------------
new : cfg:CompilerServices.TypeProviderConfig -> RProvider
module Operators from RProvider
<summary> Custom operators that make composing and working with R symbolic expressions easier. </summary>
namespace RProvider.graphics
val widgets : int list
val sprockets : int list
type R = static member Axis :?x: obj *?at: obj *?___: obj *?side: obj *?labels: obj *?paramArray: obj [] -> SymbolicExpression + 2 overloads static member abline :?a: obj *?b: obj *?h: obj *?v: obj *?reg: obj *?coef: obj *?untf: obj *?___: obj *?paramArray: obj [] -> SymbolicExpression + 2 overloads 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 + 2 overloads static member assocplot :?x: obj *?col: obj *?space: obj *?main: obj *?xlab: obj *?ylab: obj -> SymbolicExpression + 2 overloads static member axTicks :?side: obj *?axp: obj *?usr: obj *?log: obj *?nintLog: obj -> SymbolicExpression + 2 overloads 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 *?gap_axis: obj *?___: obj *?paramArray: obj [] -> SymbolicExpression + 2 overloads static member axis_Date :?side: obj *?x: obj *?at: obj *?format: obj *?labels: obj *?___: obj *?paramArray: obj [] -> SymbolicExpression + 2 overloads static member axis_POSIXct :?side: obj *?x: obj *?at: obj *?format: obj *?labels: obj *?___: obj *?paramArray: obj [] -> SymbolicExpression + 2 overloads static member barplot :?height: obj *?___: obj *?paramArray: obj [] -> SymbolicExpression + 2 overloads 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 *?ann: obj *?args_legend: obj *?___: obj *?paramArray: obj [] -> SymbolicExpression + 2 overloads ...
R functions for base graphics.
Multiple items
R.plot(paramsByName: List<string * obj>) : RDotNet.SymbolicExpression
R.plot(paramsByName: System.Collections.Generic.IDictionary<string,obj>) : RDotNet.SymbolicExpression
R.plot(?x: obj,?y: obj,?___: obj,?paramArray: obj []) : RDotNet.SymbolicExpression
No documentation available

--------------------
R.plot(paramsByName: List<string * obj>) : RDotNet.SymbolicExpression
R.plot(paramsByName: System.Collections.Generic.IDictionary<string,obj>) : RDotNet.SymbolicExpression
R.plot(?x: obj,?y: obj,?___: obj,?paramArray: obj []) : RDotNet.SymbolicExpression
Generic X-Y Plotting
R.barplot(paramsByName: List<string * obj>) : RDotNet.SymbolicExpression
R.barplot(paramsByName: System.Collections.Generic.IDictionary<string,obj>) : RDotNet.SymbolicExpression
R.barplot(?height: obj,?___: obj,?paramArray: obj []) : RDotNet.SymbolicExpression
Bar Plots
R.hist(paramsByName: List<string * obj>) : RDotNet.SymbolicExpression
R.hist(paramsByName: System.Collections.Generic.IDictionary<string,obj>) : RDotNet.SymbolicExpression
R.hist(?x: obj,?___: obj,?paramArray: obj []) : RDotNet.SymbolicExpression
Histograms
R.pie(paramsByName: List<string * obj>) : RDotNet.SymbolicExpression
R.pie(paramsByName: System.Collections.Generic.IDictionary<string,obj>) : RDotNet.SymbolicExpression
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 []) : RDotNet.SymbolicExpression
Pie Charts
namespace RProvider.grDevices
val desktop : string
namespace System
type Environment = static member Exit : exitCode: int -> unit 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 static member GetLogicalDrives : unit -> string [] static member SetEnvironmentVariable : variable: string * value: string -> unit + 1 overload static member CommandLine : string ...
<summary>Provides information about, and means to manipulate, the current environment and platform. This class cannot be inherited.</summary>
System.Environment.GetFolderPath(folder: System.Environment.SpecialFolder) : string
System.Environment.GetFolderPath(folder: System.Environment.SpecialFolder, option: System.Environment.SpecialFolderOption) : string
type SpecialFolder = | Desktop = 0 | Programs = 2 | MyDocuments = 5 | Personal = 5 | Favorites = 6 | Startup = 7 | Recent = 8 | SendTo = 9 | StartMenu = 11 | MyMusic = 13 ...
<summary>Specifies enumerated constants used to retrieve directory paths to system special folders.</summary>
field System.Environment.SpecialFolder.Desktop: System.Environment.SpecialFolder = 0
<summary>The logical Desktop rather than the physical file system location.</summary>
val path : string
type R = static member CIDFont :?family: obj *?cmap: obj *?cmapEncoding: obj *?pdfresource: obj -> SymbolicExpression + 2 overloads static member Type1Font :?family: obj *?metrics: obj *?encoding: obj -> SymbolicExpression + 2 overloads static member X11 :?display: obj *?width: obj *?height: obj *?pointsize: obj *?gamma: obj *?bg: obj *?canvas: obj *?fonts: obj *?family: obj *?xpos: obj *?ypos: obj *?title: obj *?type: obj *?antialias: obj *?symbolfamily: obj -> SymbolicExpression + 2 overloads static member X11Font :?font: obj -> SymbolicExpression + 2 overloads static member X11Fonts :?___: obj *?paramArray: obj [] -> SymbolicExpression + 2 overloads static member X11_options :?___: obj *?reset: obj *?paramArray: obj [] -> SymbolicExpression + 2 overloads static member adjustcolor :?col: obj *?alpha_f: obj *?red_f: obj *?green_f: obj *?blue_f: obj *?offset: obj *?transform: obj -> SymbolicExpression + 2 overloads static member as_graphicsAnnot :?x: obj -> SymbolicExpression + 2 overloads static member as_raster :?x: obj *?___: obj *?paramArray: obj [] -> SymbolicExpression + 2 overloads static member axisTicks :?usr: obj *?log: obj *?axp: obj *?nint: obj -> SymbolicExpression + 2 overloads ...
Graphics devices and support for base and grid graphics.
R.png(paramsByName: List<string * obj>) : RDotNet.SymbolicExpression
R.png(paramsByName: System.Collections.Generic.IDictionary<string,obj>) : RDotNet.SymbolicExpression
R.png(?filename: obj,?width: obj,?height: obj,?units: obj,?pointsize: obj,?bg: obj,?res: obj,?___: obj,?type: obj,?antialias: obj,?paramArray: obj []) : RDotNet.SymbolicExpression
BMP, JPEG, PNG and TIFF graphics devices
R.dev_off(paramsByName: List<string * obj>) : RDotNet.SymbolicExpression
R.dev_off(paramsByName: System.Collections.Generic.IDictionary<string,obj>) : RDotNet.SymbolicExpression
R.dev_off(?which: obj) : RDotNet.SymbolicExpression
No documentation available
R.title(paramsByName: List<string * obj>) : RDotNet.SymbolicExpression
R.title(paramsByName: System.Collections.Generic.IDictionary<string,obj>) : RDotNet.SymbolicExpression
R.title(?main: obj,?sub: obj,?xlab: obj,?ylab: obj,?line: obj,?outer: obj,?___: obj,?paramArray: obj []) : RDotNet.SymbolicExpression
Plot Annotation
R.lines(paramsByName: List<string * obj>) : RDotNet.SymbolicExpression
R.lines(paramsByName: System.Collections.Generic.IDictionary<string,obj>) : RDotNet.SymbolicExpression
R.lines(?x: obj,?___: obj,?paramArray: obj []) : RDotNet.SymbolicExpression
Add Connected Line Segments to a Plot