Logo RProvider

Creating Plots and Graphics

One of the compelling features of R is its ability to create beautiful plots. With the R Type Provider, you can use all of R capabilities from F#, and create simple plots 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.

Basic R plots

Basic plots 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.graphics

RProvider includes a Graphics module for capturing R plots. You may also use many (but not all) R graphics devices directly. See graphics for more details.

The primary helper function is for outputting vector-based non-interactive plots as svg graphics. Wrap your plot-producing code in a function within Graphics.svg as below:

let widgets = [ 3; 8; 12; 15; 19; 18; 18; 20; ]

Graphics.svg 7 4 (fun _ -> R.plot widgets)
1 2 3 4 5 6 7 8 5 10 15 20 Index c(3L, 8L, 12L, 15L, 19L, 18L, 18L, 20L)
Graphics.svg 7 4 (fun _ -> R.barplot widgets)
0 5 10 15 20

Using ggplot2

ggplot2 is an R package that expresses the grammar of graphics. Using RProvider, we can plot F# data in publication-quality plots using ggplot2.

open RProvider.ggplot2
open RProvider.datasets

let (++) a b = R.ggplot__add (a,b)

Graphics.svg 7 4 (fun _ ->
    R.ggplot(R.mtcars, R.aes(x = "mpg", y = "disp")) ++
    R.geom__point()
)
No value returned by any evaluator

Exporting and Saving Charts

The RProvider Graphics.svg callback (above) may be used to generate svg graphics.

Alternatively, you may use R graphics devices directly and manipulate them through R functions.

Note. Some graphics devices are not happy running when R is embedded in another process like RProvider. For example, on macOS calling Quartz will crash the process, as it will not run outside of the main thread of a process. X11 is more stable on macOS.

An example is shown below of using the PNG device.

open RProvider.grDevices

// Open the device and create the file as a png.
// R.bmp, R.jpeg, R.pdf, ... will generate other formats.
R.png(filename = "test.png", 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 ()

R plot arguments

Named parameters allow you to specify every argument supported by R, as an list of label and value tuples.

An example of using named arguments is below.

open RProvider.Operators

let sprokets = [ 5.3; 6.5; 1.2; 5.3; 4.; 18.; 15.2; 12.1 ]

Graphics.svg 7 4 (fun _ ->
    R.plot [
        "x" => widgets
        "type" => "o"
        "col" => "blue"
        "ylim" => [0; 25] ] |> ignore
    R.lines [
        "x" => sprokets
        "type" => "o"
        "pch" => 22
        "lty" => 2
        "col" => "red" ]
)
1 2 3 4 5 6 7 8 0 5 10 15 20 25 Index c(3L, 8L, 12L, 15L, 19L, 18L, 18L, 20L)
namespace RProvider
namespace RProvider.graphics
val widgets: int list
module Graphics from RProvider
<summary> Functions for working with R graphics </summary>
val svg: width: float -> height: float -> doPlot: (unit -> Abstractions.RExpr) -> string
<summary>Capture the output of an R function that uses a graphics device into a string.</summary>
<param name="width">Width of the SVG to generate</param>
<param name="height">Height of the SVG to generate</param>
<param name="doPlot">A function that has the side-effect of writing to an active R graphics device.</param>
<returns>An SVG-formatted XML string.</returns>
type R = static member ``!`` : ?paramArray: obj array -> RExpr + 2 overloads static member ``!=`` : ?paramArray: obj array -> RExpr + 2 overloads static member ``!_hexmode`` : ?a: obj -> RExpr + 2 overloads static member ``!_octmode`` : ?a: obj -> RExpr + 2 overloads static member ``$`` : ?paramArray: obj array -> RExpr + 2 overloads static member ``$<-`` : ?paramArray: obj array -> RExpr + 2 overloads static member ``$<-_POSIXlt`` : ?x: obj * ?name: obj * ?value: obj -> RExpr + 2 overloads static member ``$<-_data_frame`` : ?x: obj * ?name: obj * ?value: obj -> RExpr + 2 overloads static member ``$_DLLInfo`` : ?x: obj * ?name: obj -> RExpr + 2 overloads static member ``$_package__version`` : ?x: obj * ?name: obj -> RExpr + 2 overloads ...
Base R functions.
R.plot(paramsByName: List<string * obj>) : Abstractions.RExpr
Generic X-Y Plotting
R.plot(paramsByName: System.Collections.Generic.IDictionary<string,obj>) : Abstractions.RExpr
Generic X-Y Plotting
R.plot(?x: obj, ?y: obj, ?paramArray: obj array) : Abstractions.RExpr
Generic X-Y Plotting
type R = static member Axis: ?x: obj * ?at: obj * ?side: obj * ?labels: obj * ?paramArray: obj array -> RExpr + 2 overloads static member abline: ?a: obj * ?b: obj * ?h: obj * ?v: obj * ?reg: obj * ?coef: obj * ?untf: obj * ?paramArray: obj array -> RExpr + 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 * ?paramArray: obj array -> RExpr + 2 overloads static member assocplot: ?x: obj * ?col: obj * ?space: obj * ?main: obj * ?xlab: obj * ?ylab: obj -> RExpr + 2 overloads static member axTicks: ?side: obj * ?axp: obj * ?usr: obj * ?log: obj * ?nintLog: obj -> RExpr + 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 * ?paramArray: obj array -> RExpr + 2 overloads static member axis_Date: ?side: obj * ?x: obj * ?at: obj * ?format: obj * ?labels: obj * ?paramArray: obj array -> RExpr + 2 overloads static member axis_POSIXct: ?side: obj * ?x: obj * ?at: obj * ?format: obj * ?labels: obj * ?paramArray: obj array -> RExpr + 2 overloads static member barplot: ?height: obj * ?paramArray: obj array -> RExpr + 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 * ?paramArray: obj array -> RExpr + 2 overloads ...
R functions for base graphics.
R.barplot(paramsByName: List<string * obj>) : Abstractions.RExpr
Bar Plots
R.barplot(paramsByName: System.Collections.Generic.IDictionary<string,obj>) : Abstractions.RExpr
Bar Plots
R.barplot(?height: obj, ?paramArray: obj array) : Abstractions.RExpr
Bar Plots
namespace RProvider.ggplot2
namespace RProvider.datasets
val a: 'a
val b: 'b
type R = static member ``%+%`` : ?e1: obj * ?e2: obj -> RExpr + 2 overloads static member ``%+replace%`` : ?e1: obj * ?e2: obj -> RExpr + 2 overloads static member add__gg: ?e1: obj * ?e2: obj -> RExpr + 2 overloads static member aes: ?x: obj * ?y: obj * ?paramArray: obj array -> RExpr + 2 overloads static member aes__: ?x: obj * ?y: obj * ?paramArray: obj array -> RExpr + 2 overloads static member aes__all: ?vars: obj -> RExpr + 2 overloads static member aes__auto: ?data: obj * ?paramArray: obj array -> RExpr + 2 overloads static member aes__q: ?x: obj * ?y: obj * ?paramArray: obj array -> RExpr + 2 overloads static member aes__string: ?x: obj * ?y: obj * ?paramArray: obj array -> RExpr + 2 overloads static member after__scale: ?x: obj -> RExpr + 2 overloads ...
A system for 'declaratively' creating graphics, based on "The Grammar of Graphics". You provide the data, tell 'ggplot2' how to map variables to aesthetics, what graphical primitives to use, and it takes care of the details.
R.ggplot__add(paramsByName: List<string * obj>) : Abstractions.RExpr
No documentation available
R.ggplot__add(paramsByName: System.Collections.Generic.IDictionary<string,obj>) : Abstractions.RExpr
No documentation available
R.ggplot__add(?object: obj, ?plot: obj, ?paramArray: obj array) : Abstractions.RExpr
No documentation available
R.ggplot(paramsByName: List<string * obj>) : Abstractions.RExpr
Create a new ggplot
R.ggplot(paramsByName: System.Collections.Generic.IDictionary<string,obj>) : Abstractions.RExpr
Create a new ggplot
R.ggplot(?data: obj, ?mapping: obj, ?environment: obj, ?paramArray: obj array) : Abstractions.RExpr
Create a new ggplot
type R = static member AirPassengers: RExpr static member BJsales: RExpr static member BJsales_lead: RExpr static member BOD: RExpr static member CO2: RExpr static member ChickWeight: RExpr static member DNase: RExpr static member EuStockMarkets: RExpr static member Formaldehyde: RExpr static member HairEyeColor: RExpr ...
Base R datasets.
property R.mtcars: Abstractions.RExpr with get
R.aes(paramsByName: List<string * obj>) : Abstractions.RExpr
Construct aesthetic mappings
R.aes(paramsByName: System.Collections.Generic.IDictionary<string,obj>) : Abstractions.RExpr
Construct aesthetic mappings
R.aes(?x: obj, ?y: obj, ?paramArray: obj array) : Abstractions.RExpr
Construct aesthetic mappings
argument x: obj
Construct aesthetic mappings
argument y: obj
Construct aesthetic mappings
R.geom__point(paramsByName: List<string * obj>) : Abstractions.RExpr
Points
R.geom__point(paramsByName: System.Collections.Generic.IDictionary<string,obj>) : Abstractions.RExpr
Points
R.geom__point(?mapping: obj, ?data: obj, ?stat: obj, ?position: obj, ?na_rm: obj, ?show_legend: obj, ?inherit_aes: obj, ?paramArray: obj array) : Abstractions.RExpr
Points
namespace RProvider.grDevices
type R = static member CIDFont: ?family: obj * ?cmap: obj * ?cmapEncoding: obj * ?pdfresource: obj -> RExpr + 2 overloads static member Type1Font: ?family: obj * ?metrics: obj * ?encoding: obj -> RExpr + 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 -> RExpr + 2 overloads static member X11Font: ?font: obj -> RExpr + 2 overloads static member X11Fonts: ?paramArray: obj array -> RExpr + 2 overloads static member X11_options: ?reset: obj * ?paramArray: obj array -> RExpr + 2 overloads static member adjustcolor: ?col: obj * ?alpha_f: obj * ?red_f: obj * ?green_f: obj * ?blue_f: obj * ?offset: obj * ?transform: obj -> RExpr + 2 overloads static member as_graphicsAnnot: ?x: obj -> RExpr + 2 overloads static member as_raster: ?x: obj * ?paramArray: obj array -> RExpr + 2 overloads static member axisTicks: ?usr: obj * ?log: obj * ?axp: obj * ?nint: obj -> RExpr + 2 overloads ...
Graphics devices and support for base and grid graphics.
R.png(paramsByName: List<string * obj>) : Abstractions.RExpr
BMP, JPEG, PNG and TIFF graphics devices
R.png(paramsByName: System.Collections.Generic.IDictionary<string,obj>) : Abstractions.RExpr
BMP, JPEG, PNG and TIFF graphics devices
R.png(?filename: obj, ?width: obj, ?height: obj, ?units: obj, ?pointsize: obj, ?bg: obj, ?res: obj, ?``type`` : obj, ?antialias: obj, ?paramArray: obj array) : Abstractions.RExpr
BMP, JPEG, PNG and TIFF graphics devices
R.dev_off(paramsByName: List<string * obj>) : Abstractions.RExpr
No documentation available
R.dev_off(paramsByName: System.Collections.Generic.IDictionary<string,obj>) : Abstractions.RExpr
No documentation available
R.dev_off(?which: obj) : Abstractions.RExpr
No documentation available
module Operators from RProvider
<summary> Custom operators that make composing and working with R symbolic expressions easier. </summary>
val sprokets: float list
val ignore: value: 'T -> unit
R.lines(paramsByName: List<string * obj>) : Abstractions.RExpr
Add Connected Line Segments to a Plot
R.lines(paramsByName: System.Collections.Generic.IDictionary<string,obj>) : Abstractions.RExpr
Add Connected Line Segments to a Plot
R.lines(?x: obj, ?paramArray: obj array) : Abstractions.RExpr
Add Connected Line Segments to a Plot

Type something to start searching.