Open In App

How to Plot a Weibull Distribution in R

In this article, we are going to see how to plot a weibull distribution in R Programming Language. A Weibull distribution is a continuous probability distribution used to analyze life data, model failure times, and access product reliability when modern machines were not available during the olden times.

To plot the Weibull distribution in R we need two functions namely dweibull, and curve().



dweibull(x, shape, scale= 1): 

  • x – vector of quantiles
  • shape – shape parameter.
  • scale – scale parameter.

curve(expr, from = NULL, to = NULL)



  • function – The name of a function, or a call, or an expression written as a function of x which will evaluate an object of the same length as x.
  • from – the start range over which the function will be plotted.
  • to – the end range over which the function will be plotted.

Example 1:

To plot the probability density function of the Weibull distribution, we will have to specify the shape and scale values along with the from and to range in the curve function as shown below.




curve(dweibull(x, shape=2, scale = 1), from=0, to=5)

Output:

Weibull distribution

Example 2:

We can also draw multiple Weibull distributions with different shapes, scales and range by specifying different colors to each distribution as shown below.




curve(dweibull(x, shape=2, scale=1),
      from=0, to=5, col='blue')
  
curve(dweibull(x, shape=3, scale=2),
      from=0, to=7, col='red', add=TRUE)
  
curve(dweibull(x, shape=4, scale=3), 
      from=0, to=10, col='purple', add=TRUE)

Output:

Multiple weibull distribution

Example 3:

We can now add some aesthetics to the Weibull distributions with different shapes, scales, and ranges and include the plot title axis names as shown below.

Let us add parameters like main (main title), ylab – (y-axis label), lwd – (line width), and col ( color). Additionally, we can also pass the shape and scale values to the legend function as shown in the below code snippet.




curve(dweibull(x, shape=3, scale = 1), from=0, to=10, 
    main = 'Weibull Distribution (shape = 2, scale = 1)', # add title
    ylab = 'Density values', # change y-axis label
    lwd = 2, # increase line width to 2
    col = 'red') # change line color to steelblue 
  
# add legend
legend(2, .7, legend=c("shape=2, scale=1"),
       col=c("red"), lty=1, cex=1.5)

Output:

Weibull distribution with x and y-axis names and legend


Article Tags :