R类型提供程序和Ggplot2

5

有人同时使用过这两个吗? 我想看一个简短的示例以启动。

我可以运行example.fsx脚本:acf函数会产生一个图形。 但是我不知道如何显示ggplot图形。

open RProvider.ggplot2
open RProvider.utils

R.setwd @"C:/code/pp/Datasets/output/Kaggle/dontgetkicked"
let f = R.read_csv("measure_DNGtraining.csv")
R.qplot("erase_rate", "components",f)

那会导致一个
val it : SymbolicExpression =
  RDotNet.SymbolicExpression {Engine = RDotNet.REngine;
                              IsClosed = false;
                              IsInvalid = false;
                              IsProtected = true;
                              Type = List;}

我正在阅读说明书,但如果有人手头有片段...


PS:晚上我可以分享更完整的样例;-) - Tomas Petricek
2个回答

12

我认为你需要将结果表达式传递给 R.print

R.qplot("erase_rate", "components",f)
|> R.print 

通过 F# 类型提供程序使用 ggplot2 的问题在于 ggplot2 库太聪明了。我已经试用了一段时间,发现只要使用 qplot 函数,它就能非常好地工作。如果您想要做更复杂的事情,那么最好只需将 R 代码编写为字符串,并调用 R.eval。 做这件事需要:

// Helper function to make calling 'eval' easier
let eval (text:string) =
  R.eval(R.parse(namedParams ["text", text ]))

eval("library(\"ggplot2\")")

// Assuming we have dataframe 'ohlc' with 'Date' and 'Open'
eval("""
  print(
    ggplot(ohlc, aes(x=Date, y=Open)) + 
    geom_line() + 
    geom_smooth()
  )
  """)

我还花了一些时间研究如何将数据从 F# 传递到 R(例如基于 F# 数据创建 R 数据帧,类似于 CSV 类型提供程序)。因此,为了填充 ohlc 数据帧,我使用了以下代码(其中 SampleData 是一个针对雅虎的 CSV 提供程序):

let df =
  [ "Date",  box [| for r in SampleData.msftData -> r.Date |]
    "Open",  box [| for r in SampleData.msftData -> r.Open |]
    "High",  box [| for r in SampleData.msftData -> r.High |]
    "Low",   box [| for r in SampleData.msftData -> r.Low |]
    "Close", box [| for r in SampleData.msftData -> r.Close |] ]
  |> namedParams
  |> R.data_frame
R.assign("ohlc", df)

1
有一篇关于使用ggplot和F# RProvider绘图的博客,作者是evelinag - Peter Siebke

3

正如Tomas所指出的那样,你需要打印出结果才能让ggplot2显示任何内容。

我们可以通过在标准的F#交互启动脚本中添加打印机来实现这一点:

fsi.AddPrinter(fun (sexp: RDotNet.SymbolicExpression) -> sexp.Print())

这使得RProvider更加实用,因为它以与在R中打印结果相同的方式打印每个操作的结果。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接