2016-04-29 8 views
1

Ich versuche, einen Bereich unter einer Kurve in R zu schattieren. Ich kann es nicht recht bekommen und ich bin mir nicht sicher warum. Die Kurve ist definiert durchSchattenbereich unter einer Kurve

# Define the Mean and Stdev 
mean=1152 
sd=84 

# Create x and y to be plotted 
# x is a sequence of numbers shifted to the mean with the width of sd. 
# The sequence x includes enough values to show +/-3.5 standard deviations in the data set. 
# y is a normal distribution for x 
x <- seq(-3.5,3.5,length=100)*sd + mean 
y <- dnorm(x,mean,sd) 

Das Grundstück ist

# Plot x vs. y as a line graph 
plot(x, y, type="l") 

Der Code, den ich an Farbe unter der Kurve, um zu versuchen bin mit wobei x> = 1250 ist

polygon(c(x[x>=1250], max(x)), c(y[x==max(x)], y[x>=1250]), col="red") 

aber hier ist das Ergebnis, das ich bekomme enter image description here Wie kann ich den Teil unter der Kurve richtig färben, wo x> = 1250

Antwort

6

Sie müssen den x, y Punkten der Kurve mit dem Polygon folgen und dann zum Punkt bei x = 1250, y = 0 zurückkehren, um die Form zu vervollständigen. Die endgültige vertikale Kante wird automatisch gezeichnet, weil das Polygon die Form schließt, indem es zum Anfangspunkt zurückkehrt.

polygon(c(x[x>=1250], 1250), c(y[x>=1250],0), col="red") 
Verwandte Themen