/ / Wie man einen Graphen mit Hilfe der Kurve () in R - r, Plot, Graph, Fläche schattiert

Wie man einen Graph mit der Kurve () in R - r, Plot, Graph, Bereich schattiert

Ich zeichne die Standardnormalverteilung auf.

curve(dnorm(x), from=-4, to=4,
main = "The Standard Normal Distibution",
ylab = "Probability Density",
xlab = "X")

Aus pädagogischen Gründen möchte ich den Bereich unterhalb eines bestimmten Quantils meiner Wahl schattieren. Wie kann ich das machen?

Antworten:

4 für die Antwort № 1

Wenn Sie verwenden möchten curve und Basisplot, dann können Sie selbst eine kleine Funktion schreiben polygon:

colorArea <- function(from, to, density, ..., col="blue", dens=NULL){
y_seq <- seq(from, to, length.out=500)
d <- c(0, density(y_seq, ...), 0)
polygon(c(from, y_seq, to), d, col=col, density=dens)
}

Ein kleines Beispiel folgt:

curve(dnorm(x), from=-4, to=4,
main = "The Standard Normal Distibution",
ylab = "Probability Density",
xlab = "X")

colorArea(from=-4, to=qnorm(0.025), dnorm)
colorArea(from=qnorm(0.975), to=4, dnorm, mean=0, sd=1, col=2, dens=20)

Bildbeschreibung hier eingeben