2

I would like to plot statistical distributions (like the normal distribution) using the rCharts package. I was able to plot it using curve or ggplot2 like these.

Curve

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

enter image description here

ggplot2

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

enter image description here

I wanted to plot statistical functions using rCharts, but I couldn't. How do I plot it?

LyzandeR
  • 37,047
  • 12
  • 77
  • 87
ksmzn
  • 329
  • 1
  • 2
  • 9

1 Answers1

3

You cannot explicitly do that with rChart but it is very easy to do it on your own for any statistical distribution and in general for any function you want. You can use exactly the same technique for every statistical distribution in the form of d/r/p/q-distribution like ?rnorm, ?rbinom etc. But this can even be generalized for any function you want. I also include an example for a generic function.

For the normal distribution using dnorm and 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' )

enter image description here Similarly, for the binomial distribution you would do exactly the same using dbinom and rbinom. Same for any other distribution.

You can also use the something like x = seq(-6, 6, length = 1000) as per @Gregor's comment instead of the rnorm function to create a custom x variable and and then produce the corresponding probabilities using dnorm. The advantage of this way is that you can set the limits of the x-axis directly. e.g.:

a <- seq(-6,6,length=1000) #use -10,10 to reproduce your example
b <- dnorm(a)
df <- data.frame(a,b)
rPlot(b~a,data=df,type='line')

enter image description here

As a demonstration and a generalization on how to plot any function

Let's use the function log(1+x) as an example. Here you will see how easy it is to plot any function:

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' )

enter image description here

You can use any function you want in the same way!

LyzandeR
  • 37,047
  • 12
  • 77
  • 87
  • Why sample the `x` values instead of just doing an appropriate sequence, e.g. `x = seq(-6, 6, length = 1000)` for the normal? – Gregor Thomas Jan 05 '15 at 22:12
  • @Gregor Because the OP in his example in his question is using the standard normal distribution, so I wanted to use the same and the easiest way is the `rnorm` function, which he also seems to be familiar with since he knows `dnorm`. Your example is practically the same but said differently. It can certainly happen if the OP wants it though. It changes the limits though so I added a note and the graph. – LyzandeR Jan 05 '15 at 22:54
  • @Gregor On second thought it is probably better to use your way and define the entire x-axis. I guess `rnorm` was the first thing that popped into my mind, probably because I use it more often. Thanks again. – LyzandeR Jan 05 '15 at 23:10
  • I agree it's better. Defining the x-axis is essentially what OP is doing by using `curve()` and specifying `xlim`... curve uses `101` points by default. – Gregor Thomas Jan 06 '15 at 00:18
  • also, if you are comfortable with ggplot2, you can use the `ggplot_build` as your data source to `rCharts` – timelyportfolio Jan 07 '15 at 20:43
  • @timelyportfolio Cool, thanks for the comment. I haven't really experimented a lot with ggplot_build in particular although I have been using `ggplot2` for quite a while. If you can show something here which I will add in my answer (or maybe add as a different answer if it is big) please do show me. Thanks a lot! – LyzandeR Jan 07 '15 at 21:10