geom_polygon 绘制正态分布和逻辑分布

发布于 2024-12-05 11:52:39 字数 1307 浏览 0 评论 0原文

更新:
我已经解决了我的问题。我一直在寻找

coord_cartesian(xlim = c(800, 2100), ylim = c(0, 0.0021))

感谢每一位试图提供帮助的人!

问题是:
我想画出一幅关于正态分布和逻辑分布之间差异的漂亮图画。我已经达到了这一点:

 x=seq(1000,2000,length=200)  
 dat <- data.frame(
   norm = dnorm(x,mean=1500,sd=200), 
   logistic = dlogis(x,location=1500,scale=200), x = x
 )
 ggplot(data=dat, aes(x=x)) + 
   geom_polygon(aes(y=norm), fill="red", alpha=0.6) + 
   geom_polygon(aes(y=logistic), fill="blue", alpha=0.6) + 
   xlab("") + ylab("") +   
   opts(title="Logistic and Normal Distributions") + 
   scale_x_continuous(expand = c(0, 0)) + 
   scale_y_continuous(expand = c(0, 0))

但是后勤部分在底部被“切断”。我认为我应该做的是绘制从 0 到 3000 的分布,但只显示 1000-2000。

有任何线索如何做到这一点?

我尝试了scale_x_continuous(limits = c(1000, 2000))但这不起作用

更新:

我已经更新了我的代码所以我有图例,现在看起来像这样:

  x=seq(700,2300,length=200)  
  dat2 <- data.frame(x=x)
  dat2$value <- dnorm(x,mean=1500,sd=200)
  dat2$type <- "Normal"

 dat1 <- data.frame(x=x)
 dat1$value <- dlogis(x,location=1500,scale=200)       
 dat1$type <- "Logistic"

 dat <- rbind(dat1, dat2)

 ggplot(data=dat, aes(x=x, y=value, colour=type, fill=type)) + geom_polygon(alpha=0.6) + scale_y_continuous(expand = c(0, 0))

UPDATE:
I have solved my problem. I was looking for

coord_cartesian(xlim = c(800, 2100), ylim = c(0, 0.0021))

Thanks to every one who tried to help!

QUESTION WAS:
I would like to draw a nice picture of what is the difference between normal and logistic distributions. I have reached that point :

 x=seq(1000,2000,length=200)  
 dat <- data.frame(
   norm = dnorm(x,mean=1500,sd=200), 
   logistic = dlogis(x,location=1500,scale=200), x = x
 )
 ggplot(data=dat, aes(x=x)) + 
   geom_polygon(aes(y=norm), fill="red", alpha=0.6) + 
   geom_polygon(aes(y=logistic), fill="blue", alpha=0.6) + 
   xlab("") + ylab("") +   
   opts(title="Logistic and Normal Distributions") + 
   scale_x_continuous(expand = c(0, 0)) + 
   scale_y_continuous(expand = c(0, 0))

However the logistic one is "cut" at the bottom. I think what I should do is to draw this distribution from 0 to 3000 for example but show only 1000-2000.

Any clues how to do this?

I tried scale_x_continuous(limits = c(1000, 2000)) but this does not work

UPDATE:

I have updated my code so I have legend, now it looks like this:

  x=seq(700,2300,length=200)  
  dat2 <- data.frame(x=x)
  dat2$value <- dnorm(x,mean=1500,sd=200)
  dat2$type <- "Normal"

 dat1 <- data.frame(x=x)
 dat1$value <- dlogis(x,location=1500,scale=200)       
 dat1$type <- "Logistic"

 dat <- rbind(dat1, dat2)

 ggplot(data=dat, aes(x=x, y=value, colour=type, fill=type)) + geom_polygon(alpha=0.6) + scale_y_continuous(expand = c(0, 0))

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

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

发布评论

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

评论(4

一抹苦笑 2024-12-12 11:52:39

我会使用 z 分数来绘制它,从 [-2 ; +2]。这样做的好处是你的问题会消失。

x=seq(-2,2,length=200)  
dat <- data.frame(
  norm = dnorm(x,mean=0,sd=0.2), 
  logistic = dlogis(x,location=0,scale=0.2), x = x
)
p <- ggplot(data=dat, aes(x=x)) + 
  geom_polygon(aes(y=norm), fill="red", alpha=0.6) + 
  geom_polygon(aes(y=logistic), fill="blue", alpha=0.6) + 
  xlab("z") + ylab("") + 
  scale_x_continuous(expand = c(0, 0)) + 
  scale_y_continuous(expand = c(0, 0)) +  
  opts(title="Logistic and Normal Distributions") 

print(p)

在此处输入图像描述

I would draw it using z-scores, from [-2 ; +2]. This has the side benefit that your problem goes away.

x=seq(-2,2,length=200)  
dat <- data.frame(
  norm = dnorm(x,mean=0,sd=0.2), 
  logistic = dlogis(x,location=0,scale=0.2), x = x
)
p <- ggplot(data=dat, aes(x=x)) + 
  geom_polygon(aes(y=norm), fill="red", alpha=0.6) + 
  geom_polygon(aes(y=logistic), fill="blue", alpha=0.6) + 
  xlab("z") + ylab("") + 
  scale_x_continuous(expand = c(0, 0)) + 
  scale_y_continuous(expand = c(0, 0)) +  
  opts(title="Logistic and Normal Distributions") 

print(p)

enter image description here

梦一生花开无言 2024-12-12 11:52:39

它切断底部的原因是因为 geom_polygon 从字面上绘制了由连接您给它的点的线组成的多边形。因此,分布底部的平线只是连接数据框中的第一个值和最后一个值。如果您希望它延伸到底部,您可以将适当的点添加到数据框中:

ggplot(data=dat, aes(x=x)) + 
    geom_polygon(aes(y=norm), fill="red", alpha=0.6) + 
    geom_polygon(data = rbind(c(NA,0,1000),dat,c(NA,0,2000)),aes(y=logistic), fill="blue", alpha=0.6) + xlab("") + ylab("") +   
    opts(title="Logistic and Normal Distributions")+ 
    scale_x_continuous(expand = c(0, 0)) + 
    scale_y_continuous(expand = c(0, 0))

在此处输入图像描述

为清楚起见进行了编辑

您可以对此进行修改,通过添加具有正确值的点来使其仅下降到您想要的程度。例如,我强制物流分配一直填充到零。您可以通过rbind最小正态密度值来使其与正态分布持平。另外,请注意将它们添加到数据框中的位置geom_polygon 将按点出现的顺序连接点。。这就是为什么我在数据框的开头添加了一个,在末尾添加了一个。

编辑2

根据您修改后的代码,我的解决方案仍然可以正常工作:

x=seq(700,2300,length=200)  
dat2 <- data.frame(x=x)
dat2$value <- dnorm(x,mean=1500,sd=200)
dat2$type <- "Normal"

dat1 <- data.frame(x=x)
dat1$value <- dlogis(x,location=1500,scale=200)       
dat1$type <- "Logistic"

#Append extra points at the top/bottom to 
# complete the polygon
dat1 <- rbind(data.frame(x=700,value=0,type = "Logistic"),dat1,
                data.frame(x=2300,value=0,type = "Logistic"))

dat <- rbind(dat1, dat2)

ggplot(data=dat, aes(x=x, y=value, colour=type, fill=type)) + 
       geom_polygon(alpha=0.6) + 
       scale_y_continuous(expand = c(0, 0))

在此处输入图像描述

并且就我个人而言,我更喜欢这个而不是 coord_cartesian,因为我坚持从零开始我的轴。

The reason it cuts off the bottom is because geom_polygon literally draws the polygon consisting of lines connecting the points you give it. So the flat line across the bottom of the distribution is just connecting the first and last value in your data frame. If you want it to extend to the bottom you can add the appropriate points to your data frame:

ggplot(data=dat, aes(x=x)) + 
    geom_polygon(aes(y=norm), fill="red", alpha=0.6) + 
    geom_polygon(data = rbind(c(NA,0,1000),dat,c(NA,0,2000)),aes(y=logistic), fill="blue", alpha=0.6) + xlab("") + ylab("") +   
    opts(title="Logistic and Normal Distributions")+ 
    scale_x_continuous(expand = c(0, 0)) + 
    scale_y_continuous(expand = c(0, 0))

enter image description here

Edited for clarity

You can tinker with this to get it to go down only as far as you want by adding points with the right values. For instance, I forced the logistic distribution to fill all the way down to zero. You could make it level with the normal distribution by rbinding the minimum normal density value instead. Also, be careful where you add them in your data frame. geom_polygon will connect the dots in the order they appear. That's why I added one at the beginning of the data frame and one at the end.

Edit 2

Based on your revised code, my solution still works fine:

x=seq(700,2300,length=200)  
dat2 <- data.frame(x=x)
dat2$value <- dnorm(x,mean=1500,sd=200)
dat2$type <- "Normal"

dat1 <- data.frame(x=x)
dat1$value <- dlogis(x,location=1500,scale=200)       
dat1$type <- "Logistic"

#Append extra points at the top/bottom to 
# complete the polygon
dat1 <- rbind(data.frame(x=700,value=0,type = "Logistic"),dat1,
                data.frame(x=2300,value=0,type = "Logistic"))

dat <- rbind(dat1, dat2)

ggplot(data=dat, aes(x=x, y=value, colour=type, fill=type)) + 
       geom_polygon(alpha=0.6) + 
       scale_y_continuous(expand = c(0, 0))

enter image description here

And personally, I would prefer this over coord_cartesian, since I'm a stickler about starting my axes from zero.

2024-12-12 11:52:39

解决方案是使用

+ coord_cartesian(xlim = c(800, 2100), ylim = c(0, 0.0021))

The solution is to use

+ coord_cartesian(xlim = c(800, 2100), ylim = c(0, 0.0021))
茶底世界 2024-12-12 11:52:39

我运行了你的代码,然后分析了norm和logistic的值:

Rgames: mystat(dat$logistic)

  min      max     mean   median

3.51e-04 1.25e-03 8.46e-04 8.63e-04

 sdev      skew kurtosis

2.96e-04 -1.33e-01 -1.4

Rgames: mystat(dat$norm)

  min      max     mean   median

8.76e-05 1.99e-03 9.83e-04 9.06e-04

 sdev     skew kurtosis

6.62e-04 1.67e-01 -1.48

因此,您的逻辑值实际上已正确绘制。正如其他答案所示,有更好的方法来创建基础数据。

I ran your code, and then analyzed the values of norm and logistic:

Rgames: mystat(dat$logistic)

  min      max     mean   median

3.51e-04 1.25e-03 8.46e-04 8.63e-04

 sdev      skew kurtosis

2.96e-04 -1.33e-01 -1.4

Rgames: mystat(dat$norm)

  min      max     mean   median

8.76e-05 1.99e-03 9.83e-04 9.06e-04

 sdev     skew kurtosis

6.62e-04 1.67e-01 -1.48

So your logistic values are in fact correctly plotted. As the other answers showed, there are preferable ways to create your underlying data.

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