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)
Graphics.svg 7 4 (fun _ -> R.barplot widgets)
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()
)
|
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" ]
)
<summary> Functions for working with R graphics </summary>
<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>
Base R functions.
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
R functions for base graphics.
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
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.
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
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
Base R datasets.
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
Construct aesthetic mappings
Construct aesthetic mappings
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
Graphics devices and support for base and grid graphics.
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
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
<summary> Custom operators that make composing and working with R symbolic expressions easier. </summary>
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
RProvider