gpt4 book ai didi

R 类型提供程序和 Ggplot2

转载 作者:行者123 更新时间:2023-12-03 02:13:43 25 4
gpt4 key购买 nike

有人用过这两个吗?我不介意看到一个快速入门的简短示例。

我可以运行 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;}

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

最佳答案

我认为您需要将结果表达式传递给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 是 Yahoo 的 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)

关于R 类型提供程序和 Ggplot2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16820211/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com