ggplot2:在aes(..)和geom_bar(..)中使用`fill =…`.颜色重复 [英] ggplot2: Using `fill = …` in aes(..) and geom_bar(..). The colors repeat
问题描述
这是带有 ggplot
的条形图:
library(ggplot2)
ggplot(subset(dat, Gene=='3_RH2B'), aes(x=Morpho, y=Weights, fill=Model2)) + geom_bar(stat='identity') + ggtitle('RH2B')
我的问题是颜色重复出现,而不是形成一个大块.我希望每个条形图由三个颜色块组成,分别对应于变量 dat $ Model2
的三个级别.我怎样才能做到这一点?ggplot为什么创建此图而不是直接创建我想要的图?
My problem is that the colors repeat instead of forming one big block. I would like that each bar is formed by three blocks of color corresponding to the three levels of the variable dat$Model2
. How can I achieve this? Why does ggplot create this graph and not directly the one I'd like?
这是data.frame dat
:
Here is the data.frame dat
:
Gene Morpho Model Weights Model2
1 1_RH1 Morph_PC1 OUMV 0.081666667 OUMx
2 1_RH1 Morph_PC1 OUM 0.093333333 OUMx
3 1_RH1 Morph_PC1 BM1 0.286666667 BMx
4 1_RH1 Morph_PC1 OUMVA 0.191666667 OUMx
5 1_RH1 Morph_PC1 OU1 0.076666667 OU1
6 1_RH1 Morph_PC1 BMS 0.255000000 BMx
7 1_RH1 Morph_PC1 OUMA 0.013333333 OUMx
8 1_RH1 Morph_PC2 OU1 0.106666667 OU1
9 1_RH1 Morph_PC2 BM1 0.030000000 BMx
10 1_RH1 Morph_PC2 OUM 0.226666667 OUMx
11 1_RH1 Morph_PC2 OUMVA 0.346666667 OUMx
12 1_RH1 Morph_PC2 OUMA 0.238333333 OUMx
13 1_RH1 Morph_PC2 OUMV 0.045000000 OUMx
14 1_RH1 Morph_PC2 BMS 0.003333333 BMx
15 2_LWS Morph_PC1 BM1 0.545000000 BMx
16 2_LWS Morph_PC1 BMS 0.253333333 BMx
17 2_LWS Morph_PC1 OUM 0.061666667 OUMx
18 2_LWS Morph_PC1 OUMV 0.018333333 OUMx
19 2_LWS Morph_PC1 OUMA 0.015000000 OUMx
20 2_LWS Morph_PC1 OUMVA 0.110000000 OUMx
21 2_LWS Morph_PC1 OU1 0.000000000 OU1
22 2_LWS Morph_PC2 OU1 0.136666667 OU1
23 2_LWS Morph_PC2 OUM 0.078333333 OUMx
24 2_LWS Morph_PC2 OUMVA 0.373333333 OUMx
25 2_LWS Morph_PC2 BM1 0.028333333 BMx
26 2_LWS Morph_PC2 OUMV 0.018333333 OUMx
27 2_LWS Morph_PC2 OUMA 0.353333333 OUMx
28 2_LWS Morph_PC2 BMS 0.013333333 BMx
29 3_RH2B Morph_PC1 BM1 0.301666667 BMx
30 3_RH2B Morph_PC1 BMS 0.478333333 BMx
31 3_RH2B Morph_PC1 OU1 0.091666667 OU1
32 3_RH2B Morph_PC1 OUM 0.066666667 OUMx
33 3_RH2B Morph_PC1 OUMA 0.028333333 OUMx
34 3_RH2B Morph_PC1 OUMV 0.023333333 OUMx
35 3_RH2B Morph_PC1 OUMVA 0.008333333 OUMx
36 3_RH2B Morph_PC2 OUM 0.246666667 OUMx
37 3_RH2B Morph_PC2 OUMA 0.171666667 OUMx
38 3_RH2B Morph_PC2 OUMV 0.096666667 OUMx
39 3_RH2B Morph_PC2 BMS 0.106666667 BMx
40 3_RH2B Morph_PC2 OU1 0.213333333 OU1
41 3_RH2B Morph_PC2 BM1 0.140000000 BMx
42 3_RH2B Morph_PC2 OUMVA 0.025000000 OUMx
推荐答案
看来您的data.frame是一个汇总表.在这种情况下,在 geom_bar
命令中使用 stat ='identity'
是合适的.除了没有您需要ggplot在摘要表上执行其他聚合.对于第一个堆叠的条形图(MORPH_PC1),要堆叠的组件是有序的,尽管 stat ='identity'
,ggplot将添加适当的权重.但是,如果您更改第一个堆叠条形图的组件顺序,那么它也将包含重复的颜色.例如,将ggplot命令与以下数据框一起使用以查看效果.这是您的数据框,只是 Model2
变量的顺序稍有变化.
It appears that your data.frame is a summary table. In which case, stat = 'identity'
could be appropriate within the geom_bar
command. Except not. You need ggplot to perform additional aggregations on the summary table. For the first stacked bar (MORPH_PC1), the components to be stacked are ordered, and, despite stat='identity'
, ggplot will add the appropriate weights. But if you change the order of the components of the first stacked bar, then it too will contain repeated colours. For instance, use your ggplot command with the following data frame to see the effect. It's your data frame except for a slight change in the order for the Model2
variable.
dat = read.table(text = " Gene Morpho Model Weights Model2
29 3_RH2B Morph_PC1 BM1 0.301666667 BMx
32 3_RH2B Morph_PC1 OUM 0.066666667 OUMx
30 3_RH2B Morph_PC1 BMS 0.478333333 BMx
31 3_RH2B Morph_PC1 OU1 0.091666667 OU1
33 3_RH2B Morph_PC1 OUMA 0.028333333 OUMx
34 3_RH2B Morph_PC1 OUMV 0.023333333 OUMx
35 3_RH2B Morph_PC1 OUMVA 0.008333333 OUMx
36 3_RH2B Morph_PC2 OUM 0.246666667 OUMx
37 3_RH2B Morph_PC2 OUMA 0.171666667 OUMx
38 3_RH2B Morph_PC2 OUMV 0.096666667 OUMx
39 3_RH2B Morph_PC2 BMS 0.106666667 BMx
40 3_RH2B Morph_PC2 OU1 0.213333333 OU1
41 3_RH2B Morph_PC2 BM1 0.140000000 BMx
42 3_RH2B Morph_PC2 OUMVA 0.025000000 OUMx", header = TRUE, sep = "")
@Alpha提供的解决方案的其他解决方案:
Additional solutions to the one offered by @Alpha:
在ggplot2命令之外执行其他聚合,然后绘制:
Perform the additional aggregation outside the ggplot2 command, then plot:
datRevised = aggregate(Weights ~ Morpho + Model2, data = dat, FUN = "sum")
ggplot(datRevised, aes(x=Morpho, y=Weights, fill=Model2)) + geom_bar(stat='identity') + ggtitle('RH2B')
或者,在原始数据框上使用 weight
美观(请参见此处以获取一些详细信息-大约在页面的中间).
Or, use the weight
aesthetic on the original data frame (see here for some details - about half way down the page).
ggplot(dat, aes(x=Morpho, weight=Weights, fill=Model2)) + geom_bar() + ggtitle('RH2B')
这篇关于ggplot2:在aes(..)和geom_bar(..)中使用`fill =…`.颜色重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!