본문 바로가기
R

(R)ways to draw curve (function) -curve(), ggplot()

by jangpiano 2020. 8. 13.
반응형

<Ways to draw some famous distributions using curve(), ggplot()>



1. exponential distribution 


> curve(dexp(x,rate=2),from=0,to=6)

 >ggplot(data.frame(x=c(0,6)),aes(x=x))+stat_function(fun=dexp,args=list(rate=2))

 

 



2. Normal distribution 


> curve(dnorm(x,mean=5,sd=1),from=-10,to=10)

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

 

 

 


3. T distribution 


>curve(dt(x,df=3),from=0,to=10)

 >ggplot(data.frame(x=c(0,10)),aes(x=x))+stat_function(fun=dt,args=list(df=3))

 

 



4. Gamma distribution 


> curve(dgamma(x,shape=5,scale=2),from=0,to=10)

 >ggplot(data.frame(x=c(0,10)),aes(x=x))+stat_function(fun=dgamma,args=list(shape=5,scale=2))

 

 


5. Beta distribution 


> curve(dbeta(x,shape1=2,shape2=5),from=0,to=1)

 >ggplot(data.frame(x=c(0,1)),aes(x=x))+stat_function(fun=dbeta,args=list(shape1=2,shape2=5))

 

 



<Ways to make a new curve using curve(), ggplot()>


 > A<-function(x){1/(x^2+9)}

> curve(A(x),from=0, to=10)

 > A<-function(x){1/(x^2+9)}

>ggplot(data.frame(x=c(0,10)),aes(x=x))+stat_function(fun=A,geom="line")

 

 


<Ways to make curves in a graph using curve()>


For this case, the way to make curves in a graph is to use curve() function three times with different parameters and colors for each other. 

You should recognize that x-axis and y-axis of the graph is determined by the curve() function. 

That is the reason why the first graph has y-axis from 0 to 5 and the second graph has y-axis from 0 to 50. 


> curve(dexp(x,rate=5),from=0,to=3)

> curve(dexp(x,rate=20),add=TRUE,col="red")

> curve(dexp(x,rate=50),add=TRUE,col="blue")

> curve(dexp(x,rate=50),from=0,to=3)

> curve(dexp(x,rate=20),add=TRUE,col="red")

> curve(dexp(x,rate=50),add=TRUE,col="blue")

 

 



<Ways to make curves in a graph using ggplot()>


>ggplot(data.frame(x=c(0,3)),aes(x=x))+stat_function(fun=dexp,args=list(rate=5))+stat_function(fun=dexp,args=list(rate=20),col="red")+stat_function(fun=dexp,args=list(rate=50),col="blue")









반응형