开发者

ggplot2 - draw logistic distribution with small part of the area colored

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?


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜