如何在R / ggplot2中以分面方式绘制两条分布曲线?

时间:2022-04-04 00:51:19

I have two probability distribution curves, a Gamma and a standarized Normal, that I need to compare:

我有两个概率分布曲线,一个Gamma和一个标准化的Normal,我需要比较:

library(ggplot2)

pgammaX <- function(x) pgamma(x, shape = 64.57849, scale = 0.08854802)

f <- ggplot(data.frame(x=c(-4, 9)), aes(x)) + stat_function(fun=pgammaX)
f + stat_function(fun = pnorm)

The output is like this

输出是这样的

如何在R / ggplot2中以分面方式绘制两条分布曲线?

However I need to have the two curves separated by means of the faceting mechanism provided by ggplot2, sharing the Y axis, in a way like shown below:

但是我需要通过ggplot2提供的刻面机制分离两条曲线,共享Y轴,方式如下所示:

如何在R / ggplot2中以分面方式绘制两条分布曲线?

I know how to do the faceting if the depicted graphics come from data (i.e., from a data.frame), but I don't understand how to do it in a case like this, when the graphics are generated on line by functions. Do you have any idea on this?

如果所描绘的图形来自数据(即来自data.frame),我知道如何进行刻面,但是当图形是按功能在线生成时,我不明白如何在这样的情况下执行此操作。你对此有什么想法吗?

3 个解决方案

#1


1  

you can generate the data similar to what stat_function is doing ahead of time, something like:

你可以生成类似于stat_function提前做的数据,例如:

x <- seq(-4,9,0.1)
dat <- data.frame(p = c(pnorm(x), pgammaX(x)), g = rep(c(0,1), each = 131), x = rep(x, 2) )
ggplot(dat)+geom_line(aes(x,p, group = g)) + facet_grid(~g)

#2


1  

The issue with doing facet_wrap is that the same stat_function is designed to be applied to each panel of the faceted variable which you don't have.

执行facet_wrap的问题是,相同的stat_function旨在应用于您没有的分面变量的每个面板。

I would instead plot them separately and use grid.arrange to combine them.

我会改为分别绘制它们并使用grid.arrange来组合它们。

f1 <- ggplot(data.frame(x=c(-4, 9)), aes(x)) + stat_function(fun = pgammaX) + ggtitle("Gamma") + theme(plot.title = element_text(hjust = 0.5))
f2 <- ggplot(data.frame(x=c(-4, 9)), aes(x)) + stat_function(fun = pnorm) + ggtitle("Norm") + theme(plot.title = element_text(hjust = 0.5))
library(gridExtra)
grid.arrange(f1, f2, ncol=2)

Otherwise create the data frame with y values from both pgammaX and pnorm and categorize them under a faceting variable.

否则,使用pgammaX和pnorm中的y值创建数据框,并在分面变量下对它们进行分类。

#3


1  

Finally I got the answer. First, I need to have two data sets and attach each function to each data set, as follows:

最后我得到了答案。首先,我需要有两个数据集并将每个函数附加到每个数据集,如下所示:

library(ggplot2)

pgammaX <- function(x) pgamma(x, shape = 64.57849, scale = 0.08854802)

a <- data.frame(x=c(3,9), category="Gamma")
b <- data.frame(x=c(-4,4), category="Normal")

f <- ggplot(a, aes(x)) + stat_function(fun=pgammaX) + stat_function(data = b, mapping = aes(x), fun = pnorm)

Then, using facet_wrap(), I separate into two graphics according to the category assigned to each data set, and establishing a free_x scale.

然后,使用facet_wrap(),我根据分配给每个数据集的类别分成两个图形,并建立一个free_x比例。

f + facet_wrap("category", scales = "free_x")

The result is shown below:

结果如下所示:

如何在R / ggplot2中以分面方式绘制两条分布曲线?

#1


1  

you can generate the data similar to what stat_function is doing ahead of time, something like:

你可以生成类似于stat_function提前做的数据,例如:

x <- seq(-4,9,0.1)
dat <- data.frame(p = c(pnorm(x), pgammaX(x)), g = rep(c(0,1), each = 131), x = rep(x, 2) )
ggplot(dat)+geom_line(aes(x,p, group = g)) + facet_grid(~g)

#2


1  

The issue with doing facet_wrap is that the same stat_function is designed to be applied to each panel of the faceted variable which you don't have.

执行facet_wrap的问题是,相同的stat_function旨在应用于您没有的分面变量的每个面板。

I would instead plot them separately and use grid.arrange to combine them.

我会改为分别绘制它们并使用grid.arrange来组合它们。

f1 <- ggplot(data.frame(x=c(-4, 9)), aes(x)) + stat_function(fun = pgammaX) + ggtitle("Gamma") + theme(plot.title = element_text(hjust = 0.5))
f2 <- ggplot(data.frame(x=c(-4, 9)), aes(x)) + stat_function(fun = pnorm) + ggtitle("Norm") + theme(plot.title = element_text(hjust = 0.5))
library(gridExtra)
grid.arrange(f1, f2, ncol=2)

Otherwise create the data frame with y values from both pgammaX and pnorm and categorize them under a faceting variable.

否则,使用pgammaX和pnorm中的y值创建数据框,并在分面变量下对它们进行分类。

#3


1  

Finally I got the answer. First, I need to have two data sets and attach each function to each data set, as follows:

最后我得到了答案。首先,我需要有两个数据集并将每个函数附加到每个数据集,如下所示:

library(ggplot2)

pgammaX <- function(x) pgamma(x, shape = 64.57849, scale = 0.08854802)

a <- data.frame(x=c(3,9), category="Gamma")
b <- data.frame(x=c(-4,4), category="Normal")

f <- ggplot(a, aes(x)) + stat_function(fun=pgammaX) + stat_function(data = b, mapping = aes(x), fun = pnorm)

Then, using facet_wrap(), I separate into two graphics according to the category assigned to each data set, and establishing a free_x scale.

然后,使用facet_wrap(),我根据分配给每个数据集的类别分成两个图形,并建立一个free_x比例。

f + facet_wrap("category", scales = "free_x")

The result is shown below:

结果如下所示:

如何在R / ggplot2中以分面方式绘制两条分布曲线?