English 中文(简体)
如何通过贴上轴心特定部分的标签来划定正常的分布情况?
原标题:How to plot a normal distribution by labeling specific parts of the x-axis?
  • 时间:2012-05-07 20:54:59
  •  标签:
  • r

我正在使用以下法典,在R创造标准正常分配:

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

我需要将X-轴贴上上面和以下三个标准偏离点的标签。 我如何添加这些标签?

最佳回答

最容易(但并非一般)的方式是限制轴心的极限。 +/-1:3 sigma将贴上标签,平均标记为0-表示偏离平均值。

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

“entergraph

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"))
问题回答

如果你不使用R建构,或想在R外做,那么你会使用以下公式。

enter image description here

x<-seq(-4,4,length=200)
s = 1
mu = 0
y <- (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))

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

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

这是非常笼统的, f 可自由界定,并包含任何特定参数,例如:

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

在蒙特卡洛模拟思想基础上开展工作的一个极端低效和异常但美丽的解决办法是:

  1. simulate many draws (or samples) from a given distribution (say the normal).
  2. 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.

因此,从标准正常(即平均0和标准偏离1的正常状态)中抽出50 000人的面积,并划定其密度。

x = rnorm(50000,0,1)

plot(density(x))

随着人数的推移,这一数目将趋向于向正常分配。 为了说明这一点,见以下图象:从左到右上到底,5000 500 000和500万个样本:5000,5000获得500万个样本。

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:

library(lattice)

e4a <- seq(-4, 4, length = 10000)            # Data to set up out normal
e4b <- 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 )
               })

“enterography

在此例子中,我把<代码>z = 1和z = 1.5/code>之间的区域划出。 你可以根据你的问题轻而易举地改变这一参数。

轴标签是自动的。





相关问题
How to plot fitted model over observed time series

This is a really really simple question to which I seem to be entirely unable to get a solution. I would like to do a scatter plot of an observed time series in R, and over this I want to plot the ...

REvolution for R

since the latest Ubuntu release (karmic koala), I noticed that the internal R package advertises on start-up the REvolution package. It seems to be a library collection for high-performance matrix ...

R - capturing elements of R output into text files

I am trying to run an analysis by invoking R through the command line as follows: R --no-save < SampleProgram.R > SampleProgram.opt For example, consider the simple R program below: mydata =...

R statistical package: wrapping GOFrame objects

I m trying to generate GOFrame objects to generate a gene ontology mapping in R for unsupported organisms (see http://www.bioconductor.org/packages/release/bioc/vignettes/GOstats/inst/doc/...

Changing the order of dodged bars in ggplot2 barplot

I have a dataframe df.all and I m plotting it in a bar plot with ggplot2 using the code below. I d like to make it so that the order of the dodged bars is flipped. That is, so that the bars labeled "...

Strange error when using sparse matrices and glmnet

I m getting a weird error when training a glmnet regression. invalid class "dgCMatrix" object: length(Dimnames[[2]]) must match Dim[2] It only happens occasionally, and perhaps only under larger ...

Generating non-duplicate combination pairs in R

Sorry for the non-descriptive title but I don t know whether there s a word for what I m trying to achieve. Let s assume that I have a list of names of different classes like c( 1 , 2 , 3 , 4 ) ...

Per panel smoothing in ggplot2

I m plotting a group of curves, using facet in ggplot2. I d like to have a smoother applied to plots where there are enough points to smooth, but not on plots with very few points. In particular I d ...