How to plot a normal distribution graph in R?

In R language, we can utilize the dnorm() function to generate the probability density function of a normal distribution and use the curve() function to plot the function curve.

Here is an example of drawing a normal distribution graph.

# 生成正态分布的随机数
set.seed(123)
x <- rnorm(1000, mean = 0, sd = 1)

# 绘制正态分布的概率密度函数
curve(dnorm(x, mean = 0, sd = 1), from = -3, to = 3, 
      xlab = "x", ylab = "Density", main = "Normal Distribution")

# 添加网格线
grid()

# 添加直方图
hist(x, prob = TRUE, breaks = 30, add = TRUE, col = "lightblue")

This code will generate a probability density function curve of a normal distribution, and add the corresponding histogram on the same plot.

You can adjust the mean and standard deviation parameters according to your needs to change the mean and standard deviation of the normal distribution, as well as modify the from and to parameters to change the range of the plot.

bannerAds