1

I have following code to draw my logistic distribution:

x=seq(-2000,2000,length=1000) 
dat <- data.frame(x=x)
dat$value <- dlogis(x,location=200,scale=400/log(10))
dat$type <- "Expected score"       
p <- ggplot(data=dat, aes(x=x, y=value)) + geom_line(col="blue", size=1) + 
coord_cartesian(xlim = c(-500, 900), ylim = c(0, 0.0016)) + 
scale_x_continuous(breaks=c(seq(-500, 800, 100)))
pp <- p + geom_line(aes(x = c(0,0), y = c(0,0.0011)), size=0.9, colour="green", linetype=2, alpha=0.7)

Now what I would like to do is to highlight the area to the left of x = 0.
I tried to do it like this:

x = seq(-500, 0, length=10)
y = dlogis(x,location=200,scale=400/log(10))
pol <- data.frame(x = x, y = y)
pp + geom_polygon(aes(data=pol,x=x, y=y), fill="light blue", alpha=0.6)

But this does not work. Not sure what I am doing wrong. Any help?

joran
  • 169,992
  • 32
  • 429
  • 468
mkk
  • 7,583
  • 7
  • 46
  • 62
  • Try searching SO with '[r] shade area'. I'm certain this has come up before. – joran Sep 23 '11 at 22:31
  • 1
    Does anything in http://stackoverflow.com/questions/3494593/shading-a-kernel-density-plot-between-two-points/4371473#4371473 help? – Ben Bolker Sep 23 '11 at 22:32
  • Cool! I tried to search for this but couldn't find it! I believe this fixes my issue! – mkk Sep 23 '11 at 22:35

1 Answers1

3

I haven't diagnosed the problem with your polygon (although I think you would need to give the full path around the outside, i.e. attach rep(0,length(x)) to the end of y and rev(x) to the end of x), but geom_ribbon (as in Shading a kernel density plot between two points. ) seems to do the trick:

pp + geom_ribbon(data=data.frame(x=x,y=y),aes(ymax=y,x=x,y=NULL),
   ymin=0,fill="light blue",alpha=0.5)
Community
  • 1
  • 1
Ben Bolker
  • 211,554
  • 25
  • 370
  • 453