-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fig04.Rmd
3040 lines (2552 loc) · 127 KB
/
Fig04.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
title: "Data analysis"
author: "Aoyue Bi, Daxing Xu"
date: "`r Sys.Date()`"
output:
html_document: # html_notebook, html_document
toc: true
toc_float:
toc_collapsed: true
toc_depth: 3
number_sections: true
theme: simplex # default, united, cerulean, journal, flatly, darkly, readable, spacelab, united, cosmo, lumen, paper, sandstone, simplex, and yeti
highlight: haddock # default, tango, pygments, kate, monochrome, espresso, zenburn, haddock, and breezedark
# bibliography: bibliography.json
---
# Fig4 Expansion Load
```{r setup, echo =F, message = T, warning = F}
library(tidyverse)
library(RColorBrewer)
# library(ggpubr)
# library(egg)
## eval 代码要不要运行
## echo 代码要不要输出
## include 图要不要输出
## warning 警告要不要输出
## message 默认Bin等信息要不要输出
```
#### === Fig4A ===
## Fig4A: DAF-plot by loadGroup deleterious
```{r}
### step1: df
df <- read_tsv("data/015_AAF/001_aaf_bySubspecies/001_aaf_bySubspecies.txt.gz")
### step2: DAF bin
df1 <- df %>%
rename(DAF=AAF) %>%
filter(DAF!=0) %>%
filter(DAF!=1) %>%
mutate(bin=cut_width(DAF,width = 0.2,boundary = 0)) %>%
# mutate(MergedSub=if_else(Sub=="D","D","AB")) %>%
group_by(Group,GenomeType,LoadGroup,Sub) %>%
count(bin) %>%
mutate(fre=n/sum(n))
### step3: filter dataframe
df2 <- df1 %>%
filter(GenomeType=="AABBDD") %>%
filter(Sub == "D") %>%
filter(LoadGroup=="Deleterious") %>%
droplevels()
### step4: set factor levels
levels_A <- c("LR_EA","OtherHexaploids","Cultivar","LR_EU","Tibetan semi-wild","LR_WA","LR_AM","LR_AF","LR_CSA","Spelt","Yunan wheat","Macha","Club wheat","Vavilovii","Indian dwarf wheat","Xinjiang wheat")
df2$Group <- factor(df2$Group,levels = levels_A)
### 为每个亚群添加倍性信息,dfhash 为 dfcolorDB 服务
dfhash <- read_tsv("/Users/Aoyue/project/wheatVMap2_1000/001_germplasm/021_WheatVMap2_GermplasmInfo.txt") %>%
select(Taxa=VcfID,GroupbyContinent,Subspecies_by22_TreeValidated,GenomeType) %>%
mutate(GroupforExpansionLoad = if_else(Subspecies_by22_TreeValidated=="Landrace",GroupbyContinent,Subspecies_by22_TreeValidated)) %>%
select(GenomeType,GroupforExpansionLoad) %>%
filter(!is.na(GroupforExpansionLoad)) %>%
distinct(.,GroupforExpansionLoad,.keep_all = T)
### 每个亚群的颜色信息
dfcolorDB <- read_tsv("data/001_TaxaDB/003_VMap2_colorDB.txt") %>%
filter(ColorCategory %in% c("GroupforExpansionLoad")) %>% droplevels() %>%
left_join(.,dfhash,by=c("Group"="GroupforExpansionLoad")) %>%
### 过滤数据,使之和输入的数据框保持一致
filter(GenomeType =="AABBDD") %>%
### 这里是个重点,我想自定义顺序!
mutate(Label=factor(Label,levels = levels_A)) %>%
arrange(as.factor(Label))
### 设置数据框和画图颜色的因子顺序,使都保持一致
df1$Group <- factor(df1$Group,levels = dfcolorDB$Label)
colB <- dfcolorDB$ColorCode
#### facet by sub
# colB <- c("#e69f00","#004680")
p <- df2 %>%
# filter(Sub %in% c("A","B")) %>%
# filter(GenomeType=="AABBDD") %>%
# filter(LoadGroup=="Deleterious") %>%
# filter(Sub == "D") %>%
# filter(bin %in% c("[0,0.1]","(0.1,0.2]")) %>%
ggplot(aes(x=bin,y=fre,group=Group,color=Group))+
geom_line(alpha=0.9)+
labs(y="Proportion of deleterious SNPs",x="Derived allele frequency")+
scale_color_manual(values = colB)+
# scale_color_manual(values = colorRampPalette(rev(brewer.pal(n = 12, name = "Set3")))(16))+
# scale_fill_manual(values = colB)+
# scale_y_continuous(limits = c(0,1))+
# scale_x_discrete(label=c("0.1","","0.3","","0.5","","0.7","","0.9",""))+
# facet_grid(Sub~Group)+
theme(strip.background = element_blank())+
# theme(axis.text.x = element_text(vjust = 1, hjust = 1, angle = 45))+
theme(
# plot.margin=unit(rep(1,4),'lines'),
panel.background = element_blank(),
panel.border = element_rect(colour = "black", fill=NA, size=0.4),
legend.key = element_blank(),
legend.position = 'none',
text = element_text(size = 6))
p
# ggsave(p,filename = "~/Documents/temp.png",height = 6,width = 8)
library("ggpubr")
ggp_legend <- get_legend(p)
ggsave(as_ggplot(ggp_legend),filename = "~/Documents/legend.pdf",height = 4,width = 1.5)
# ggsave(p,filename = "~/Documents/test.pdf",height = 3.2,width = 3.2) ### 刚好可以把所有图例展示出来
ggsave(p,filename = "~/Documents/test.pdf",height = 2.28,width = 2.28) ### 刚好可以把所有图例展示出来
```
#### === Fig4B ===
## ThetaW only hexaploid
```{r}
### 开始处理 pi 的结果
dfori <- read_tsv("/Users/Aoyue/project/wheatVMap2_1000/001_germplasm/Group/004_taxaDB_GroupforExpansionLoad.txt") %>%
filter(!is.na(GroupforExpansionLoad)) %>%
### 添加过滤条件
filter(GenomeType=="AABBDD")
dfhash <- read_tsv("data/001_TaxaDB/004_taxaDB_GroupforExpansionLoad.txt") %>%
select(GroupforExpansionLoad,GroupforExpansionLoad2,GenomeType,Subspecies_by6_TreeValidated) %>%
distinct(GroupforExpansionLoad,.keep_all = T)
df <- read_tsv("data/014_PopParameters/003_angsd/002_angsd_summary.txt.gz") %>%
filter(tW_mean!=0) %>%
mutate(Sub=str_sub(RefChr,2,2)) %>%
left_join(dfhash,by=c("Group"="GroupforExpansionLoad2")) %>%
### 添加过滤条件
filter(GenomeType=="AABBDD",Sub=="D")
### 统计每个亚基因组内,按照A亚基因组排序
### dfV2 为排序后的亚洲列表
dfV1 <- df %>%
mutate(Subspecies_by6_TreeValidated=factor(Subspecies_by6_TreeValidated,levels = c("Landrace","Cultivar","OtherHexaploids"))) %>%
group_by(GenomeType,Sub,Subspecies_by6_TreeValidated,GroupforExpansionLoad) %>%
summarise(mean = mean(tW_mean)) %>%
arrange(.,GenomeType,Sub,Subspecies_by6_TreeValidated,-mean) %>%
ungroup() %>%
filter(Sub=="D") %>%
select(GroupforExpansionLoad)
# add_row(GroupforExpansionLoad="Strangulata")
### 因子排序
dfori$GroupforExpansionLoad <- factor(dfori$GroupforExpansionLoad,levels = dfV1$GroupforExpansionLoad)
df$GroupforExpansionLoad <- factor(df$GroupforExpansionLoad,levels = dfV1$GroupforExpansionLoad)
### 计算每个亚群的个数,并添加到taxa数据库后面
dftaxaCount <- dfori %>%
select(GroupforExpansionLoad) %>%
group_by(GroupforExpansionLoad) %>%
count() %>%
ungroup() %>%
mutate(GroupforExpansionLoad_AddCount = str_c(GroupforExpansionLoad," (", n, ")",sep = "")) %>%
select(-n)
yvalue <- 30
p <- df %>%
ggplot(aes(x=GroupforExpansionLoad,y=tW_mean))+
labs(y= expression(italic(theta)[italic(W)]), x = "")+
### 加阴影
geom_rect(aes(xmin=1-0.5,xmax=1+0.5,ymin=-Inf,ymax=Inf),fill="#f2f2f2",alpha=0.5,color="white")+
geom_rect(aes(xmin=3-0.5,xmax=3+0.5,ymin=-Inf,ymax=Inf),fill="#f2f2f2",alpha=0.5)+
geom_rect(aes(xmin=5-0.5,xmax=5+0.5,ymin=-Inf,ymax=Inf),fill="#f2f2f2",alpha=0.5)+
geom_rect(aes(xmin=7-0.5,xmax=7+0.5,ymin=-Inf,ymax=Inf),fill="#f2f2f2",alpha=0.5)+
geom_rect(aes(xmin=9-0.5,xmax=9+0.5,ymin=-Inf,ymax=Inf),fill="#f2f2f2",alpha=0.5)+
geom_rect(aes(xmin=11-0.5,xmax=11+0.5,ymin=-Inf,ymax=Inf),fill="#f2f2f2",alpha=0.5)+
geom_rect(aes(xmin=13-0.5,xmax=13+0.5,ymin=-Inf,ymax=Inf),fill="#f2f2f2",alpha=0.5)+
geom_rect(aes(xmin=15-0.5,xmax=15+0.5,ymin=-Inf,ymax=Inf),fill="#f2f2f2",alpha=0.5)+
# geom_rect(aes(xmin=17-0.5,xmax=17+0.5,ymin=-Inf,ymax=Inf),fill="#f2f2f2",alpha=0.5)+
# geom_rect(aes(xmin=19-0.5,xmax=19+0.5,ymin=-Inf,ymax=Inf),fill="#f2f2f2",alpha=0.5)+
# geom_rect(aes(xmin=21-0.5,xmax=21+0.5,ymin=-Inf,ymax=Inf),fill="#f2f2f2",alpha=0.5)+
# geom_rect(aes(xmin=23-0.5,xmax=23+0.5,ymin=-Inf,ymax=Inf),fill="#f2f2f2",alpha=0.5)+
# geom_rect(aes(xmin=25-0.5,xmax=25+0.5,ymin=-Inf,ymax=Inf),fill="#f2f2f2",alpha=0.5)+
# 最上面的条纹
# geom_rect(aes(xmin=1-0.5,xmax=1+0.5,ymin=yvalue,ymax=Inf),fill="#ffd702",alpha=0.5)+
# geom_rect(aes(xmin=2-0.5,xmax=2+0.5,ymin=yvalue,ymax=Inf),fill="#7f5701",alpha=0.5)+
# geom_rect(aes(xmin=3-0.5,xmax=6+0.5,ymin=yvalue,ymax=Inf),fill="#016699",alpha=0.5)+
# geom_rect(aes(xmin=7-0.5,xmax=9+0.5,ymin=yvalue,ymax=Inf),fill="#00f3ff",alpha=0.5)+
geom_rect(aes(xmin=1-0.5,xmax=6+0.5,ymin=yvalue,ymax=Inf),fill="#fc6e6e",alpha=0.5)+
geom_rect(aes(xmin=7-0.5,xmax=7+0.5,ymin=yvalue,ymax=Inf),fill="#9900ff",alpha=0.5)+
geom_rect(aes(xmin=8-0.5,xmax=16+0.5,ymin=yvalue,ymax=Inf),fill="#fe63c2",alpha=0.5)+
# geom_rect(aes(xmin=26-0.5,xmax=26+0.5,ymin=yvalue,ymax=Inf),fill="#87cef9",alpha=0.5)+
# 开始画图
# stat_boxplot(geom = "errorbar",width=0.15,position = position_dodge(1))+ # add error bar
# geom_point(position = position_jitterdodge(0.5),alpha = 0.6,aes(col=Sub),size=0.3)+
# stat_summary(fun.data=mean_sdl,geom="pointrange",size=0.3)+
### boxplot
geom_boxplot(
# aes(color=Sub),
color="black",
position = position_dodge(0.8),
outlier.color = NA,
# outlier.alpha = 0.8,
# outlier.size = 0.7,
alpha=0.8,width=0.7)+ #,width=0.0.7
stat_summary(
aes(group=Sub),
color="red",
fun=mean, geom="point",position = position_dodge(0.8),size=0.4)+
### boxplot and point
# geom_boxplot(position = position_dodge(0.9),outlier.color = 'white',alpha=0.5,outlier.size = 0.5)+
# geom_point(position = position_jitterdodge(0.6),alpha = 0.6,aes(color=GenomeType),size=0.8)+
geom_vline(xintercept = 7.5,linetype="dashed",color="gray")+
# geom_vline(xintercept = 25.5,linetype="dashed",color="black")+
# scale_color_manual(values = c("#fd8582","#967bce","#4bcdc6"),name='')+
scale_x_discrete(labels = dftaxaCount$GroupforExpansionLoad_AddCount)+
theme(axis.text.x= element_text(angle=45, vjust=1,hjust = 1))+
# theme(plot.margin=unit(rep(1.3,4),'lines'))+
theme(
panel.background = element_blank(),
panel.border = element_rect(colour = "black", fill=NA, size=0.5),
legend.position = 'none',
# legend.position = c(0.9,0.6),
text = element_text(size = 7))
p
# ggsave(p,filename = "/Users/Aoyue/Documents/test.pdf",height = 4,width = 7.2)
ggsave(p,filename = "/Users/Aoyue/Documents/test.pdf",height = 2.12,width = 4.23)
ggsave(p,filename = "/Users/Aoyue/Documents/test.pdf",height = 2,width = 3.5)
```
#### === Fig4CDE===
## World map
```{r}
choiceA <- "Ratio_031_GERP16way_1.5andDerived_PolyPhen2_0.5"
### 先处理del文件
dfdel <- read_tsv("data/004_delCount/delRatio.txt.gz")
dftaxaDB <- read_tsv("data/001_TaxaDB/WheatVMap2_GermplasmInfo.txt") %>%
select(Taxa=VcfID,GenomeType,Longitude,Latitude)
df <- dfdel %>% right_join(.,dftaxaDB,by="Taxa") %>%
filter(GenomeType=="AABBDD") %>%
filter(Subgenome=="D") %>%
filter(DelCountGroup == "TotalDerivedSNPCount") %>%
filter(Stand_LoadGroup == choiceA) %>%
select(-c("GenomeType","Subgenome","DelCountGroup","Stand_LoadGroup")) %>% filter(!is.na(Longitude)) ### 802个个体 -> 543 个有经纬度信息
mp<-NULL #定义一个空的地图
# 注意: borders函数中的colour是地图国家边界线的颜色,fill是国家的填充颜色
mapworld<-borders("world",colour = "#dedfe0",fill="#dedfe0") #绘制基本地图
mpvoid<-ggplot()+ mapworld + ylim(-55,90)+theme_void()
colB <- c("#ffd702","#fc6e6e","#87cef9")
mp<-mpvoid +
geom_point(data=df,aes(x=Longitude,y=Latitude
),shape=21,alpha=0.8,size=2,fill="orange")+
theme(legend.position = 'none')
mp
ggsave(mp,filename = "figs/map_s1062_1.pdf", height = 2.76,width = 6.89)
```
## Bubble map
```{r}
choiceA <- "Ratio_031_GERP16way_1.5andDerived_PolyPhen2_0.5"
### 先处理del文件
dfdel <- read_tsv("data/004_delCount/delRatio.txt.gz")
dftaxaDB <- read_tsv("data/001_TaxaDB/WheatVMap2_GermplasmInfo.txt") %>%
select(Taxa=VcfID,GenomeType,Longitude,Latitude)
df <- dfdel %>% right_join(.,dftaxaDB,by="Taxa") %>%
filter(GenomeType=="AABBDD") %>%
filter(Subgenome=="D") %>%
filter(DelCountGroup == "TotalDerivedSNPCount") %>%
filter(Stand_LoadGroup == choiceA) %>% ###******* choice
select(-c("GenomeType","Subgenome","DelCountGroup","Stand_LoadGroup")) %>% filter(!is.na(Longitude)) %>% ### 802个个体 -> 543 个有经纬度信息
arrange(Stand_delCount) #### 排序
mp<-NULL #定义一个空的地图
# 注意: borders函数中的colour是地图国家边界线的颜色,fill是国家的填充颜色
mapworld<-borders("world",colour = "#dedfe0",fill="#dedfe0") #绘制基本地图
mpvoid<-ggplot()+ mapworld + ylim(-55,90)+theme_void()
library(viridis)
mp<-mpvoid +
geom_point(data = df,mapping = aes(x=Longitude,y=Latitude,
size=Stand_delCount, color=Stand_delCount),
alpha = 0.9)+
scale_size_continuous(range=c(1,3))+ ## 0.5-3
# scale_color_manual(values = colorRampPalette(brewer.pal(n = 3, name = "RdBu"))(100))+
# scale_fill_manual(values = colorRampPalette(brewer.pal(n = 3, name = "RdBu"))(100))
# scale_fill_gradientn(colours =rev(colorRampPalette(brewer.pal(n = 3, name = "RdBu"))(100)))+
scale_color_gradientn(colours = rev(colorRampPalette(brewer.pal(n = 3, name = "RdBu"))(100)))
# scale_color_viridis(trans="log") +
# scale_fill_viridis(trans="log")
# theme(legend.position = 'none')
mp
ggsave(mp,filename = "figs/map_s1062_2.pdf", height = 2.76,width = 6.89)
```
## Spatstat world map
```{r}
choiceA <- "Ratio_031_GERP16way_1.5andDerived_PolyPhen2_0.5"
### 先处理del文件
dfdel <- read_tsv("data/004_delCount/delRatio.txt.gz")
dftaxaDB <- read_tsv("data/001_TaxaDB/WheatVMap2_GermplasmInfo.txt") %>%
select(Taxa=VcfID,GenomeType,Longitude,Latitude)
set.seed(123)
df1 <- dfdel %>% right_join(.,dftaxaDB,by="Taxa") %>%
filter(GenomeType=="AABBDD") %>%
filter(Subgenome=="D") %>%
filter(DelCountGroup == "TotalDerivedSNPCount") %>%
filter(Stand_LoadGroup == choiceA) %>%
select(-IfSelected,-Group_refobj) %>%
select(-c("GenomeType","Subgenome","DelCountGroup","Stand_LoadGroup")) %>%
filter(!is.na(Longitude)) %>% ### 802个个体 -> 543 个有经纬度信息
arrange(Stand_delCount)
df <- df1 %>%
mutate(Longitude = Longitude + runif(nrow(df1),0.001,0.005)) ##通过添加随机数,避免重复
```
### plot
```{r}
# library(raster)
# # detach(package:raster,unload = T)
# library(spatstat)
# library(RColorBrewer)
#
# ppp.df <- ppp(df$Longitude, df$Latitude, c(-180, 180), c(-60, 90))
# marks(ppp.df) <- df[, 2] ### load ratio 做为 marks 信息
# # plot(density(ppp.df)) ###画出点的聚集程度 测试
# ppp.df2 <- Smooth(ppp.df) ### 求每个点对应的load值的大小
# ppp.df2.m <- ppp.df2$v ### 获取矩阵内的密度数据
# ###矩阵上下颠倒128 -> 1 ,依次减一
# ppp.df2.m1 <- ppp.df2.m[seq(128, 1, by = -1), ]
#
# ### 创建 raster 类,并将密度的结果填充进去
# df.raster <- raster(nrows = 128, ncols = 128, xmn = -180, xmx = 180, ymn = -60, ymx = 90)
# values(df.raster) <- as.vector(t(as.matrix(ppp.df2.m1)))
# plot(df.raster)
#
# world.shp <- shapefile("data/002_spatstat/spatstat/shapefile/TM_WORLD_BORDERS_SIMPL-0.3/TM_WORLD_BORDERS_SIMPL-0.3.shp")
# df.raster.final <- mask(df.raster, world.shp)
# par(mfrow = c(1,1))
#
# # pdf(file = str_c("figs/Fig4/misc",choiceA,"_wholeWorld.pdf"),height = 3,width = 4)
# ### whole world
# png(filename = str_c("~/Documents/",choiceA,"_wholemap.png"),units = 'in',height = 4,width = 5.3,res = 300)
# plot(df.raster.final, col = colorRampPalette(rev(brewer.pal(n = 3, name = "RdBu")))(100),xaxt="n",yaxt="n")
# # points(df$Longitude, df$Latitude, pch = 16, cex = 0.5,col=rgb(252, 110, 110,90,maxColorValue=255))
# axis(1,at=seq(-150,180,60))
# axis(2,at=seq(-90,90,60))
# dev.off()
#
# ### 亚欧大陆和非洲
# png(filename =str_c("~/Documents/",choiceA,"_Eurasia.png"),units = 'in',height = 4,width = 5.3,res = 300)
# # pdf( "~/Documents/test.pdf")
# plot(df.raster.final, col = colorRampPalette(rev(brewer.pal(n = 3, name = "RdBu")))(100),xlim=c(-20,150),ylim=c(-35,62),xaxt="n",yaxt="n")
# # points(df$Longitude, df$Latitude, pch = 16, cex = 0.8,col=rgb(252, 110, 110,90,maxColorValue=255))
# axis(1,at=seq(0,150,30))
# axis(2,at=seq(-20,62,20))
# # axis(1,at=seq(0,150,30),labels = c("0","","","","",""))
# # axis(2,at=seq(0,62,20))
# dev.off()
#
# ### 美洲和非洲
# png(filename = str_c("~/Documents/",choiceA,"_American.png"),units = 'in',height = 4,width = 5.3,res = 300)
# plot(df.raster.final, col = colorRampPalette(rev(brewer.pal(n = 3, name = "RdBu")))(100),xlim=c(-150,60),ylim=c(-60,62),xaxt="n",yaxt="n")
# # points(df$Longitude, df$Latitude, pch = 16, cex = 0.8,col=rgb(252, 110, 110,90,maxColorValue=255))
# axis(1,at=seq(-150,60,30))
# axis(2,at=seq(-60,62,30))
# dev.off()
```
#### === Fig4F===
## boxplot load
- 可以选择定义有害突变的方式
unique(dfdel$Stand_LoadGroup)
```{r}
choiceA <- "Ratio_031_GERP16way_1.5andDerived_PolyPhen2_0.5"
dfdel <- read_tsv("data/004_delCount/delRatio.txt.gz")
### ********************** Section1 *********************************###
### taxa 属性数据框
factorA <- c("Landrace","Cultivar","OtherHexaploids","compactum","sphaerococcum","tibeticum","vavilovii","petropavlovskyi","yunna-nense","macha","spelta","dicoccoides","dicoccum","durum","turanicum","polonicum","turgidum","karamyschevii","ispahanicum","carthlicum","strangulata")
labelsA <- c("Landrace","Cultivar","OtherHexaploids","Club wheat","Indian dwarf wheat","Tibetan semi-wild","Vavilovii","Xinjiang wheat","Yunan wheat","Macha","Spelt","Wild emmer","Domesticated emmer","Durum","Khorasan wheat ","Polish wheat","Rivet wheat","Georgian wheat","Ispahanicum","Persian wheat","Strangulata")
dfhash <- tibble(Subspecies_by22_TreeValidated=factorA,Subspecies22=labelsA)
dftaxaDB <- read_tsv("data/001_TaxaDB/WheatVMap2_GermplasmInfo.txt") %>%
select(Taxa=VcfID,GenomeType,Subspecies_by22_TreeValidated,GroupbyContinent) %>%
left_join(.,dfhash,by="Subspecies_by22_TreeValidated") %>%
mutate(GroupforExpansionLoad = if_else(Subspecies22=="Landrace",GroupbyContinent,Subspecies22)) %>%
select(Taxa,GenomeType,GroupforExpansionLoad)
### ********************** Section2 *********************************###
### del load 数据框
df <- dfdel %>% left_join(.,dftaxaDB,by="Taxa") %>%
filter(Subgenome=="B") %>%
filter(DelCountGroup=="TotalDerivedSNPCount") %>%
filter(GenomeType=="AABBDD") %>%
select(-c(DelCountGroup,IfSelected,Group_refobj,GenomeType)) %>%
filter(!is.na(GroupforExpansionLoad))
### 统计每个亚基因组内,以亚洲为单位的均值,按照D亚基因组排序
### dfV2 为排序后的亚洲列表
dfV1 <- df %>%
group_by(Subgenome,Stand_LoadGroup,GroupforExpansionLoad) %>%
summarise(mean = mean(Stand_delCount)) %>%
arrange(.,Subgenome,Stand_LoadGroup,mean) %>%
ungroup() %>%
filter(Subgenome=="B",Stand_LoadGroup== choiceA) %>%
select(GroupforExpansionLoad)
### 因子排序,排序后计数
df$GroupforExpansionLoad <- factor(df$GroupforExpansionLoad,levels = dfV1$GroupforExpansionLoad)
### 计算每个亚群的个数
### 千万注意数据框的分组情况,如sub VariantsGroup ,计算后核查确认!!!
dftaxaCount <- df %>%
group_by(Subgenome,Stand_LoadGroup,GroupforExpansionLoad) %>%
count() %>% ungroup() %>%
distinct(.,GroupforExpansionLoad,.keep_all = T) %>%
select(-Subgenome) %>%
mutate(GroupforExpansionLoad_AddCount = str_c(GroupforExpansionLoad," (", n, ")",sep = "")) %>%
select(-n)
```
### plot
```{r}
library(RColorBrewer)
p <- df %>%
filter(Stand_LoadGroup==choiceA) %>%
ggplot(aes(x=GroupforExpansionLoad,y= Stand_delCount))+
labs(y=str_c("Mutation load per accession\nby ",choiceA),x="")+
geom_boxplot(aes(fill=GroupforExpansionLoad), outlier.color =NA,alpha=0.6)+ ### width=0.1
geom_point(position = position_jitterdodge(0.6),alpha = 0.4,aes(color=Subgenome),size=0.2)+
stat_summary(aes(group = Subgenome), fun=mean, geom="point", color="blue",position = position_dodge(1),size=0.7)+
scale_color_manual(values =c("#fd8582","#967bce","#4bcdc6"),name='')+
scale_fill_manual(values = colorRampPalette(rev(brewer.pal(n = 1, name = "RdBu")))(16))+
scale_x_discrete(labels = dftaxaCount$GroupforExpansionLoad_AddCount)+
theme(strip.background = element_blank())+
# theme(plot.margin=unit(rep(1.3,4),'lines'))+
coord_flip()+
theme(
panel.background = element_blank(),
panel.border = element_rect(colour = "black", fill=NA, size=0.4),
text = element_text(size = 7),legend.position = 'none')
p
### 竖版
# size=7
ggsave(p,filename = "/Users/Aoyue/Documents/test.pdf",height = 4.2,width = 2.6)
### 横版
# size=14
# ggsave(p,filename = "/Users/Aoyue/Documents/test.pdf",height = 4.2,width = 2.6)
```
#### === Fig4G===
## TajimaD
```{r message=FALSE, warning=FALSE}
df <- read_tsv("data/014_PopParameters/003_angsd/001_angsd_subspecies26_geneRegion_RefChr.txt.gz") %>%
mutate(Sub=str_sub(RefChr,2))
### taxa 属性数据框
factorA <- c("OtherHexaploids","Cultivar","OtherHexaploids","compactum","sphaerococcum","tibeticum","vavilovii","petropavlovskyi","yunna-nense","macha","spelta","dicoccoides","dicoccum","durum","turanicum","polonicum","turgidum","karamyschevii","ispahanicum","carthlicum","strangulata", "LR_WA","LR_EA","LR_EU","LR_CSA","LR_AM","LR_AF")
labelsA <- c("OtherHexaploids","Cultivar","OtherHexaploids","Club wheat","Indian dwarf wheat","Tibetan semi-wild","Vavilovii","Xinjiang wheat","Yunan wheat","Macha","Spelt","Wild emmer","Domesticated emmer","Durum","Khorasan wheat ","Polish wheat","Rivet wheat","Georgian wheat","Ispahanicum","Persian wheat","Strangulata","LR_WA","LR_EA","LR_EU","LR_CSA","LR_AM","LR_AF")
dfhash <- tibble(Group=factorA,Subspecies22=labelsA)
### 得到3列数据,GenomeType,GroupforExpansionLoad,Group
dftaxaDB <- read_tsv("data/001_TaxaDB/WheatVMap2_GermplasmInfo.txt") %>%
select(Taxa=VcfID,GenomeType,Subspecies_by22_TreeValidated,GroupbyContinent) %>%
mutate(Group=if_else(Subspecies_by22_TreeValidated=="Landrace",GroupbyContinent,Subspecies_by22_TreeValidated)) %>%
left_join(.,dfhash,by="Group") %>%
mutate(GroupforExpansionLoad = if_else(Subspecies22=="Landrace",GroupbyContinent,Subspecies22)) %>%
select(GenomeType,GroupforExpansionLoad,Group) %>%
filter(!is.na(GroupforExpansionLoad)) %>%
distinct(GroupforExpansionLoad,.keep_all = T)
### ********************** Section2 *********************************###
###
df <- df %>% left_join(.,dftaxaDB,by="Group")
```
### plot density_ridges TajimaD
```{r}
fun_getLoadOrder <- function(){
### ********************** Section1 *********************************###
### 求作图的顺序和load一致
choiceA <- "Ratio_031_GERP16way_1.5andDerived_PolyPhen2_0.5"
dfdel <- read_tsv("data/004_delCount/delRatio.txt.gz")
### taxa 属性数据框
factorA <- c("Landrace","Cultivar","OtherHexaploids","compactum","sphaerococcum","tibeticum","vavilovii","petropavlovskyi","yunna-nense","macha","spelta","dicoccoides","dicoccum","durum","turanicum","polonicum","turgidum","karamyschevii","ispahanicum","carthlicum","strangulata")
labelsA <- c("Landrace","Cultivar","OtherHexaploids","Club wheat","Indian dwarf wheat","Tibetan semi-wild","Vavilovii","Xinjiang wheat","Yunan wheat","Macha","Spelt","Wild emmer","Domesticated emmer","Durum","Khorasan wheat ","Polish wheat","Rivet wheat","Georgian wheat","Ispahanicum","Persian wheat","Strangulata")
dfhash <- tibble(Subspecies_by22_TreeValidated=factorA,Subspecies22=labelsA)
dftaxaDB <- read_tsv("data/001_TaxaDB/WheatVMap2_GermplasmInfo.txt") %>%
select(Taxa=VcfID,GenomeType,Subspecies_by22_TreeValidated,GroupbyContinent) %>%
left_join(.,dfhash,by="Subspecies_by22_TreeValidated") %>%
mutate(GroupforExpansionLoad = if_else(Subspecies22=="Landrace",GroupbyContinent,Subspecies22)) %>%
select(Taxa,GenomeType,GroupforExpansionLoad)
### del load 数据框
df <- dfdel %>% left_join(.,dftaxaDB,by="Taxa") %>%
filter(Subgenome=="D") %>%
filter(DelCountGroup=="TotalDerivedSNPCount") %>%
filter(GenomeType=="AABBDD") %>%
select(-c(DelCountGroup,IfSelected,Group_refobj,GenomeType)) %>%
filter(!is.na(GroupforExpansionLoad))
### 统计每个亚基因组内,以亚洲为单位的均值,按照D亚基因组排序
### dfV2 为排序后的亚洲列表
dfV1 <- df %>%
group_by(Subgenome,Stand_LoadGroup,GroupforExpansionLoad) %>%
summarise(mean = mean(Stand_delCount)) %>%
arrange(.,Subgenome,Stand_LoadGroup,mean) %>%
ungroup() %>%
filter(Subgenome=="D",Stand_LoadGroup== choiceA) %>%
select(GroupforExpansionLoad)
order <- dfV1$GroupforExpansionLoad
### 因子排序,排序后计数
df$GroupforExpansionLoad <- factor(df$GroupforExpansionLoad,levels = order)
return(order)
}
labelA <- fun_getLoadOrder()
df$GroupforExpansionLoad <- factor(df$GroupforExpansionLoad,levels = labelA)
```
```{r}
library(reshape2)
library(ggridges)
### ********************** Section2 *********************************###
df1 <- df %>%
filter(Sub=="D") %>%
filter(GenomeType=="AABBDD") %>%
select(Chr,WinCenter,tW,tP,Tajima,nSites,GroupforExpansionLoad,RefChr,RefPos,Sub) %>%
# filter(tW>0) %>%
filter(!is.na(Tajima)) %>%
mutate(num=as.numeric(factor(GroupforExpansionLoad)))
p <-
# ggplot(df1,aes(x=Tajima,y=GroupforExpansionLoad,fill = 0.5 - abs(0.5 - stat(ecdf))))+
ggplot(df1,aes(x=Tajima,y=GroupforExpansionLoad,fill = stat(ecdf)))+
# stat_density_ridges(quantile_lines = TRUE, quantiles = 2
# ,scale=4,alpha=0.6, color='gray'
# )+
stat_density_ridges(geom = "density_ridges_gradient", calc_ecdf = TRUE,scale=4,alpha=0.6, color='gray') +
# geom_density_ridges_gradient(scale = 3,size=0.3) +
scale_fill_gradientn(colours = rev(colorRampPalette(brewer.pal(n = 3, name = "RdBu"))(100)))+
# scale_fill_viridis_c(name = "Temp. [F]", option = "C") +
# scale_y_discrete(expand = c(0.01, 0)) +
# scale_x_continuous(expand = c(0, 0),limits = c(-2,4),breaks = seq(-2,4,2))+
coord_cartesian(xlim = c(-1.5,4))+
# theme_ridges(grid = FALSE,center_axis_labels = TRUE)+
# theme_pubr()+
# labs(x= expression(paste("Tajima's ",italic(D))), y = "Group number")+
labs(x= expression(paste("Tajima's ",italic(D))), y = "")+
geom_vline(xintercept = 0, linetype="dotted", color = "blue")+
theme(strip.background = element_blank())+
theme(panel.grid= element_blank(),
panel.background = element_blank(),
axis.line = element_line(size=0.4, colour = "black"),
text = element_text(size = 14.5),legend.position = 'none')
p
### AI size=7
ggsave(p,filename = "~/Documents/test.pdf",height = 3.9,width = 1.96)
### PPT size=14.5
ggsave(p,filename = "~/Documents/test.pdf",height = 6,width = 6)
```
#### =======================
#### ==== Extended Data ====
#### =======================
#### ======
## ****Map by subspecies
- 这里只画有经纬度信息的
```{r}
### ********************** Section1 *********************************###
### taxa 属性数据框
factorA <- c("dicoccoides","dicoccum","durum","turanicum","polonicum","turgidum","karamyschevii","ispahanicum","carthlicum","Landrace","Cultivar","OtherHexaploids","compactum","sphaerococcum","tibeticum","vavilovii","petropavlovskyi","yunna-nense","macha","spelta","strangulata")
labelsA <- c("Wild emmer","Domesticated emmer","Durum","Khorasan wheat","Polish wheat","Rivet wheat","Georgian wheat","Ispahanicum","Persian wheat","Landrace","Cultivar","OtherHexaploids","Club wheat","Indian dwarf wheat","Tibetan semi-wild","Vavilovii","Xinjiang wheat","Yunan wheat","Macha","Spelt","Strangulata")
dfhash <- tibble(Subspecies_by22_TreeValidated=factorA,Subspecies22=labelsA)
dftaxaDB <- read_tsv("data/001_TaxaDB/WheatVMap2_GermplasmInfo.txt") %>%
select(Taxa=VcfID,GenomeType,Subspecies_by22_TreeValidated,GroupbyContinent,Latitude,Longitude,orgCty,Country) %>%
left_join(.,dfhash,by="Subspecies_by22_TreeValidated") %>%
mutate(GroupforExpansionLoad = if_else(Subspecies22=="Landrace",GroupbyContinent,Subspecies22)) %>%
mutate(GroupforExpansionLoad2=
if_else(Subspecies22=="Landrace",GroupbyContinent,Subspecies_by22_TreeValidated)) %>%
select(Taxa,GenomeType,GroupforExpansionLoad,GroupforExpansionLoad2,Latitude,Longitude,orgCty,Country)
# write_tsv(dftaxaDB,file = "/Users/Aoyue/project/wheatVMap2_1000/001_germplasm/Group/002_taxaDB_GroupforExpansionLoad.txt")
### ********************** Section 2 *********************************###
### 先将有经纬度信息的材料进行画图查看,将异常点的位置信息进行校正
dftaxaDB2 <- dftaxaDB %>%
filter(!is.na(Latitude))
### 写出没有经纬度信息的336份材料,根据国家添加人为估计的经纬度信息
dftaxaDB3_noPos <- dftaxaDB %>%
filter(is.na(Latitude))
# write_csv(dftaxaDB3_noPos,file = "/Users/Aoyue/project/wheatVMap2_1000/002_dataAnalysis/026_heter_load_distance/001_noPosition_table/001_taxa_noPosition.csv")
### 写出具体的亚群,手动添加地区注释
dftable <- dftaxaDB %>%
filter(!is.na(GroupforExpansionLoad)) %>%
distinct(GroupforExpansionLoad,.keep_all = T) %>%
select(GenomeType,GroupforExpansionLoad,GroupforExpansionLoad2)
# write_csv(dftable,file = "~/Documents/taxaDB_category.csv")
plotfun_map <- function(fun_df,fun_factor){
mp <- NULL #定义一个空的地图
# 注意: borders函数中的colour是地图国家边界线的颜色,fill是国家的填充颜色
mapworld <- borders("world", colour = "#dedfe0", fill = "#dedfe0") #绘制基本地图
mpvoid <- ggplot() + mapworld + ylim(-55, 90) + theme_void()
mp <- mpvoid +
geom_point(
data = fun_df,
aes(x = Longitude, y = Latitude),
shape = 21,
alpha = 0.8,
size = 1.5,
fill = "orange"
) +
labs(title = fun_factor)+
theme(plot.title = element_text(hjust = 0.5)) +
theme(text = element_text(size = 6))+
theme(legend.position = 'none')
mp
ggsave(mp,filename = str_c("~/Documents/",fun_factor,"_map.pdf"),height = 1.38,width = 3.445)
return(mp)
}
#### 需要修饰的方面
# 1.调节因子顺序
# 2.标题居中,调节字体大小
# 3.调节圆点大小
### 建立2个一模一样的表格,为创建矩阵做准备
df2 <- dftaxaDB2 %>%
filter(!is.na(GroupforExpansionLoad)) %>%
# mutate(GroupforExpansionLoad=factor(GroupforExpansionLoad,levels = factorAo,labels = factorAo)) %>%
# arrange(factor(GroupforExpansionLoad,levels = factorAo)) %>%
group_by(as.character(GroupforExpansionLoad)) %>%
group_map(~plotfun_map(.x,.y))
### 图片保存
q <- ggpubr::ggarrange(plotlist=df2,ncol = 4,nrow = 6)
q
cowplot::save_plot(q,filename = "~/Documents/test.pdf",base_height = 7.2,base_width = 7.2)
# factorAo <- c("Wild emmer","Domesticated emmer","Durum","Khorasan wheat","Polish wheat","Rivet wheat","Georgian wheat","Ispahanicum","Persian wheat","LR_EA","LR_CSA","LR_WA","LR_AF","LR_EU","LR_AM","Cultivar","OtherHexaploids","Club wheat","Indian dwarf wheat","Tibetan semi-wild","Vavilovii","Xinjiang wheat","Yunan wheat","Macha","Spelt","Strangulata")
# factorAo <- c("Wild emmer","Domesticated emmer","Durum","Khorasan wheat ","Polish wheat","Rivet wheat","Ispahanicum","Persian wheat","LR_EA","LR_CSA","LR_WA","LR_AF","LR_EU","LR_AM","Cultivar","OtherHexaploids","Club wheat","Indian dwarf wheat","Tibetan semi-wild","Xinjiang wheat","Macha","Spelt","Strangulata")
### "Georgian wheat","Vavilovii","Yunan wheat"
# q <- ggpubr::ggarrange(plotlist=list(df2[[22]],df2[[3]],df2[[4]],df2[[7]],df2[[17]],
# df2[[18]],df2[[]],df2[[]],df2[[]],df2[[]],
# df2[[]],df2[[]],df2[[]],df2[[]],df2[[]],
# df2[[]],df2[[]],df2[[]],df2[[]],df2[[]],
# df2[[]],df2[[]],df2[[]])
# ,ncol = 4,nrow = 6)
```
## 重要 MAP & Latitude estimation by manual
- 这里根据国家信息手动估计了经纬度
```{r}
### ********************** Section1 *********************************###
# df_all <- read_tsv("/Users/Aoyue/project/wheatVMap2_1000/001_germplasm/Group/002_taxaDB_GroupforExpansionLoad.txt")
#
### step1: 写出没有经纬度信息的336份材料,根据国家添加人为估计的经纬度信息
### "Georgian wheat","Vavilovii","Yunan wheat" 47个国家
#
# df_noPosition <- read_csv("/Users/Aoyue/project/wheatVMap2_1000/002_dataAnalysis/026_heter_load_distance/001_noPosition_table/001_taxa_noPosition.csv")
#
### step2: 输出没有地理位置信息的国家,手动添加信息
# dftest <- df_noPosition %>%
# distinct(Country,.keep_all = T) %>%
# select(4:7)
### write_csv(dftest,file = "~/Documents/country_noLatitude.csv")
#
### step3: 336份没有经纬度信息的,加上经纬度信息和新的一列(是否是自己手动添加经纬度)
# df_manualPosition <- read_csv("/Users/Aoyue/project/wheatVMap2_1000/002_dataAnalysis/026_heter_load_distance/001_noPosition_table/002_country_noLatitude.csv")
#
# df_336 <- df_noPosition %>%
# select(Taxa,GenomeType,GroupforExpansionLoad,GroupforExpansionLoad2,Country) %>%
# left_join(df_manualPosition,by="Country") %>%
# mutate(IfLatitudebyManually="Yes") %>%
# relocate(Country,.after = orgCty)
### write_csv(df_336,file = "/Users/Aoyue/project/wheatVMap2_1000/002_dataAnalysis/026_heter_load_distance/001_noPosition_table/003_taxa_noPosition_addbyManually.csv")
#
### step4: 有些没有国家的材料,有种群信息,没有国家信息,但是可以根据种群信息手动添加经纬度信息。
### 这一步进行剩余10几份材料(15)的手动添加
### df_336_withManuallyPosition
#
### step5: 1062-336= 726 份材料添加注释信息
# df_726_withPosition <- df_all %>%
# anti_join(df_336,by="Taxa") %>%
# mutate(IfLatitudebyManually="No") %>%
# mutate(IfHasCountryInfo="Yes")
#
# df_336_withManuallyPosition <- read_csv("/Users/Aoyue/project/wheatVMap2_1000/002_dataAnalysis/026_heter_load_distance/001_noPosition_table/004_taxa_noPosition_addbyManually_secondTime.csv")
#
# df_1062_withPosition <- df_726_withPosition %>%
# bind_rows(df_336_withManuallyPosition) %>%
# arrange(Taxa)
#
### write_tsv(df_1062_withPosition,file = "/Users/Aoyue/project/wheatVMap2_1000/001_germplasm/Group/003_taxaDB_GroupforExpansionLoad.txt")
### write_csv(df_1062_withPosition,file = "/Users/Aoyue/project/wheatVMap2_1000/001_germplasm/Group/003_taxaDB_GroupforExpansionLoad.csv")
### step6: 添加 6个群的信息
# dftaxaDB <- read_tsv("/Users/Aoyue/project/wheatVMap2_1000/001_germplasm/021_WheatVMap2_GermplasmInfo.txt") %>% select(Taxa=VcfID,Subspecies_by6_TreeValidated)
#
# df_1062_withPosition <- read_tsv("/Users/Aoyue/project/wheatVMap2_1000/001_germplasm/Group/003_taxaDB_GroupforExpansionLoad.txt")
#
# df_1062_update <- df_1062_withPosition %>%
# left_join(dftaxaDB,by="Taxa") %>%
# relocate(Subspecies_by6_TreeValidated,.after = GroupforExpansionLoad2)
###write_tsv(df_1062_update,file = "/Users/Aoyue/project/wheatVMap2_1000/001_germplasm/Group/004_taxaDB_GroupforExpansionLoad.txt")
### ------------ 开始画地图 ------------ ###
### 先建立函数
plotfun_map <- function(fun_df,fun_factor){
mp <- NULL #定义一个空的地图
# 注意: borders函数中的colour是地图国家边界线的颜色,fill是国家的填充颜色
mapworld <- borders("world", colour = "#dedfe0", fill = "#dedfe0") #绘制基本地图
mpvoid <- ggplot() + mapworld + ylim(-55, 90) + theme_void()
mp <- mpvoid +
geom_point(
data = fun_df,
aes(x = Longitude, y = Latitude),
shape = 21,
alpha = 0.8,
size = 1.5,
fill = "orange"
) +
labs(title = fun_factor)+
theme(plot.title = element_text(hjust = 0.5)) +
theme(text = element_text(size = 6))+
theme(legend.position = 'none')
mp
ggsave(mp,filename = str_c("~/Documents/",fun_factor,"_map.pdf"),height = 1.38,width = 3.445)
return(mp)
}
#### 需要修饰的方面
# 1.调节因子顺序
# 2.标题居中,调节字体大小
# 3.调节圆点大小
### 建立2个一模一样的表格,为创建矩阵做准备
### 写出具体的亚群,手动添加地区注释
df2 <- read_tsv("/Users/Aoyue/project/wheatVMap2_1000/001_germplasm/Group/003_taxaDB_GroupforExpansionLoad.txt") %>%
filter(!is.na(GroupforExpansionLoad)) %>%
# mutate(GroupforExpansionLoad=factor(GroupforExpansionLoad,levels = factorAo,labels = factorAo)) %>%
# arrange(factor(GroupforExpansionLoad,levels = factorAo)) %>%
group_by(as.character(GroupforExpansionLoad)) %>%
group_map(~plotfun_map(.x,.y))
### 图片保存
# q <- ggpubr::ggarrange(plotlist=df2,ncol = 4,nrow = 7)
#
# q
#
# cowplot::save_plot(q,filename = "~/Documents/test.pdf",base_height = 7.2,base_width = 7.2)
### 按照自己定义的顺序进行图片保存
q <- ggpubr::ggarrange(plotlist=list(df2[[24]],df2[[3]],df2[[4]],df2[[8]],df2[[18]],
df2[[19]],df2[[5]],df2[[7]],df2[[17]],
df2[[15]],df2[[20]],
df2[[9]],df2[[10]],df2[[11]],df2[[12]],df2[[13]],
df2[[14]],df2[[2]],df2[[16]],
df2[[1]],df2[[6]],df2[[22]],df2[[23]],df2[[25]],df2[[26]],
df2[[21]]
),
labels = as.character(c(1:26)),
font.label = list(size = 7),
ncol = 4,nrow = 7)
cowplot::save_plot(q,filename = "~/Documents/test.pdf",base_height = 7.2,base_width = 7.2)
```
#### ======
## ****DAF
### plot by 642 nonsyn and syn
- 注意:该方法将 del 列为 nonsyn,不画出 del
```{r}
### step1: df
df <- read_tsv("data/015_AAF/001_aaf_bySubspecies/001_aaf_bySubspecies.txt.gz",show_col_types = FALSE)
### step2: DAF bin
df1 <- df %>%
mutate(VariantsType = ifelse(LoadGroup=="Synonymous","Synonymous","Nonsynonymous")) %>%
select(-LoadGroup) %>%
rename(LoadGroup=VariantsType) %>%
rename(DAF=AAF) %>%
filter(DAF!=0) %>%
filter(DAF!=1) %>%
mutate(bin=cut_width(DAF,width = 0.1,boundary = 0)) %>%
group_by(GenomeType,Sub,LoadGroup,Group) %>%
count(bin) %>%
mutate(fre=n/sum(n))
df3 <- df %>%
mutate(VariantsType = ifelse(LoadGroup=="Synonymous","Synonymous","Nonsynonymous")) %>%
select(-LoadGroup) %>%
rename(LoadGroup=VariantsType) %>%
rename(DAF=AAF) %>%
filter(DAF!=0) %>%
filter(DAF!=1) %>%
mutate(bin=cut_width(DAF,width = 0.2,boundary = 0)) %>%
group_by(GenomeType,Sub,LoadGroup,Group) %>%
count(bin) %>%
mutate(fre=n/sum(n))
### step3: plot_function
fun_plotDAF <- function(df_fun,factor_fun){
### df_fun 提取因子信息
fun_genometype <- factor_fun$GenomeType
fun_sub <- factor_fun$Sub
fun_loadgroup <- factor_fun$LoadGroup
### 设置因子顺序,由于 设置的 0.1 bin 导致有些亚群没有颜色信息
factorA <- df3 %>%
filter(GenomeType == fun_genometype,
Sub == fun_sub,
LoadGroup == fun_loadgroup) %>%
filter(bin=="[0,0.2]") %>%
arrange(-fre) %>%
pull(Group)
### 为每个亚群添加倍性信息,dfhash 为 dfcolorDB 服务
dfhash <-
read_tsv("data/001_TaxaDB/004_taxaDB_GroupforExpansionLoad.txt",show_col_types = FALSE) %>%
select(GenomeType, GroupforExpansionLoad,GroupforExpansionLoad2) %>%
filter(!is.na(GroupforExpansionLoad)) %>%
distinct(., GroupforExpansionLoad, .keep_all = T)
### 每个亚群的颜色信息
dfcolorDB <- read_tsv("data/001_TaxaDB/003_VMap2_colorDB.txt",show_col_types = FALSE) %>%
filter(ColorCategory %in% c("GroupforExpansionLoad")) %>% droplevels() %>%
left_join(dfhash, by = c("Group" = "GroupforExpansionLoad2")) %>%
### 过滤数据,使之和输入的数据框 df_fun 保持一致
filter(GenomeType == fun_genometype) %>%
mutate(Label=factor(Label,levels = factorA)) %>%
arrange(as.factor(Label))
### 设置数据框和画图颜色的因子顺序,使都保持一致
colB <- dfcolorDB$ColorCode
df_fun$Group <- factor(df_fun$Group,levels = factorA)
q <- df_fun %>%
ggplot(aes(
x = bin,
y = fre,
group = Group,
color = Group
)) +
geom_line(alpha = 0.9) +
labs(y=str_c("Proportion of ",str_to_lower(fun_loadgroup)," SNPs"),
x="Derived allele frequency")+
# labs(y = "Proportion of nonsynonymous SNPs", x = "Derived allele frequency") +
scale_color_manual(values = colB) +
# scale_color_manual(values = colorRampPalette(rev(brewer.pal(n = 12, name = "Set3")))(16))+
# scale_fill_manual(values = colB)+
# scale_y_continuous(limits = c(0,1))+
scale_x_discrete(label = c("0.1", "", "0.3", "", "0.5", "", "0.7", "", "0.9", "")) +
# facet_grid(Sub~Group)+
theme(strip.background = element_blank()) +
# theme(axis.text.x = element_text(vjust = 1, hjust = 1, angle = 45))+
theme(
# plot.margin=unit(rep(1,4),'lines'),
panel.background = element_blank(),
panel.border = element_rect(
colour = "black",
fill = NA,
size = 0.7
),
legend.key = element_blank(),
legend.position = 'none',
text = element_text(size = 9)
)
q
# ggsave(q,filename = "~/Documents/temp.png",height = 6,width = 8)
fun_genometype <- factor_fun$GenomeType
fun_sub <- factor_fun$Sub
fun_loadgroup <- factor_fun$LoadGroup
filename_fun <- str_c("~/Documents/",fun_genometype,"_",fun_sub,"Sub","_",fun_loadgroup,".pdf")
ggsave(q,
# filename = "~/Documents/test.pdf",
filename = filename_fun,
height = 4.2,
width = 7) ### 刚好可以把所有图例展示出来
### 打出出图顺序
print(filename_fun)
return(q)
}
### step4: invoke the plot function
df2 <- df1 %>%
group_by(GenomeType,Sub,LoadGroup) %>%
group_map(~fun_plotDAF(.x,.y))
## step5: combine figs automatically or manually
### 先保存6倍体的,再保存4倍体的,最后2倍体
q <- ggpubr::ggarrange(plotlist=df2,ncol = 3)
q
### 六倍体
q <- ggpubr::ggarrange(plotlist=list(df2[[6]],df2[[8]],df2[[10]],df2[[5]],df2[[7]],df2[[9]]),
ncol = 3,nrow = 2)
q
cowplot::save_plot(q,filename = "~/Documents/AABBDD.pdf",base_height = 4.45,base_width = 7.2)
### 四倍体
q <- ggpubr::ggarrange(plotlist=list(df2[[2]],df2[[4]],
df2[[1]],df2[[3]]),
ncol = 2,nrow = 2)
q
cowplot::save_plot(q,filename = "~/Documents/AABB.pdf",base_height = 4.45,base_width = 7.2)
```
### plot by 642 del nonsyn and syn
- 注意:该方法将 del 列为 nonsyn,不画出 del