在同一页中绘制多个直方图,但是一个直方图的坐标是相反的

时间:2021-07-03 14:58:38

I would like to plot two or more histograms from the same data, but inverting the coordinates of one group, like the following:

我想从相同的数据中画出两个或更多的直方图,但是将一个组的坐标反演,如下所示:

在同一页中绘制多个直方图,但是一个直方图的坐标是相反的

(note: this figure is taken from Grossman et al 2011, A composite of Multiple Signals Distinguishes Causal Variants in Regions of Positive Selection)

(注:这一数字取自Grossman等人2011年的数据,多重信号的组合可以区分正选择区域的因果变异)

For example, let's take the diamonds dataset from ggplot2:

例如,让我们以ggplot2中的钻石数据集为例:

> library(ggplot2)
> head(diamonds)
  carat       cut color clarity depth table price    x    y    z
1  0.23     Ideal     E     SI2  61.5    55   326 3.95 3.98 2.43
2  0.21   Premium     E     SI1  59.8    61   326 3.89 3.84 2.31
3  0.23      Good     E     VS1  56.9    65   327 4.05 4.07 2.31
4  0.29   Premium     I     VS2  62.4    58   334 4.20 4.23 2.63
5  0.31      Good     J     SI2  63.3    58   335 4.34 4.35 2.75
6  0.24 Very Good     J    VVS2  62.8    57   336 3.94 3.96 2.48

One approach I have tried has been to calculate the histogram using stat_bin without plotting it, changing the values of the counts column returned by it.

我尝试过的一种方法是使用stat_bin计算直方图,而不绘制它,更改它返回的counts列的值。

> my_sb <-stat_bin(data=diamonds, mapping=aes(x=x)

The documentation of stat_bin says that this function should return a data.frame equal to the mapped one, but adding four new columns (count, density, ncount, ndensity). However, I can not find these columns anywhere:

stat_bin的文档说,这个函数应该返回一个与映射的数据相同的frame,但是要添加四个新的列(count、density、ncount、ndensity)。然而,我在任何地方都找不到这些专栏:

# I supposed that this should contain a count, density columns, but it does not.
> print(head(my_sb$data))  
  carat       cut color clarity depth table price    x    y    z
1  0.23     Ideal     E     SI2  61.5    55   326 3.95 3.98 2.43
2  0.21   Premium     E     SI1  59.8    61   326 3.89 3.84 2.31
3  0.23      Good     E     VS1  56.9    65   327 4.05 4.07 2.31
4  0.29   Premium     I     VS2  62.4    58   334 4.20 4.23 2.63
5  0.31      Good     J     SI2  63.3    58   335 4.34 4.35 2.75
6  0.24 Very Good     J    VVS2  62.8    57   336 3.94 3.96 2.48

Another possible approach is to use scale_y_reverse(), but I don't know how to apply it to a single dataset.

另一种可能的方法是使用scale_y_reverse(),但是我不知道如何将它应用到单个数据集。

The third approach that I can think of is to use viewPorts, but I am not quite sure on how to implement it.

我能想到的第三种方法是使用viewPorts,但我不太确定如何实现它。

1 个解决方案

#1


4  

Like this maybe:

这样的可能:

ggplot(data = diamonds) + 
    geom_histogram(aes(x = x,y = ..count..)) + 
    geom_histogram(aes(x = x,y = -..count..))

FYI - I couldn't remember exactly how I'd done this in the past, so I Googled "ggplot2 inverted histogram" and clicked on the first hit, a * question.

我不记得我以前是怎么做的,所以我在谷歌上搜索了“ggplot2反向直方图”,点击了第一个点击,一个*的问题。

I'm not sure exactly how the proto object that stat_bin returns is structured, but the new variables are in there somewhere. The way this works is that geom_histogram itself calls stat_bin to perform the binning, and so it has access to the computed variables, which we can map to the y variable.

我不确定stat_bin返回的原始对象是如何构造的,但是新的变量在那里。它的工作方式是,geom_histogram本身调用stat_bin来执行绑定,因此它可以访问计算的变量,我们可以将这些变量映射到y变量。

#1


4  

Like this maybe:

这样的可能:

ggplot(data = diamonds) + 
    geom_histogram(aes(x = x,y = ..count..)) + 
    geom_histogram(aes(x = x,y = -..count..))

FYI - I couldn't remember exactly how I'd done this in the past, so I Googled "ggplot2 inverted histogram" and clicked on the first hit, a * question.

我不记得我以前是怎么做的,所以我在谷歌上搜索了“ggplot2反向直方图”,点击了第一个点击,一个*的问题。

I'm not sure exactly how the proto object that stat_bin returns is structured, but the new variables are in there somewhere. The way this works is that geom_histogram itself calls stat_bin to perform the binning, and so it has access to the computed variables, which we can map to the y variable.

我不确定stat_bin返回的原始对象是如何构造的,但是新的变量在那里。它的工作方式是,geom_histogram本身调用stat_bin来执行绑定,因此它可以访问计算的变量,我们可以将这些变量映射到y变量。