modified: src1/worker.c
[GalaxyCodeBases.git] / R / etc / linkedSegments.r
blob2cdafc0b5993516bb90a783b6e2c008aec5cfea7
1 #!/usr/bin/env littler
2 # 三组或更多组的画法,只需添加数据即可
3 # https://mp.weixin.qq.com/s/FZWinr14RTs6YSUE_juaug
4 # Galaxy实测,两组也行。
6 library(tidyverse)
8 df <- data.frame(
9 Phylum=c("Ruminococcaceae","Bacteroidaceae","Eubacteriaceae","Lachnospiraceae","Porphyromonadaceae"),
10 GroupA=c(37.7397,31.34317,222.08827,5.08956,3.7393),
11 GroupB=c(113.2191,94.02951,66.26481,15.26868,11.2179),
12 GroupC=c(123.2191,94.02951,46.26481,35.26868,1.2179),
13 GroupD=c(37.7397,31.34317,222.08827,5.08956,3.7393)
16 df.long <- df %>% gather(group, abundance, -Phylum)
18 ## 组间连线数据:
19 ## 假设第一列是Phylum
20 link_dat <- df %>%
21 arrange(by=desc(Phylum)) %>%
22 mutate_if(is.numeric, cumsum)
23 bar.width <- 0.7
24 link_dat <- link_dat[, c(1,2,rep(3:(ncol(link_dat)-1),each=2), ncol(link_dat))]
25 link_dat <- data.frame(y=t(matrix(t(link_dat[,-1]), nrow=2)))
26 link_dat$x.1 <- 1:(ncol(df)-2)+bar.width/2
27 link_dat$x.2 <- 1:(ncol(df)-2)+(1-bar.width/2)
29 p <- ggplot(df.long, aes(x=group, y=abundance, fill=Phylum)) +
30 geom_bar(stat = "identity", width=bar.width, col='black') +
31 geom_segment(data=link_dat, aes(x=x.1, xend=x.2, y=y.1, yend=y.2), inherit.aes = F)
33 pdf(file="linkedSegments.pdf")
34 print(p)
35 dev.off()