如何在R中使用ggMarginal将箱线图添加到直方图

如何在R中使用ggMarginal将箱线图添加到直方图

本文介绍了如何在R中使用ggMarginal将箱线图添加到直方图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想绘制一条带有密度曲线的直方图,然后在顶部边缘上方放置一个箱形图。我知道如何使用 hist() boxplot() layout( )函数,或使用 ggplot2 grid 包中的函数。但是,我正在寻找在 ggplot2 和 ggMarginal()函数的特定解决方案> ggExtra 软件包。在提出问题之前,让我们模拟一些数据:

I would like to draw a histogram with a density curve and then put a boxplot above the top margin. I know how to do this using the hist(), boxplot() and layout() functions, or using functions from the ggplot2 and grid packages. However, I am looking for a specific solution using ggplot2 and the ggMarginal() function within the ggExtra package. Let's simulate some data before I present my problem:

library(ggplot2)
library(ggExtra)
set.seed(1234)
vdat = data.frame(V1 = c(sample(1:10, 100, T), 99))
vname = colnames(vdat)[1]
boxplot(vdat[[vname]], horizontal = T)

注意,我明确插入离群值99到1到10的数字样本中。因此,当我绘制箱线图时,应将99显示为离群值。

To note, I explicitly insert an outlier 99 into a sample of numbers from 1 to 10. Hence, when I draw the boxplot, 99 should be displayed as an outlier.

我可以使用 ggplot2 轻松绘制直方图。

I can easily draw a histogram using ggplot2.

p = ggplot(data=vdat, aes_string(x=vname)) +
  geom_histogram(aes(y=stat(density)),
                         bins=nclass.Sturges(vdat[[vname]])+1,
                         color="black", fill="steelblue", na.rm=T) +
  geom_density(na.rm=T) +
  theme_bw()
p

当我尝试使用 ggMarginal 添加一个边缘盒图,添加的盒图不正确。

When I try to use ggMarginal to add a marginal boxplot, the added boxplots are not right.

p1 = ggMarginal(p, type="boxplot")
p1

右边的方框图可能是正确的。但是最上面的那个,我需要的那个,绝对是错误的。离群值99不存在,中位数显然不正确。

The boxplot on the right might be right. But the one on top, which is the very one I need, is definitely wrong. The outlier 99 is not there and the median is clearly not right.

当我尝试不提供 p1 时,却提供了原始数据 x 和帮助文档建议的 y ,我得到了正确的箱线图,但直方图现在不见了。

When I try not to provide p1, but the original data, x, and y as suggested by the help documentation, I get the right boxplot but the histogram is now gone.

p2 = ggMarginal(data=vdat, x=vname, y=NA, type="boxplot", margins="x")
p2

我如何结合p1和p2的正确部分,使我有p1的直方图和p2的箱线图?

How can I combine the correct parts of p1 and p2 such that I have the histogram from p1 and the boxplot from p2?

我正在尝试类似

p1 + p2

ggMarginal(p1, data=vdat, x=vname, y=NA, type="boxplot", margins="x")

但是它们不起作用。

推荐答案

根据 ggMarginal 的文档, p 应该是ggplot 散点图。我们可以在 p 中插入以下行作为第一个geom层:

According to ggMarginal's documentation, p is expected to be a ggplot scatterplot. We can insert the following line as the first geom layer in p:

geom_point(aes(y = 0.01), alpha = 0)

y = 0.01 作为现有图的y轴范围内的值,并且 alpha = 0 确保该图层不可见。

y = 0.01 was chosen as a value within the existing plot's y-axis range, and alpha = 0 ensures this layer isn't visible.

使用此 p 运行代码应该会为您提供带有异常值的箱线图。

Running your code with this p should give you the boxplot with outlier.

p <- ggplot(data=vdat, aes_string(x=vname)) +
  geom_point(aes(y = 0.01), alpha = 0) +
  geom_histogram(aes(y=stat(density)),
                 bins=nclass.Sturges(vdat[[vname]])+1,
                 color="black", fill="steelblue", na.rm=T) +
  geom_density(na.rm=T) +
  theme_bw()

p1 = ggMarginal(p, type="boxplot", margins = "x")
p1

顺便说一句,在这种情况下,我认为在右侧绘制箱形图确实没有任何意义,因为您尚未为 y

By the way, I don't think it really makes sense to plot a boxplot to the right in this instance, since you have not assigned any variable to y.

这篇关于如何在R中使用ggMarginal将箱线图添加到直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 20:45