如何使用ggplot2和lattice在直方图上叠加分布曲线

发布于 2024-09-13 17:47:20 字数 438 浏览 4 评论 0原文

比如说,我在 ggplot2 中使用 Facet_grid() 来获取 2 个直方图。现在我想将这些直方图与泊松曲线(两个直方图/网格具有不同的平均值)和另一个分布的第二条曲线(我想手动提供值的概率函数)叠加。这怎么能做到呢?

构建一个示例:

library(ggplot2)

value<-c(rpois(500,1.5))

group<-rep(c("A","B"),250)

data<-data.frame(value,group)

g1<-ggplot(data,aes(value))

g1+geom_histogram(aes(y=..count..),binwidth=1,position="identity")+facet_grid(.~group)

接下来做什么?

或者,可以使用lattice包来完成吗?

Say, I am using facet_grid() in ggplot2 to obtain 2 histograms. Now I want to superimpose these histograms with Poisson curves (having different means for the 2 histogram plots/grids) and a second curve of another distribution (for which I want to manually provide the probability function of values). How can this be done?

Constructing an example:

library(ggplot2)

value<-c(rpois(500,1.5))

group<-rep(c("A","B"),250)

data<-data.frame(value,group)

g1<-ggplot(data,aes(value))

g1+geom_histogram(aes(y=..count..),binwidth=1,position="identity")+facet_grid(.~group)

What next?

Alternatively, can it be done using the lattice package?

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

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

发布评论

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

评论(2

彼岸花似海 2024-09-20 17:47:20

最简单的方法是绘制密度而不是计数并使用 stat_function()

library(ggplot2)
value<-c(rpois(500,1.5))
group<-rep(c("A","B"),250)
data<-data.frame(value,group)
ggplot(data,aes(value)) + 
        geom_histogram(aes(y=..density..), binwidth=1,position="identity") + 
        facet_grid(.~group) + 
        stat_function(geom = "line", fun = dpois, arg = list(lambda = 1.5), colour = "red", fill = NA, n = 9)

如果您想要计数,那么您需要将 dpois 的密度转换为“计数”

ggplot(data,aes(value)) + 
        geom_histogram(aes(y=..count..), binwidth=1,position="identity") + 
        facet_grid(.~group) + 
        stat_function(geom = "line", fun = function(..., total){dpois(...) * total}, arg = list(lambda = 1.5, total = 250), colour = "red", fill = NA, n = 9)

The easy way is to plot densities instead of counts and use stat_function()

library(ggplot2)
value<-c(rpois(500,1.5))
group<-rep(c("A","B"),250)
data<-data.frame(value,group)
ggplot(data,aes(value)) + 
        geom_histogram(aes(y=..density..), binwidth=1,position="identity") + 
        facet_grid(.~group) + 
        stat_function(geom = "line", fun = dpois, arg = list(lambda = 1.5), colour = "red", fill = NA, n = 9)

If you want counts then you need to convert the densities of dpois to 'counts'

ggplot(data,aes(value)) + 
        geom_histogram(aes(y=..count..), binwidth=1,position="identity") + 
        facet_grid(.~group) + 
        stat_function(geom = "line", fun = function(..., total){dpois(...) * total}, arg = list(lambda = 1.5, total = 250), colour = "red", fill = NA, n = 9)
情绪 2024-09-20 17:47:20

最近遇到类似的问题(比较分布)时,我为 透明重叠直方图可能会给您一些从哪里开始的想法。

When recently faced with a similar problem (comparing distributions), I wrote up some code for transparent overlapping histograms that might give you some ideas on where to start.

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