如何通过标记x轴的特定部分来绘制正态分布?

时间:2022-02-24 00:44:20

I am using the following code to create a standard normal distribution in R:

我使用以下代码在R中创建一个标准正态分布:

x <- seq(-4, 4, length=200)y <- dnorm(x, mean=0, sd=1)plot(x, y, type="l", lwd=2)

I need the x-axis to be labeled at the mean and at points three standard deviations above and below the mean. How can I add these labels?

我需要x轴标记在均值和三个标准差处。我如何添加这些标签?

6 个解决方案

#1


18  

The easiest (but not general) way is to restrict the limits of the x axis. The +/- 1:3 sigma will be labeled as such, and the mean will be labeled as 0 - indicating 0 deviations from the mean.

最简单的(但不是一般的)方法是限制x轴的极限。+/- 1:3西格玛将被这样标记,而均值将被标记为0 -表示离均值0个偏差。

plot(x,y, type = "l", lwd = 2, xlim = c(-3.5,3.5))

如何通过标记x轴的特定部分来绘制正态分布?

Another option is to use more specific labels:

另一种选择是使用更具体的标签:

plot(x,y, type = "l", lwd = 2, axes = FALSE, xlab = "", ylab = "")axis(1, at = -3:3, labels = c("-3s", "-2s", "-1s", "mean", "1s", "2s", "3s"))

#2


14  

Using the code in this answer, you could skip creating x and just use curve() on the dnorm function:

使用这个答案中的代码,您可以跳过创建x,只在dnorm函数上使用curve():

curve(dnorm, -3.5, 3.5, lwd=2, axes = FALSE, xlab = "", ylab = "")axis(1, at = -3:3, labels = c("-3s", "-2s", "-1s", "mean", "1s", "2s", "3s"))

But this doesn't use the given code anymore.

但它不再使用给定的代码。

#3


5  

An extremely inefficient and unusual, but beautiful solution, which works based on the ideas of Monte Carlo simulation, is this:

根据蒙特卡罗模拟的思想,有一种效率极低、不寻常但很漂亮的解决方案:

  1. simulate many draws (or samples) from a given distribution (say the normal).
  2. 模拟从一个给定的分布中抽取(或样本)。
  3. plot the density of these draws using rnorm. The rnorm function takes as arguments (A,B,C) and returns a vector of A samples from a normal distribution centered at B, with standard deviation C.
  4. 使用rnorm绘制这些绘制的密度。rnorm函数以参数(A,B,C)作为参数,并从以B为中心的正态分布中返回一个样本的向量。

Thus to take a sample of size 50,000 from a standard normal (i.e, a normal with mean 0 and standard deviation 1), and plot its density, we do the following:

因此,从标准法线(i)中抽取一个大小为50,000的样本。e,均值为0,标准差为1的法线,绘制密度,我们做如下:

x = rnorm(50000,0,1)plot(density(x))

As the number of draws goes to infinity this will converge in distribution to the normal. To illustrate this, see the image below which shows from left to right and top to bottom 5000,50000,500000, and 5 million samples. 如何通过标记x轴的特定部分来绘制正态分布?

当绘制次数趋于无穷大时,它将收敛于正态分布。为了说明这一点,请看下面的图片,从左到右,从上到下依次是5000、50000、500000和500万个样本。

#4


3  

If you like hard way of doing something without using R built in function or you want to do this outside R, you can use the following formula.

如果你喜欢不使用R内置函数的方法或者你想在R之外做这个,你可以使用下面的公式。

如何通过标记x轴的特定部分来绘制正态分布?

x<-seq(-4,4,length=200)s = 1mu = 0y <- (1/(s * sqrt(2*pi))) * exp(-((x-mu)^2)/(2*s^2))plot(x,y, type="l", lwd=2, col = "blue", xlim = c(-3.5,3.5))

#5


2  

In general case, for example: Normal(2, 1)

一般情况下,例如:Normal(2,1)

f <- function(x) dnorm(x, 2, 1)plot(f, -1, 5)

This is a very general, f can be defined freely, with any given parameters, for example:

这是很一般的,f可以*定义,有任意给定的参数,例如:

f <- function(x) dbeta(x, 0.1, 0.1)plot(f, 0, 1)

#6


0  

I particularly love Lattice for this goal. It easily implements graphical information such as specific areas under a curve, the one you usually require when dealing with probabilities problems such as find P(a < X < b) etc. Please have a look:

我特别喜欢这个目标。它很容易实现图形化信息,如曲线下的具体区域,这是处理概率问题时通常需要的,如查找P(a < X < b)等。请看:

library(lattice)e4a <- seq(-4, 4, length = 10000)            # Data to set up out normale4b <- dnorm(e4a, 0, 1)         xyplot(e4b ~ e4a,                   # Lattice xyplot               type = "l",               main = "Plot 2",               panel = function(x,y, ...){                   panel.xyplot(x,y, ...)                   panel.abline( v = c(0, 1, 1.5), lty = 2)  #set z and lines                   xx <- c(1, x[x>=1 & x<=1.5], 1.5)         #Color area                   yy <- c(0,   y[x>=1 & x<=1.5], 0)                    panel.polygon(xx,yy, ..., col='red')               })

如何通过标记x轴的特定部分来绘制正态分布?

In this example I make the area between z = 1 and z = 1.5 stand out. You can move easily this parameters according to your problem.

在这个例子中,我使z = 1和z = 1.5之间的区域突出。您可以根据您的问题轻松地移动这些参数。

Axis labels are automatic.

轴标签是自动的。

#1


18  

The easiest (but not general) way is to restrict the limits of the x axis. The +/- 1:3 sigma will be labeled as such, and the mean will be labeled as 0 - indicating 0 deviations from the mean.

最简单的(但不是一般的)方法是限制x轴的极限。+/- 1:3西格玛将被这样标记,而均值将被标记为0 -表示离均值0个偏差。

plot(x,y, type = "l", lwd = 2, xlim = c(-3.5,3.5))

如何通过标记x轴的特定部分来绘制正态分布?

Another option is to use more specific labels:

另一种选择是使用更具体的标签:

plot(x,y, type = "l", lwd = 2, axes = FALSE, xlab = "", ylab = "")axis(1, at = -3:3, labels = c("-3s", "-2s", "-1s", "mean", "1s", "2s", "3s"))

#2


14  

Using the code in this answer, you could skip creating x and just use curve() on the dnorm function:

使用这个答案中的代码,您可以跳过创建x,只在dnorm函数上使用curve():

curve(dnorm, -3.5, 3.5, lwd=2, axes = FALSE, xlab = "", ylab = "")axis(1, at = -3:3, labels = c("-3s", "-2s", "-1s", "mean", "1s", "2s", "3s"))

But this doesn't use the given code anymore.

但它不再使用给定的代码。

#3


5  

An extremely inefficient and unusual, but beautiful solution, which works based on the ideas of Monte Carlo simulation, is this:

根据蒙特卡罗模拟的思想,有一种效率极低、不寻常但很漂亮的解决方案:

  1. simulate many draws (or samples) from a given distribution (say the normal).
  2. 模拟从一个给定的分布中抽取(或样本)。
  3. plot the density of these draws using rnorm. The rnorm function takes as arguments (A,B,C) and returns a vector of A samples from a normal distribution centered at B, with standard deviation C.
  4. 使用rnorm绘制这些绘制的密度。rnorm函数以参数(A,B,C)作为参数,并从以B为中心的正态分布中返回一个样本的向量。

Thus to take a sample of size 50,000 from a standard normal (i.e, a normal with mean 0 and standard deviation 1), and plot its density, we do the following:

因此,从标准法线(i)中抽取一个大小为50,000的样本。e,均值为0,标准差为1的法线,绘制密度,我们做如下:

x = rnorm(50000,0,1)plot(density(x))

As the number of draws goes to infinity this will converge in distribution to the normal. To illustrate this, see the image below which shows from left to right and top to bottom 5000,50000,500000, and 5 million samples. 如何通过标记x轴的特定部分来绘制正态分布?

当绘制次数趋于无穷大时,它将收敛于正态分布。为了说明这一点,请看下面的图片,从左到右,从上到下依次是5000、50000、500000和500万个样本。

#4


3  

If you like hard way of doing something without using R built in function or you want to do this outside R, you can use the following formula.

如果你喜欢不使用R内置函数的方法或者你想在R之外做这个,你可以使用下面的公式。

如何通过标记x轴的特定部分来绘制正态分布?

x<-seq(-4,4,length=200)s = 1mu = 0y <- (1/(s * sqrt(2*pi))) * exp(-((x-mu)^2)/(2*s^2))plot(x,y, type="l", lwd=2, col = "blue", xlim = c(-3.5,3.5))

#5


2  

In general case, for example: Normal(2, 1)

一般情况下,例如:Normal(2,1)

f <- function(x) dnorm(x, 2, 1)plot(f, -1, 5)

This is a very general, f can be defined freely, with any given parameters, for example:

这是很一般的,f可以*定义,有任意给定的参数,例如:

f <- function(x) dbeta(x, 0.1, 0.1)plot(f, 0, 1)

#6


0  

I particularly love Lattice for this goal. It easily implements graphical information such as specific areas under a curve, the one you usually require when dealing with probabilities problems such as find P(a < X < b) etc. Please have a look:

我特别喜欢这个目标。它很容易实现图形化信息,如曲线下的具体区域,这是处理概率问题时通常需要的,如查找P(a < X < b)等。请看:

library(lattice)e4a <- seq(-4, 4, length = 10000)            # Data to set up out normale4b <- dnorm(e4a, 0, 1)         xyplot(e4b ~ e4a,                   # Lattice xyplot               type = "l",               main = "Plot 2",               panel = function(x,y, ...){                   panel.xyplot(x,y, ...)                   panel.abline( v = c(0, 1, 1.5), lty = 2)  #set z and lines                   xx <- c(1, x[x>=1 & x<=1.5], 1.5)         #Color area                   yy <- c(0,   y[x>=1 & x<=1.5], 0)                    panel.polygon(xx,yy, ..., col='red')               })

如何通过标记x轴的特定部分来绘制正态分布?

In this example I make the area between z = 1 and z = 1.5 stand out. You can move easily this parameters according to your problem.

在这个例子中,我使z = 1和z = 1.5之间的区域突出。您可以根据您的问题轻松地移动这些参数。

Axis labels are automatic.

轴标签是自动的。