*56*

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)

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

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)