叠加两个ggplot facet_wrap直方图 [英] Overlaying two ggplot facet_wrap histograms
问题描述
所以我有两个直方图,一次只能做一个.使用以下代码的结果给出了针对六种不同直方图的2行x 3列刻面图:
So I have two histogram plots I can do one at a time. The result using the following code gives a 2 row x 3 col facet plot for six different histograms:
ggplot(data) +
aes(x=values) +
geom_histogram(binwidth=2, fill='blue', alpha=0.3, color="black", aes(y=(..count..)*100/(sum(..count..)/6))) +
facet_wrap(~ model_f, ncol = 3)
aes(y...)
只是给出百分比而不是计数.
Here the aes(y...)
just gives the percentage instead of counts.
如上所述,这6个facet_wrap图中有两个,现在我将其合并以显示一个比另一个偏移更大. 另外,数据大小是不一样的,所以我有一个:
As stated, I have two of this 6 facet_wrap plot, which I now which to combine to show that one is more shifted than the other. In addition, the data size is not the same, so for one I have:
# A tibble: 5,988 x 5
values ID structure model model_f
<dbl> <chr> <chr> <chr> <fctr>
1 6 1 bone qua Model I
2 7 1 bone liu Model II
3 20 1 bone dav Model III
4 3 1 bone ema Model IV
5 3 1 bone tho Model V
6 4 1 bone ranc Model VI
7 3 2 bone qua Model I
8 5 2 bone liu Model II
9 18 2 bone dav Model III
10 2 2 bone ema Model IV
# ... with 5,978 more rows
另一个:
# A tibble: 954 x 5
values ID structure model model_f
<dbl> <chr> <chr> <chr> <fctr>
1 9 01 bone qua Model I
2 8 01 bone liu Model II
3 22 01 bone dav Model III
4 6 01 bone ema Model IV
5 5 01 bone tho Model V
6 9 01 bone ran Model VI
7 12 02 bone qua Model I
8 11 02 bone liu Model II
9 24 02 bone dav Model III
10 9 02 bone ema Model IV
# ... with 944 more rows
因此它们的大小不相同,ID也不相同(数据不相关),但是我还是希望合并直方图,以查看数据之间的差异.
So they are not the same size, the ID's are not the same (data not related), but still, I wish to merge the histograms in order to see the difference between the data.
我认为这可以解决问题:
I thought this might do the trick:
ggplot() +
geom_histogram(data=data1, aes(x=values), binwidth=1, fill='blue', alpha=0.3, color="black", aes(y=(..count..)*100/(sum(..count..)/6))) +
geom_histogram(data=data2, aes(x=values), binwidth=1, fill='blue', alpha=0.3, color="black", aes(y=(..count..)*100/(sum(..count..)/6))) +
facet_wrap(~ model_f, ncol = 3)
但是,那没什么大作用.
However, that didn't do much.
所以现在我被卡住了.这可能吗,还是...?
So now I'm stuck. Is this possible to do, or...?
推荐答案
基于内置数据集iris
,这是我的难题(因为您未提供可重复的数据).为了创建更小的移位数据集,我使用dplyr
保留每个物种的前20行,并为每个观测值的Sepal长度加1:
Here is my crack at this, based on the builtin dataset iris
(since you did not provide reproducible data). To create the smaller, shifted dataset, I am using dplyr
to keep the first 20 rows from each species and add 1 to the Sepal length for each observation:
smallIris <-
iris %>%
group_by(Species) %>%
slice(1:20) %>%
ungroup() %>%
mutate(Sepal.Length = Sepal.Length + 1)
您的代码最后使您接近,但您没有为两个直方图指定不同的颜色.如果为每个fill
设置不同的内容,则会使它们以不同的方式显示.您可以直接设置此名称(例如,在其中之一中将蓝色"更改为红色"),也可以在aes
中设置名称.在aes
中设置它的优点是可以创建(并标记)图例:
Your code at the end gets you close, but you did not specify different colors for the two histograms. If you set the fill
differently for each, you will get them to show up differently. You could either set this directly (e.g., change "blue" to "red" in one of them) or by setting a name within aes
. Setting it in aes
has the advantage of creating (and labeling) a legend:
ggplot() +
geom_histogram(data=iris
, aes(x=Sepal.Length
, fill = "Big"
, y=(..count..)*100/(sum(..count..)))
, alpha=0.3) +
geom_histogram(data=smallIris
, aes(x=Sepal.Length
, fill = "Small"
, y=(..count..)*100/(sum(..count..)))
, alpha=0.3) +
facet_wrap(~Species)
创建此:
但是,我倾向于不喜欢重叠直方图的外观,因此我更喜欢使用密度图.您可以像上面一样进行操作(只需更改geom_histogram
),但是我认为通过堆叠数据,您可以获得更多的控制权(并且可以将其扩展到两个以上的组).同样,这使用dplyr
将两个数据集缝合在一起:
However, I tend to dislike the look of overlapping histograms, so I would prefer to use a density plot. You can do it just like the above (just change the geom_histogram
), but I think you get a bit more control (and the ability to expand this to more than two groups) by stacking the data. Again, this uses dplyr
to stitch the two datasets together:
bigIris <-
bind_rows(
small = smallIris
, big = iris
, .id = "Source"
)
然后,您可以相对轻松地创建图:
Then, you can create the plot relatively easily:
bigIris %>%
ggplot(aes(x = Sepal.Length, col = Source)) +
geom_line(stat = "density") +
facet_wrap(~Species)
创建:
这篇关于叠加两个ggplot facet_wrap直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!