ggplot2 - 绘制逻辑分布,其中一小部分区域有颜色

发布于 2024-12-06 06:24:54 字数 782 浏览 0 评论 0原文

我有以下代码来绘制我的物流分布:

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)

现在我想做的是突出显示 x = 0 左侧的区域。
我尝试这样做:

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)

但这不起作用。不确定我做错了什么。有什么帮助吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

装迷糊 2024-12-13 06:24:54

我还没有诊断出您的多边形的问题(尽管我认为您需要提供外部的完整路径,即将 rep(0,length(x)) 附加到 的末尾>yrev(x)x 的末尾),但是 geom_ribbon (如 对两点之间的核密度图进行着色。)似乎可以解决这个问题:

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)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文