Last active
December 24, 2015 05:19
-
-
Save mathias-brandewinder/6749688 to your computer and use it in GitHub Desktop.
Producing and saving R charts from F# using the R Type Provider,
based on http://www.harding.edu/fmccown/r/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: | |
#r @"..\packages\R.NET.1.5.5\lib\net40\RDotNet.dll" | |
#r @"..\packages\RDotNet.FSharp.0.1.2.1\lib\net40\RDotNet.FSharp.dll" | |
#r @"..\packages\R.NET.1.5.5\lib\net40\RDotNet.NativeLibrary.dll" | |
#r @"..\packages\RProvider.1.0.2\lib\RProvider.dll" | |
open System | |
open RDotNet | |
open RProvider | |
open RProvider.``base`` | |
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: | |
open RProvider.grDevices | |
let path = @"C:/users/mathias/desktop/mychart.png" | |
R.png(filename=path, height=200,width=300,bg="white") | |
R.barplot(widgets) | |
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<string,object> which contains all the arguments passed to the function. | |
Named Arguments | |
As an example, consider the following example: | |
let widgets = [ 3; 8; 12; 15; 19; 18; 18; 20; ] | |
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 a IDictionary of string, object. The string is the name of the argument, and the object its value. | |
The easiest way to use that feature is to leverage the built-in function namedParams, like in this example: | |
let widgets = [ 3; 8; 12; 15; 19; 18; 18; 20; ] | |
let sprockets = [ 5; 4; 6; 7; 12; 9; 5; 6; ] | |
R.plot( | |
namedParams [ | |
"x", box widgets; | |
"type", box "o"; | |
"col", box "blue"; | |
"ylim", box [0; 25] ]) | |
R.lines( | |
namedParams [ | |
"x", box sprockets; | |
"type", box "o"; | |
"pch", box 22; | |
"lty", box 2; | |
"col", box "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: | |
namedParams [ | |
"x", box widgets; | |
"type", box "o"; | |
"col", box "blue"; | |
"ylim", box [0; 25] ] | |
|> R.plot | |
namedParams [ | |
"x", box sprockets; | |
"type", box "o"; | |
"pch", box 22; | |
"lty", box 2; | |
"col", box "red" ] | |
|> R.lines | |
A Complete Example | |
The example that follows is a conversion of the following R tutorial, by Frank McCown: http://www.harding.edu/fmccown/r/ | |
It has been converted step by step in a literal fashion, and should give you a good starting point for how to use optional parameters to create better formatting for your charts. | |
#r @"..\packages\R.NET.1.5.5\lib\net40\RDotNet.dll" | |
#r @"..\packages\RDotNet.FSharp.0.1.2.1\lib\net40\RDotNet.FSharp.dll" | |
#r @"..\packages\R.NET.1.5.5\lib\net40\RDotNet.NativeLibrary.dll" | |
#r @"..\packages\RProvider.1.0.2\lib\RProvider.dll" | |
open System | |
open RDotNet | |
open RProvider | |
open RProvider.``base`` | |
open RProvider.graphics | |
open RProvider.grDevices | |
// Section 1: raw chart | |
let cars = [ 1; 3; 6; 4; 9 ] | |
R.plot(cars) | |
// Section 2: adding a title | |
R.plot( | |
namedParams [ | |
"x", box cars; | |
"type", box "o"; | |
"col", box "blue"; ]) | |
R.plot( | |
namedParams [ | |
"x", box cars; | |
"type", box "o"; | |
"col", box "blue"; ]) | |
R.title( | |
namedParams [ | |
"main", box "Autos"; | |
"col.main", box "red"; | |
"font.main", box 4; ]) | |
// Section 3: 2 series | |
let trucks = [ 2; 5; 4; 5; 12 ] | |
R.plot( | |
namedParams [ | |
"x", box cars; | |
"type", box "o"; | |
"col", box "blue"; | |
"ylim", box [0; 12]; ]) | |
R.lines( | |
namedParams [ | |
"x", box trucks; | |
"type", box "o"; | |
"pch", box 22; | |
"lty", box 2; | |
"col", box "red"; ]) | |
R.title( | |
namedParams [ | |
"main", box "Autos"; | |
"col.main", box "red"; | |
"font.main", box 4; ]) | |
// Section 4: formatting axes, adding legend | |
let g_range = R.range(0, cars, trucks) | |
R.plot( | |
namedParams [ | |
"x", box cars; | |
"type", box "o"; | |
"col", box "blue"; | |
"ylim", box g_range; | |
"axes", box false; | |
"ann", box false; ]) | |
R.axis( | |
namedParams [ | |
"side", box 1; | |
"at", box [1..5]; | |
"lab", box [|"Mon"; "Tue"; "Wed"; "Thu"; "Fri"|] ]) | |
R.axis( | |
namedParams [ | |
"side", box 2; | |
"las", box 1; | |
"at", box [| 0 .. 4 .. 12 |] ]) | |
R.box () | |
R.lines( | |
namedParams [ | |
"x", box trucks; | |
"type", box "o"; | |
"pch", box 22; | |
"lty", box 2; | |
"col", box "red"; ]) | |
R.title( | |
namedParams [ | |
"main", box "Autos"; | |
"col.main", box "red"; | |
"font.main", box 4; ]) | |
R.title( | |
namedParams [ | |
"xlab", box "Days"; | |
"col.lab", R.rgb(0, 0.5, 0) |> box ]); | |
R.title( | |
namedParams [ | |
"ylab", box "Total" ; | |
"col.lab", R.rgb(0, 0.5, 0) |> box ]); | |
R.legend( | |
namedParams [ | |
"x", box 1; | |
"y", (g_range.GetValue<float[]>()).[1] |> box; | |
"legend", box [| "cars"; "trucks" |] ; | |
"cex", box 0.8; | |
"col", box [| "blue"; "red" |]; | |
"pch", box [| 21 .. 22 |]; | |
"lty", box [| 1; 2 |] ]) | |
// Section 5: saving charts | |
let path = @"C:/users/mathias/desktop/figure.png" | |
R.png( | |
namedParams [ | |
"filename", box path; | |
"height", box 295; | |
"width", box 300; | |
"bg", box "white"; ]) | |
R.plot( | |
namedParams [ | |
"x", box cars; | |
"type", box "o"; | |
"col", box "blue"; | |
"ylim", box g_range; | |
"axes", box false; | |
"ann", box false; ]) | |
R.axis( | |
namedParams [ | |
"side", box 1; | |
"at", box [1..5]; | |
"lab", box [|"Mon"; "Tue"; "Wed"; "Thu"; "Fri"|] ]) | |
R.axis( | |
namedParams [ | |
"side", box 2; | |
"las", box 1; | |
"at", box [| 0 .. 4 .. 12 |] ]) | |
R.box () | |
R.lines( | |
namedParams [ | |
"x", box trucks; | |
"type", box "o"; | |
"pch", box 22; | |
"lty", box 2; | |
"col", box "red"; ]) | |
R.title( | |
namedParams [ | |
"main", box "Autos"; | |
"col.main", box "red"; | |
"font.main", box 4; ]) | |
R.title( | |
namedParams [ | |
"xlab", box "Days" ; | |
"col.lab", R.rgb(0, 0.5, 0) |> box ]); | |
R.title( | |
namedParams [ | |
"ylab", box "Total" ; | |
"col.lab", R.rgb(0, 0.5, 0) |> box ]); | |
R.legend( | |
namedParams [ | |
"x", box 1; | |
"legend", box [| "cars"; "trucks" |]; | |
"cex", box 0.8; | |
"col", box [| "blue"; "red" |]; | |
"pch", box [| 21 .. 22 |]; | |
"lty", box [| 1; 2 |] ]) | |
R.dev_off () | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment