数据可视化
网络组合图
代码
remove(list = ls()) ##清空当前环境
setwd("D:/生信代码复现/R语言绘制网络组合图") ##💚💚💚设置路径
######加载R包
rm(list=ls())
####----loadRPackage----####
library(tidyverse)
library(readxl)
library(tidygraph)
library(ggraph)
library(igraph)
library(cowplot)
library(ggnewscale)
source("layout_function.R")
source("combine_plot.R")
####----loadData(加载数据)----####
#nodefile
nodes<-readxl::read_xlsx(path="nodes.xlsx")
#edgefile
edges<-readxl::read_xlsx(path="edges.xlsx")
#ppifile
ppi<-readxl::read_xlsx(path="ppi.xlsx")
####----Plot----####
graph<-as_tbl_graph(ppi)%>%
tidygraph::mutate(Popularity=centrality_degree(mode='out'))%>%
tidygraph::left_join(nodes,by=c("name"="name"))
####----Plot----####
graph<-as_tbl_graph(ppi)%>%
tidygraph::mutate(Popularity=centrality_degree(mode='out'))%>%
tidygraph::left_join(nodes,by=c("name"="name"))
ly4<-layout_function(graph)
p_ppi<-ggraph(ly4)+
geom_edge_diagonal(aes(color=type))+
geom_node_point(aes(size=Popularity,color=type),alpha=1)+
geom_node_text(data=ly4%>%
tidygraph::filter(name%in%c("Cis","Ctrl")),
aes(label=name),
size=5,
color="#ffffff")+
scale_size(range=c(3,15))+
coord_fixed()+
theme_void()+
theme(legend.position="none")
#### 加载节点数据 ####
nodes <- readxl::read_xlsx("nodes.xlsx")
#### 基于节点数据生成 data_ex ####
data_ex <- nodes %>%
mutate(
id = row_number(), # 为每个节点添加顺序编号
data1 = sample(letters[1:5], n(), replace = TRUE), # 分类数据
data2 = runif(n(), min = 0, max = 20), # 连续数据1
data3 = runif(n(), min = 0, max = 16), # 连续数据2
data4 = runif(n(), min = 0, max = 30) # 柱形图数据
)
####然后是依次画圈####
p2<-ggplot(data=data_ex)+
geom_tile(aes(x=id,y=1,fill=data1),height=0.2)+
scale_fill_manual(values=c('#8dd3c7','#ffffb3','#bebada','#fb8072','#80b1d3'),na.value="transparent")+
new_scale_fill()+
geom_tile(aes(x=id,y=1.25,fill=data2),height=0.2)+
scale_fill_gradient(low="#fcc5c0",high="#dd3497",na.value="transparent")+
new_scale_fill()+
geom_tile(aes(x=id,y=1.5,fill=data3),height=0.2)+
scale_fill_gradient(low="#dadaeb",high="#807dba",na.value="transparent")+
coord_polar(
start=1.17*pi
)+
scale_y_continuous(
limits=c(-1,2.5)
)+
theme_void()
p2
# 保存图片到本地
ggsave(filename = "Output/p2_plot.png", # 保存路径和文件名
plot = p2, # 要保存的图表对象
width = 10, # 宽度(单位:英寸)
height = 10, # 高度(单位:英寸)
dpi = 300) # 分辨率
#####然后是最外面的柱形图#####
data_ex_add_angle<-data_ex%>%
dplyr::mutate(angle=90-360*(id-0.5)/nrow(.)+(1.17*pi))%>%
dplyr::mutate(hjust=ifelse(angle<-90,0,1))%>%
dplyr::mutate(angle=ifelse(angle<-90,angle+150,angle-25))
p3<-ggplot(data=data_ex_add_angle)+
geom_bar(aes(x=id,y=data4),stat="identity",fill="#d9d9d9")+
geom_text(data=data_ex_add_angle%>%dplyr::filter(!name%in%c("Cis","Ctrl")),
aes(x=id,y=data4+1,label=name,hjust=hjust),
angle=data_ex_add_angle%>%dplyr::filter(!name%in%c("Cis","Ctrl"))%>%pull(angle))+
coord_polar(
start=1.17*pi
)+
scale_y_continuous(
limits=c(-50,30)
)+
theme_void()
p3
#####最后是拼图#####
####----combine----####
p_combine<-combine_plot(p2,p_ppi,p3)
p_combine
ggsave(filename="Output/p_combine.pdf",
height=12,
width=12)
