geom_polygon 绘制正态分布和逻辑分布
更新:
我已经解决了我的问题。我一直在寻找
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我会使用 z 分数来绘制它,从 [-2 ; +2]。这样做的好处是你的问题会消失。
I would draw it using z-scores, from [-2 ; +2]. This has the side benefit that your problem goes away.
它切断底部的原因是因为
geom_polygon
从字面上绘制了由连接您给它的点的线组成的多边形。因此,分布底部的平线只是连接数据框中的第一个值和最后一个值。如果您希望它延伸到底部,您可以将适当的点添加到数据框中:为清楚起见进行了编辑
您可以对此进行修改,通过添加具有正确值的点来使其仅下降到您想要的程度。例如,我强制物流分配一直填充到零。您可以通过
rbind
最小正态密度值来使其与正态分布持平。另外,请注意将它们添加到数据框中的位置。geom_polygon
将按点出现的顺序连接点。。这就是为什么我在数据框的开头添加了一个,在末尾添加了一个。编辑2
根据您修改后的代码,我的解决方案仍然可以正常工作:
并且就我个人而言,我更喜欢这个而不是 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: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
rbind
ing 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:
And personally, I would prefer this over
coord_cartesian
, since I'm a stickler about starting my axes from zero.解决方案是使用
The solution is to use
我运行了你的代码,然后分析了norm和logistic的值:
Rgames: mystat(dat$logistic)
3.51e-04 1.25e-03 8.46e-04 8.63e-04
2.96e-04 -1.33e-01 -1.4
Rgames: mystat(dat$norm)
8.76e-05 1.99e-03 9.83e-04 9.06e-04
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)
3.51e-04 1.25e-03 8.46e-04 8.63e-04
2.96e-04 -1.33e-01 -1.4
Rgames: mystat(dat$norm)
8.76e-05 1.99e-03 9.83e-04 9.06e-04
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.