How can we plot a Weibull distribution in R?

To plot a Weibull distribution in R, we can use the “dweibull” function from the “stats” package. This function takes in the shape and scale parameters of the Weibull distribution and generates a probability density curve. We can then use the “plot” function to plot this curve on a graph. Additionally, we can customize the plot by adding labels, legends, and changing the colors or line types. It is also possible to overlay the theoretical distribution with a histogram of the data to visually compare the fit. Overall, plotting a Weibull distribution in R is a simple and effective way to visualize and analyze data that follows this particular distribution.

Plot a Weibull Distribution in R


To plot the probability density function for a Weibull distribution in R, we can use the following functions:

  • dweibull(x, shape, scale = 1) to create the probability density function.
  • curve(function, from = NULL, to = NULL) to plot the probability density function.

To plot the probability density function, we need to specify the value for the shape and scale parameter in the dweibull function along with the from and to values in the curve() function.

For example, the following code illustrates how to plot a probability density function for a Weibull distribution with parameters shape = 2 and scale = 1 where the x-axis of the plot ranges from 0 to 4:

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

 

Plot of a Weibull distribution in R

We can add a title, change the y-axis label, increase the line width, and even change the line color to make the plot more aesthetically pleasing:

curve(dweibull(x, shape=2, scale = 1), from=0, to=4, 
    main = 'Weibull Distribution (shape = 2, scale = 1)', #add title
    ylab = 'Density', #change y-axis label
    lwd = 2, #increase line width to 2
    col = 'steelblue') #change line color to steelblue

Weibull distribution plot in R

We can also add more than one curve to the graph to compare Weibull distributions with different shape and scale parameters:

curve(dweibull(x, shape=2, scale = 1), from=0, to=4, col='red')
curve(dweibull(x, shape=1.5, scale = 1), from=0, to=4, col='blue', add=TRUE)

Multiple Weibull distribution plots in R

We can add a legend to the plot by using the legend() function, which takes on the following syntax:

legend(x, y=NULL, legend, fill, col, bg, lty, cex)

where:

  • x, y: the x and y coordinates used to position the legend
  • legend: the text to go in the legend
  • fill: fill color inside the legend
  • col: the list of colors to be used for the lines inside the legend
  • bg: the background color for the legend
  • lty: line style
  • cex: text size in the legend
#create density plots
curve(dweibull(x, shape=2, scale = 1), from=0, to=4, col='red')
curve(dweibull(x, shape=1.5, scale = 1), from=0, to=4, col='blue', add=TRUE)

#add legend
legend(2, .7, legend=c("shape=2, scale=1", "shape=1.5, scale=1"),
       col=c("red", "blue"), lty=1, cex=1.2)

Weibull distribution plot in R with a legend

x