How to Plot a Log Normal Distribution in R

To plot a log-normal distribution in R, you can use the dlnorm() function to generate the probability density function (PDF) of the log-normal distribution, and then plot it using the plot() function.

Here’s an example code that generates a log-normal distribution with a mean of 2 and a standard deviation of 1, and then plots it:

 

# Generate data for the log-normal distribution
x <- seq(0.01, 10, length.out = 1000)
mu <- 2
sigma <- 1
y <- dlnorm(x, meanlog = mu, sdlog = sigma)

# Plot the log-normal distribution
plot(
x,
y,
type = "l",
xlab = "x",
ylab = "Density",
main = "Log-Normal Distribution"
)

In this code, seq(0.01, 10, length.out = 1000) generates a sequence of 1000 equally spaced values from 0.01 to 10, which will be used as the x-axis values for the plot. meanlog and sdlog parameters of the dlnorm() function specify the mean and standard deviation of the underlying normal distribution in the logarithmic scale. type = "l" specifies that the plot should be a line plot, and xlab, ylab, and main are used to set the axis labels and the plot title.

Example – 2

Here is an example code that generates a log-normal distribution with mean = 0 and standard deviation = 1, and plots the PDF:

# Set the mean and standard deviation
mu <- 0
sigma <- 1

# Generate a sequence of x values
x <- seq(from = 0.01, to = 10, by = 0.01)

# Calculate the PDF using dlnorm()
pdf <- dlnorm(x, meanlog = mu, sdlog = sigma)

# Plot the PDF using plot()
plot(
x,
pdf,
type = "l",
lwd = 2,
xlab = "x",
ylab = "PDF"
)

This code generates a log-normal distribution with a mean of 0 and a standard deviation of 1, and plots the resulting PDF on the x-axis. The type = "l" argument specifies that we want to plot a line graph, and lwd = 2 sets the line width to 2. The xlab and ylab arguments are used to label the x- and y-axes, respectively.

You can adjust the values of mu and sigma to create log-normal distributions with different means and standard deviations. You can also adjust the x sequence to change the range and resolution of the x-axis.

Exponential Distribution in R Programming

Continuous Uniform Distribution in R