2

I am trying to plot two distributions on a graph, and I am using the polygon function to shade

x <- seq(-0.1,0.1,length=1000) 
plot(x = x,                   
 y = dnorm(x, 2, 1),              
 type = "l",     
 col = "white",
 axes = FALSE,
 mgp = c(2, 2, 2), 
 ylim=c(0,30),                         
 frame.plot=TRUE,
 xlab = "  ",                         
 ylab = "  ",
 font.main = 1,
 main=paste("  "),
 lwd=2,)    

polygon(x,dnorm(x, 0, 0.019),col=6,border = NULL)
polygon(x,dnorm(x, 0.08, 0.02),col=rgb(0.8,0.8,0.8,0.5),border = T)

Unfortunately the second distribution isn't plotting correctly, as can be seen in the attached picture. I'm aware that this is because the polygon function joins the first and last points of the distribution, but I'm not sure how to specify that I would like the most far-right part of the distribution to return to zero on the y-axis before joining those two points.

Thanks in advance.

Second distribution not plotting correctly

Fuzzybear
  • 1,388
  • 2
  • 25
  • 42

0 Answers0