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
| rm(list = ls())
library(ggraph) library(igraph) library(tidyverse) library(viridis)
df.abun <- NULL
for (i in 1:3) { df <- readxl::read_excel("test.xlsx", sheet = i)
df.ft <- NULL
df.1 <- df %>% dplyr::select("K", "P", "C", "O", "F", "G") for (k in 1:(ncol(df.1) - 1)) { df.temp <- df.1[, c(k, k + 1)] colnames(df.temp) <- c("from", "to") df.ft <- rbind(df.ft, df.temp) }
df.ft <- df.ft %>% dplyr::mutate( from = stringr::str_sub(from, 4, nchar(from)), to = stringr::str_sub(to, 4, nchar(to)) ) %>% dplyr::filter(from != "" & to != "")
df.size <- NULL
for (j in c("K", "P", "C", "O", "F", "G")) { df.1 <- df %>% dplyr::select(average_abundance, `j`) colnames(df.1)[2] <- "cat" df.1 <- df.1 %>% dplyr::group_by(cat) %>% dplyr::mutate(sum = sum(average_abundance)) %>% dplyr::select(2:3) %>% dplyr::ungroup() %>% dplyr::distinct_all() colnames(df.1) <- c("name", "size") df.size <- rbind(df.size, df.1) }
df.size <- df.size %>% dplyr::mutate(name = stringr::str_sub(name, 4, nchar(name))) %>% dplyr::filter(name != "") %>% dplyr::group_by(name) %>% dplyr::mutate(n = n()) %>% dplyr::filter(n <= 1) %>% dplyr::ungroup() %>% dplyr::mutate(group = ifelse(i == 1, "接种后", ifelse(i == 2, "CK", "Air") ))
df.abun <- rbind(df.abun, df.size)
df.ft <- df.ft %>% dplyr::filter(from %in% df.size$name) %>% dplyr::filter(to %in% df.size$name)
mygraph <- graph_from_data_frame(df.ft, vertices = df.size)
group <- ifelse(i == 1, "接种后", ifelse(i == 2, "CK", "Air") )
set.seed(123) ggraph(mygraph, layout = "circlepack", weight = size) + geom_node_circle(aes(fill = depth)) + geom_node_text(aes(label = name, filter = leaf, fill = depth, size = size)) + theme_void() + coord_fixed() + theme(legend.position = "FALSE") + scale_fill_viridis() -> p1
export::graph2ppt(p1, file = paste0(group, "(有标签).pptx"), width = 10, height = 10 )
set.seed(123) ggraph(mygraph, layout = "circlepack", weight = size) + geom_node_circle(aes(fill = depth)) + theme_void() + coord_fixed() + theme(legend.position = "FALSE") + scale_fill_viridis() -> p2 export::graph2ppt(p2, file = paste0(group, "(无标签).pptx"), width = 10, height = 10 ) }
df.size %>% dplyr::select(1, 2, 4) %>% as.data.frame() %>% xlsx::write.xlsx( file = "丰度数据.xlsx", row.names = FALSE )
|