简体   繁体   中英

How to sketch the graph of a density function in R?

Suppose I have a density function of exponential distribution $f(x)= -\\lambda e^{-\\lambda x}$ where $\\lambda = 0.5$.

Is there any quick command that helps drawing the graph of it, instead of manually define that function and plot it? All command I know so far is to plot the graph of a set of sample data, and I don't know how to draw the graph for the population.

A density in R can generally be plotted exactly if the distribution is available in R using the d*** command eg dexp(x,rate) or dnorm(x,mu,sqrt(var)) . For an exponential with lambda=0.5 just use the following commands:

x<-seq(1,10,by=0.01) ### x-axis length
y<-dexp(x,0.5) ### exponential function for range of x
plot(x,y,type="l") ### this will plot your points as a line.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM