Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 206316

Shade area under a curve [duplicate]

$
0
0

I'm trying to shade an area under a curve in R. I can't quite get it right and I'm not sure why. The curve is defined by

# 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)

The plot is

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

The code I'm using to try to color under the curve where x >= 1250 is

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

but here's the result I'm getting enter image description here How can I correctly color the portion under the curve where x >= 1250


Viewing all articles
Browse latest Browse all 206316

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>