├── .gitignore ├── .paket ├── paket.bootstrapper.exe └── paket.targets ├── README.md ├── examples.fsx ├── ggplot-example.png ├── ggplot.fs ├── ggplot.fsproj ├── ggplot.sln ├── paket.dependencies ├── paket.lock └── paket.references /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | # User-specific files 5 | *.suo 6 | *.user 7 | *.sln.docstates 8 | 9 | # Xamarin Studio / monodevelop user-specific 10 | *.userprefs 11 | *.dll.mdb 12 | *.exe.mdb 13 | 14 | # Build results 15 | 16 | [Dd]ebug/ 17 | [Rr]elease/ 18 | x64/ 19 | build/ 20 | [Bb]in/ 21 | [Oo]bj/ 22 | 23 | # MSTest test Results 24 | [Tt]est[Rr]esult*/ 25 | [Bb]uild[Ll]og.* 26 | 27 | *_i.c 28 | *_p.c 29 | *.ilk 30 | *.meta 31 | *.obj 32 | *.pch 33 | *.pdb 34 | *.pgc 35 | *.pgd 36 | *.rsp 37 | *.sbr 38 | *.tlb 39 | *.tli 40 | *.tlh 41 | *.tmp 42 | *.tmp_proj 43 | *.log 44 | *.vspscc 45 | *.vssscc 46 | .builds 47 | *.pidb 48 | *.log 49 | *.scc 50 | 51 | # Visual C++ cache files 52 | ipch/ 53 | *.aps 54 | *.ncb 55 | *.opensdf 56 | *.sdf 57 | *.cachefile 58 | 59 | # Visual Studio profiler 60 | *.psess 61 | *.vsp 62 | *.vspx 63 | 64 | # Other Visual Studio data 65 | .vs/ 66 | 67 | # Guidance Automation Toolkit 68 | *.gpState 69 | 70 | # ReSharper is a .NET coding add-in 71 | _ReSharper*/ 72 | *.[Rr]e[Ss]harper 73 | 74 | # TeamCity is a build add-in 75 | _TeamCity* 76 | 77 | # DotCover is a Code Coverage Tool 78 | *.dotCover 79 | 80 | # NCrunch 81 | *.ncrunch* 82 | .*crunch*.local.xml 83 | 84 | # Installshield output folder 85 | [Ee]xpress/ 86 | 87 | # DocProject is a documentation generator add-in 88 | DocProject/buildhelp/ 89 | DocProject/Help/*.HxT 90 | DocProject/Help/*.HxC 91 | DocProject/Help/*.hhc 92 | DocProject/Help/*.hhk 93 | DocProject/Help/*.hhp 94 | DocProject/Help/Html2 95 | DocProject/Help/html 96 | 97 | # Click-Once directory 98 | publish/ 99 | 100 | # Publish Web Output 101 | *.Publish.xml 102 | 103 | # Enable nuget.exe in the .nuget folder (though normally executables are not tracked) 104 | !.nuget/NuGet.exe 105 | 106 | # Windows Azure Build Output 107 | csx 108 | *.build.csdef 109 | 110 | # Windows Store app package directory 111 | AppPackages/ 112 | 113 | # Others 114 | sql/ 115 | *.Cache 116 | ClientBin/ 117 | [Ss]tyle[Cc]op.* 118 | ~$* 119 | *~ 120 | *.dbmdl 121 | *.[Pp]ublish.xml 122 | *.pfx 123 | *.publishsettings 124 | 125 | # RIA/Silverlight projects 126 | Generated_Code/ 127 | 128 | # Backup & report files from converting an old project file to a newer 129 | # Visual Studio version. Backup files are not needed, because we have git ;-) 130 | _UpgradeReport_Files/ 131 | Backup*/ 132 | UpgradeLog*.XML 133 | UpgradeLog*.htm 134 | 135 | # SQL Server files 136 | App_Data/*.mdf 137 | App_Data/*.ldf 138 | 139 | 140 | #LightSwitch generated files 141 | GeneratedArtifacts/ 142 | _Pvt_Extensions/ 143 | ModelManifest.xml 144 | 145 | # ========================= 146 | # Windows detritus 147 | # ========================= 148 | 149 | # Windows image file caches 150 | Thumbs.db 151 | ehthumbs.db 152 | 153 | # Folder config file 154 | Desktop.ini 155 | 156 | # Recycle Bin used on file shares 157 | $RECYCLE.BIN/ 158 | 159 | # Mac desktop service store files 160 | .DS_Store 161 | 162 | # =================================================== 163 | # Exclude F# project specific directories and files 164 | # =================================================== 165 | 166 | # NuGet Packages Directory 167 | packages/ 168 | 169 | # Generated documentation folder 170 | docs/output/ 171 | 172 | # Temp folder used for publishing docs 173 | temp/ 174 | 175 | # Test results produced by build 176 | TestResults.xml 177 | 178 | # Nuget outputs 179 | nuget/*.nupkg 180 | release.cmd 181 | release.sh 182 | localpackages/ 183 | paket-files 184 | *.orig 185 | .paket/paket.exe 186 | docs/content/license.md 187 | docs/content/release-notes.md 188 | .fake 189 | docs/tools/FSharp.Formatting.svclog -------------------------------------------------------------------------------- /.paket/paket.bootstrapper.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/evelinag/ffplot/ae2aee5ce08aee4ce95c071737aa31480dbd423c/.paket/paket.bootstrapper.exe -------------------------------------------------------------------------------- /.paket/paket.targets: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | true 6 | 7 | true 8 | $(MSBuildThisFileDirectory) 9 | $(MSBuildThisFileDirectory)..\ 10 | /Library/Frameworks/Mono.framework/Commands/mono 11 | mono 12 | 13 | 14 | 15 | $(PaketToolsPath)paket.exe 16 | $(PaketToolsPath)paket.bootstrapper.exe 17 | "$(PaketExePath)" 18 | $(MonoPath) --runtime=v4.0.30319 "$(PaketExePath)" 19 | "$(PaketBootStrapperExePath)" 20 | $(MonoPath) --runtime=v4.0.30319 $(PaketBootStrapperExePath) 21 | 22 | $(MSBuildProjectDirectory)\paket.references 23 | $(MSBuildStartupDirectory)\paket.references 24 | $(MSBuildProjectFullPath).paket.references 25 | $(PaketCommand) restore --references-files "$(PaketReferences)" 26 | $(PaketBootStrapperCommand) 27 | 28 | RestorePackages; $(BuildDependsOn); 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ffplot: F# wrapper for ggplot2 2 | 3 | [ggplot2](http://ggplot2.org/) is a great R library for flexible visualizations, 4 | based on compositional design of plots. Unfortunately, using it from F# can be 5 | sometimes cumbersome. To simplify using ggplot2 from F#, I put together a 6 | simple wrapper, ffplot. 7 | 8 | You can find more information on my [blog](http://evelinag.com/blog/2015/12-03-using-ggplot2-from-f/index.html). 9 | 10 | ## Requirements: 11 | To use ggplot2 from F#, you'll need the following tools: 12 | 13 | - [R](https://www.r-project.org/) installation with the [ggplot2](http://ggplot2.org/) 14 | package. 15 | - [RProvider](http://bluemountaincapital.github.io/FSharpRProvider/) - 16 | F# type provider for calling R from F#. 17 | - Optional: [FsLab](http://fslab.org/) collects all the necessary dependencies and simplifies other data science tasks in F#. 18 | 19 | ## Usage: 20 | 21 | To use `ffplot`, simply reference the `ggplot.fs` file from your solution. 22 | You can also add the file using the [paket](https://fsprojects.github.io/Paket/index.html) dependency manager 23 | by adding the following line into your `paket.dependencies` file: 24 | 25 | ``` 26 | github evelinag/ffplot ggplot.fs 27 | ``` 28 | See [paket documentation](https://fsprojects.github.io/Paket/github-dependencies.html) for more details on referencing Github dependencies. 29 | 30 | For creating ggplot2 plots, see `examples.fsx` with both simple and more complex examples showing how to use ffplot in F#. 31 | 32 | ![ggplot2 example](ggplot-example.png) 33 | -------------------------------------------------------------------------------- /examples.fsx: -------------------------------------------------------------------------------- 1 | #I "packages/Deedle/lib/net40" 2 | #I "packages/Deedle.RPlugin/lib/net40" 3 | #I "packages/RProvider/lib/net40" 4 | #I "packages/R.NET.Community/lib/net40" 5 | #I "packages/R.NET.Community.FSharp/lib/net40" 6 | 7 | #r "RProvider.Runtime.dll" 8 | #r "RProvider.dll" 9 | #r "RDotNet.dll" 10 | #r "RDotNet.NativeLibrary.dll" 11 | #r "RDotNet.FSharp.dll" 12 | #r "Deedle.dll" 13 | #r "Deedle.RProvider.Plugin.dll" 14 | 15 | #load "ggplot.fs" 16 | 17 | open Deedle 18 | open RProvider 19 | open RProvider.ggplot2 20 | open RProvider.datasets 21 | open ggplot 22 | 23 | fsi.AddPrinter(fun (synexpr:RDotNet.SymbolicExpression) -> 24 | synexpr.Print()) 25 | 26 | (** 27 | Using ggplot2 from F# with RProvider 28 | ==================================================== 29 | This file contains various examples of using ggplot in F#, with 30 | the ffplot wrapper. 31 | 32 | First we load some example datasets: 33 | *) 34 | 35 | // mtcars dataset 36 | let mtc = R.mtcars.GetValue>() 37 | 38 | // Iris dataset 39 | let iris = R.iris.GetValue>() 40 | 41 | // Diamonds dataset 42 | let diamonds = R.diamonds.GetValue>() 43 | 44 | //------------------------------------------------- 45 | // Scatter plots 46 | //------------------------------------------------- 47 | 48 | G.ggplot(mtc, R.aes__string(x="disp", y="drat")) 49 | ++ R.geom__point() 50 | 51 | G.ggplot(mtc, G.aes(x="disp", y="drat")) 52 | ++ R.geom__point() 53 | 54 | 55 | // 3 ways to create the same plot using different ggplot initialisations 56 | G.ggplot(iris, G.aes("Sepal.Width", "Sepal.Length")) 57 | ++ R.geom__point() 58 | 59 | G.ggplot() 60 | ++ R.geom__point(data=iris, mapping=G.aes("Sepal.Width", "Sepal.Length")) 61 | 62 | G.ggplot(data=iris) 63 | ++ R.geom__point(G.aes("Sepal.Width", "Sepal.Length")) 64 | 65 | //------------------------------------------------- 66 | // Bar plots 67 | //------------------------------------------------- 68 | 69 | G.ggplot(mtc, R.aes(R.factor(mtc?cyl))) 70 | ++ R.geom__bar() 71 | ++ R.coord__flip() 72 | 73 | G.ggplot(diamonds, G.aes("clarity", fill="cut")) 74 | ++ R.geom__bar() 75 | 76 | // Faceting 77 | G.ggplot(diamonds, G.aes("clarity")) 78 | ++ R.geom__bar() 79 | ++ R.facet__wrap(R.eval(R.parse(text="~cut"))) 80 | 81 | //------------------------------------------------- 82 | // Line plots 83 | //------------------------------------------------- 84 | 85 | // Calling ffplot with a manually created R data frame 86 | let x = [0.0 .. 0.1 .. 10.0] 87 | let y = x |> List.map (fun value -> sin(value)) 88 | let dataframe = 89 | namedParams ["X", x; "Value", y] 90 | |> R.data_frame 91 | 92 | G.ggplot(dataframe, G.aes(x="X", y="Value")) 93 | ++ R.geom__line() 94 | 95 | G.ggplot(diamonds, G.aes(x="carat", y="price")) 96 | ++ R.geom__point() 97 | ++ R.geom__smooth() 98 | 99 | G.ggplot(mtc, G.aes("mpg", "wt")) 100 | ++ R.geom__point() 101 | ++ R.geom__smooth() 102 | 103 | //------------------------------------------------- 104 | // Density plots 105 | //------------------------------------------------- 106 | 107 | G.ggplot(diamonds, G.aes("carat")) 108 | ++ R.geom__density() 109 | 110 | G.ggplot(diamonds, G.aes(x="carat", colour="color", fill="color")) 111 | ++ R.geom__density() 112 | 113 | //------------------------------------------------- 114 | // Histograms 115 | //------------------------------------------------- 116 | 117 | G.ggplot(diamonds, G.aes("carat")) 118 | ++ R.geom__histogram() 119 | 120 | G.ggplot(diamonds, G.aes("carat", fill="color")) 121 | ++ R.geom__histogram(namedParams["binwidth", 0.1]) 122 | 123 | //------------------------------------------------- 124 | // Working with categorical values 125 | //------------------------------------------------- 126 | 127 | G.ggplot(diamonds, G.aes(x="color", y="price/carat")) 128 | ++ R.geom__point() 129 | 130 | G.ggplot(diamonds, G.aes(x="color", y="price/carat")) 131 | ++ R.geom__jitter() 132 | 133 | //------------------------------------------------- 134 | // Using more complex aesthetics 135 | //------------------------------------------------- 136 | 137 | // use R.aes__string function 138 | 139 | G.ggplot(diamonds, G.aes(x="color", y="price/carat")) 140 | ++ R.geom__boxplot(R.aes__string(namedParams["fill", "color"])) 141 | 142 | //------------------------------------------------- 143 | // Composing more complex plots 144 | //------------------------------------------------- 145 | 146 | // change sizes of axes labels and legends 147 | let sizeSettings () = 148 | R.theme(namedParams["axis.text", R.element__text(namedParams["size", 12])]) 149 | ++ R.theme(namedParams["legend.text", R.element__text(namedParams["size", 12])]) 150 | ++ R.theme(namedParams["axis.title", R.element__text(namedParams["size", 14])]) 151 | ++ R.theme(namedParams["plot.title", R.element__text(namedParams["size", 18])]) 152 | 153 | // Create a plot 154 | G.ggplot(iris, G.aes(x="Sepal.Length", y="Sepal.Width",colour="Petal.Length")) 155 | ++ R.geom__point(namedParams["size", 4]) 156 | ++ R.theme__bw() 157 | ++ R.scale__color__gradient( 158 | namedParams["low", "blue"; "high", "gold"]) 159 | ++ R.ggtitle("Iris dataset") 160 | ++ R.xlab("Sepal length") 161 | ++ R.ylab("Sepal width") 162 | ++ sizeSettings() 163 | 164 | -------------------------------------------------------------------------------- /ggplot-example.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/evelinag/ffplot/ae2aee5ce08aee4ce95c071737aa31480dbd423c/ggplot-example.png -------------------------------------------------------------------------------- /ggplot.fs: -------------------------------------------------------------------------------- 1 | module ggplot 2 | 3 | open Deedle 4 | open RProvider 5 | open RProvider.``base`` 6 | open RProvider.ggplot2 7 | 8 | let (++) (plot1:RDotNet.SymbolicExpression) (plot2:RDotNet.SymbolicExpression) = 9 | R.``+``(plot1, plot2) 10 | 11 | /// Wrapper around common ways of initialising a ggplot object 12 | type G = 13 | static member ggplot() = R.ggplot() 14 | 15 | static member ggplot(dataframe:RDotNet.SymbolicExpression, ?aes:RDotNet.SymbolicExpression) = 16 | let parameters = 17 | ("data", box dataframe):: 18 | (match aes with 19 | | None -> [] 20 | | Some a -> ["mapping", box a]) 21 | 22 | namedParams parameters 23 | |> R.ggplot 24 | 25 | static member ggplot(data:Frame<_,_>, ?aes:RDotNet.SymbolicExpression) = 26 | let df = R.as_data_frame(data) 27 | match aes with 28 | | None -> G.ggplot(df) 29 | | Some a -> G.ggplot(df, a) 30 | 31 | /// colour in aes is specified by data column name 32 | static member aes(x:string, ?y:string, ?colour:string, ?fill:string) = 33 | let parameters = 34 | [ Some ("x", x) 35 | (match y with | Some value -> Some("y", value) | None -> None) 36 | (match colour with | Some c -> Some("colour", c) | None -> None) 37 | (match fill with | Some c -> Some("fill", c) | None -> None) ] 38 | |> List.choose id 39 | R.aes__string(namedParams parameters) 40 | 41 | -------------------------------------------------------------------------------- /ggplot.fsproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | 2.0 8 | 4a97769d-6ce7-4416-b125-d3df49740055 9 | Exe 10 | ggplot 11 | ggplot 12 | v4.5 13 | true 14 | 4.3.1.0 15 | ggplot 16 | 17 | 18 | true 19 | full 20 | false 21 | false 22 | bin\Debug\ 23 | DEBUG;TRACE 24 | 3 25 | AnyCPU 26 | bin\Debug\ggplot.XML 27 | true 28 | 29 | 30 | pdbonly 31 | true 32 | true 33 | bin\Release\ 34 | TRACE 35 | 3 36 | AnyCPU 37 | bin\Release\ggplot.XML 38 | true 39 | 40 | 41 | 11 42 | 43 | 44 | 45 | 46 | $(MSBuildExtensionsPath32)\..\Microsoft SDKs\F#\3.0\Framework\v4.0\Microsoft.FSharp.Targets 47 | 48 | 49 | 50 | 51 | $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\FSharp\Microsoft.FSharp.Targets 52 | 53 | 54 | 55 | 56 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | True 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | packages\Deedle\lib\net40\Deedle.dll 84 | True 85 | True 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | packages\DynamicInterop\lib\net40\DynamicInterop.dll 95 | True 96 | True 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | packages\R.NET.Community\lib\net40\RDotNet.NativeLibrary.dll 106 | True 107 | True 108 | 109 | 110 | packages\R.NET.Community\lib\net40\RDotNet.dll 111 | True 112 | True 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | packages\R.NET.Community.FSharp\lib\net40\RDotNet.FSharp.dll 122 | True 123 | True 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | packages\RProvider\lib\net40\RProvider.Runtime.dll 133 | True 134 | True 135 | 136 | 137 | packages\RProvider\lib\net40\RProvider.dll 138 | True 139 | True 140 | 141 | 142 | 143 | 144 | -------------------------------------------------------------------------------- /ggplot.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 2013 4 | VisualStudioVersion = 12.0.31101.0 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "ggplot", "ggplot.fsproj", "{4A97769D-6CE7-4416-B125-D3DF49740055}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Any CPU = Debug|Any CPU 11 | Release|Any CPU = Release|Any CPU 12 | EndGlobalSection 13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 14 | {4A97769D-6CE7-4416-B125-D3DF49740055}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 15 | {4A97769D-6CE7-4416-B125-D3DF49740055}.Debug|Any CPU.Build.0 = Debug|Any CPU 16 | {4A97769D-6CE7-4416-B125-D3DF49740055}.Release|Any CPU.ActiveCfg = Release|Any CPU 17 | {4A97769D-6CE7-4416-B125-D3DF49740055}.Release|Any CPU.Build.0 = Release|Any CPU 18 | EndGlobalSection 19 | GlobalSection(SolutionProperties) = preSolution 20 | HideSolutionNode = FALSE 21 | EndGlobalSection 22 | EndGlobal 23 | -------------------------------------------------------------------------------- /paket.dependencies: -------------------------------------------------------------------------------- 1 | source https://nuget.org/api/v2 2 | nuget Deedle 3 | nuget Deedle.RPlugin 4 | nuget RProvider -------------------------------------------------------------------------------- /paket.lock: -------------------------------------------------------------------------------- 1 | NUGET 2 | remote: https://nuget.org/api/v2 3 | specs: 4 | Deedle (1.2.4) 5 | Deedle.RPlugin (1.2.4) 6 | Deedle (>= 1.2.4) 7 | R.NET.Community (>= 1.6.4) 8 | R.NET.Community.FSharp (>= 1.6.4) 9 | RProvider (>= 1.1.13) 10 | DynamicInterop (0.7.4) 11 | R.NET.Community (1.6.4) 12 | DynamicInterop (0.7.4) 13 | R.NET.Community.FSharp (1.6.4) 14 | R.NET.Community (>= 1.6.4) 15 | RProvider (1.1.15) 16 | DynamicInterop (0.7.4) 17 | R.NET.Community (1.6.4) 18 | R.NET.Community.FSharp (1.6.4) 19 | -------------------------------------------------------------------------------- /paket.references: -------------------------------------------------------------------------------- 1 | RProvider 2 | Deedle 3 | --------------------------------------------------------------------------------