限制 ggplot+geom_密度 的 y 轴范围

发布于 2024-09-28 19:46:36 字数 446 浏览 1 评论 0原文

我正在使用 geom_密度 来绘制尾部非常细的密度。我想限制 y 轴范围(因此分布的顶部将离开屏幕,尾部更清晰可见),但它在计算密度时会丢弃屏幕外的数据,而不是仅仅不显示内容已离开屏幕。

例如,

这绘制了完整的分布,

testData = data.frame(counts=c(rep(1,5), 1:10))
ggplot(testData, aes(x=testData$counts))+geom_density()

但是当 y 范围受到限制时,分布看起来好像具有较小的支持。

ggplot(testData, aes(x=testData$counts))+geom_density()+scale_y_continuous(limits=c(0,0.1))

如何在不丢失数据的情况下“放大”y 轴?

I'm using geom_density to plot densities with very thin tails. I want to restrict the y-axis range (so the top of the distribution will be off screen and the tail is more clearly visible) but it's throwing away data that is off-screen when it calculate the density, rather than just not showing what is off screen.

E.g.

This plots the full distribution,

testData = data.frame(counts=c(rep(1,5), 1:10))
ggplot(testData, aes(x=testData$counts))+geom_density()

but when the y range is restricted, it looks as though the distribution has smaller support.

ggplot(testData, aes(x=testData$counts))+geom_density()+scale_y_continuous(limits=c(0,0.1))

How can I "zoom in" on the y axis without throwing away data?

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

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

发布评论

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

评论(1

缱绻入梦 2024-10-05 19:46:36

我相信您正在寻找 coord_cartesian():

ggplot(testData, aes(x=testData$counts))+geom_density()+coord_cartesian(ylim=c(0, 0.1))

I believe you're looking for coord_cartesian():

ggplot(testData, aes(x=testData$counts))+geom_density()+coord_cartesian(ylim=c(0, 0.1))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文