R:如何使用rCharts绘制统计函数

R:如何使用rCharts绘制统计函数,r,plot,rcharts,R,Plot,Rcharts,我想使用rCharts软件包绘制统计分布(如正态分布)。 我可以用曲线或类似的ggplot2来绘制它 曲线 curve(dnorm, xlim=c(-10,10)) ggplot2 ggplot(data.frame(x=c(-10,10)), aes(x)) + stat_function(fun=dnorm, args=list(0, 1)) 我想用rCharts绘制统计函数,但我做不到。 如何绘制它?您无法使用rChart显式地进行绘制,但对于任何统计分布和您想要的任何函数,您都可

我想使用
rCharts
软件包绘制统计分布(如正态分布)。 我可以用
曲线
或类似的ggplot2来绘制它

曲线

curve(dnorm, xlim=c(-10,10))

ggplot2

ggplot(data.frame(x=c(-10,10)), aes(x)) + stat_function(fun=dnorm, args=list(0, 1))

我想用
rCharts
绘制统计函数,但我做不到。
如何绘制它?

您无法使用
rChart
显式地进行绘制,但对于任何统计分布和您想要的任何函数,您都可以轻松地进行绘制。您可以对每个统计分布使用完全相同的技术,如
d/r/p/q分布
,如
?rnorm
?rbinom
等。但这甚至可以推广到任何您想要的函数。我还提供了一个通用函数的示例

对于正态分布,使用
dnorm
rnorm

x <- rnorm(1000) #you need rnorm here to create 1000 standard normally distributed observations. 
y <- eval(dnorm(x)) #evaluate the function using dnorm now to get probabilities.
#the use of eval() will be clear in the next example. Here you can even omit it if it confuses you.
df <- data.frame(x,y) #make df

#plot
rPlot(y ~ x, data=df, type='line' )

作为如何绘制任何函数的演示和概括

让我们以函数
log(1+x)
为例。在这里,您将看到绘制任何函数是多么容易:

x <- runif(1000,1,10) #1000 points are enough
y <- eval(log(1+x)) #easily evaluate the function for your x vector
#the previous was a very special case where you had 2 functions rnorm and dnorm
#instead of an x vector and an f(x) function like here
#this is very easy to generalize
df <- data.frame(x,y) #make df

#plot
rPlot(y ~ x, data=df, type='line' )

x为什么要对
x
值进行采样,而不是仅仅对正态分布进行适当的排序,例如
x=seq(-6,6,length=1000)
。@Gregor因为他问题中的例子中的OP使用的是标准正态分布,所以我想使用相同且最简单的方法是
rnorm
函数,他似乎也很熟悉,因为他知道
dnorm
。你的例子实际上是一样的,但说得不同。但是,如果OP想要的话,它肯定会发生。不过它改变了限制,所以我添加了一个注释和图表。@Gregor再三想,用你的方式定义整个x轴可能更好。我想,
rnorm
是我第一个想到的东西,可能是因为我经常使用它。再次感谢。我同意这样更好。通过使用
curve()
并指定
xlim
,OP基本上就是在定义x轴。。。默认情况下,曲线使用
101
点。此外,如果您对ggplot2感到满意,则可以使用
ggplot\u构建
作为
rCharts
x <- runif(1000,1,10) #1000 points are enough
y <- eval(log(1+x)) #easily evaluate the function for your x vector
#the previous was a very special case where you had 2 functions rnorm and dnorm
#instead of an x vector and an f(x) function like here
#this is very easy to generalize
df <- data.frame(x,y) #make df

#plot
rPlot(y ~ x, data=df, type='line' )