Home » How to Plot a Log Normal Distribution in R

How to Plot a Log Normal Distribution in R

by Tutor Aspire

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

  • dlnorm(x, meanlog = 0, sdlog = 1) to create the probability density function.
  • curve(function, from = NULL, to = NULL) to plot the probability density function.

For example, the following code illustrates how to plot a probability density function for a log normal distribution with mean = 0 and standard deviation = 1 (on a log scale) where the x-axis of the plot ranges from 0 to 10:

curve(dlnorm(x, meanlog=0, sdlog=1), from=0, to=10)

Log normal distribution plot in R

By default, meanlog = 0 and sdlog =1 which means we can produce the exact same plot without specifying these parameters in the dlnorm() function:

curve(dlnorm(x), from=0, to=10)

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(dlnorm(x), from=0, to=10, 
    main = 'Log Normal Distribution', #add title
    ylab = 'Density', #change y-axis label
    lwd = 2, #increase line width to 2
    col = 'steelblue') #change line color to steelblue 

Log normal distribution plot with title in R

We can also add more than one curve to the graph to compare log normal distributions with different standard deviations. For example, the following code creates log normal distribution plots with sdlog = .3, sdlog = .5, and sdlog = 1:

curve(dlnorm(x, meanlog=0, sdlog=.3), from=0, to=10, col='blue')
curve(dlnorm(x, meanlog=0, sdlog=.5), from=0, to=10, col='red', add=TRUE)
curve(dlnorm(x, meanlog=0, sdlog=1), from=0, to=10, col='purple', add=TRUE)

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

In our example we will use the following syntax to create a legend:

#create density plots
curve(dlnorm(x, meanlog=0, sdlog=.3), from=0, to=10, col='blue')
curve(dlnorm(x, meanlog=0, sdlog=.5), from=0, to=10, col='red', add=TRUE)
curve(dlnorm(x, meanlog=0, sdlog=1), from=0, to=10, col='purple', add=TRUE)

#add legend
legend(6, 1.2, legend=c("sdlog=.3", "sdlog=.5", "sdlog=1"),
       col=c("blue", "red", "purple"), lty=1, cex=1.2)

Multiple log normal density functions in one plot in R

You may also like