參考文章:可視化之circular barplot
之前利用WGCNA分析得到了一組感興趣的基因(WGCNA學(xué)習(xí)筆記)号枕,然后又用cytoscape軟件得到一個表,里面有每一個node(基因)對應(yīng)了多少條edge的文件:
這個表里一共有115個基因湿故,degree.layout表示的是每一個基因有多少條edge與其相連“⒊茫現(xiàn)在想把這個表可視化一下。但是如果做個柱形圖坛猪,這個表就太長了脖阵,而且難看:
所以就想著把它掰彎墅茉。命黔。。
(一)方法一
#導(dǎo)入數(shù)據(jù)就斤,就是上面那個表格
> b = read.csv("weight_morethan0.35_node_degree.csv",header = T,sep = ",")
# 設(shè)置幾列空表悍募,因為我不想讓環(huán)閉合,如果你想要閉合的環(huán)洋机,可以不用這一步
> empty_bar=10
# 把空表加到原始數(shù)據(jù)里
> to_add = matrix(NA, empty_bar, ncol(b))
> colnames(to_add) = colnames(b)
> data=rbind(b, to_add)
> data$id=seq(1, nrow(data))
# 加名字
> label_data = data
> number_of_bar = nrow(label_data)
> angle= 90 - 360 * (label_data$id - 0.5) /number_of_bar #這一步是設(shè)置環(huán)的角度
> label_data$hjust<-ifelse( angle < -90, 1, 0)
> label_data$angle<-ifelse(angle < -90, angle+180, angle)
# 畫圖
> p = ggplot(data, aes(x=as.factor(id), y=degree.layout)) +
geom_bar(stat="identity", fill=alpha("blue", 0.7)) +
ylim(-100,120) +
theme_minimal() +
theme(
axis.text = element_blank(),
axis.title = element_blank(),
panel.grid = element_blank(),
plot.margin = unit(rep(-1,4), "cm")
) +
coord_polar(start = 0) +
#這一行是給環(huán)外側(cè)加注釋
geom_text(data=label_data, aes(x=id, y=degree.layout+20, label=name, hjust=hjust), color="black", fontface="bold",alpha=0.6, size=2.5, angle= label_data$angle, inherit.aes = FALSE )+
#這一行是給環(huán)內(nèi)側(cè)加數(shù)字
geom_text(aes(y = ifelse(degree.layout >= 10,8, (degree.layout + 3)), color = "red",label = round(degree.layout, 2)), size = 1.5)
> p
(二)方法二
如果我想根據(jù)degree.layout的值坠宴,把這些基因分成幾組,分別用不同的顏色應(yīng)該怎么弄绷旗?
> library(tidyverse)
#導(dǎo)入數(shù)據(jù)
> data2 = read.csv("weight_morethan0.35_node_degree.csv",header = T,sep = ",")
#把每一組的基因數(shù)統(tǒng)計出來
> A = sum(data2$degree.layout >=60)
> B = sum(data2$degree.layout >=50) - A
> C = sum(data2$degree.layout >=40) - A - B
> D = sum(data2$degree.layout >=30) - (A+B+C)
> E = sum(data2$degree.layout >=20) - (A+B+C+D)
> f = sum(data2$degree.layout >=10) - (A+B+C+D+E)
> G = sum(data2$degree.layout < 10)
#把分組情況加到原始數(shù)據(jù)里
> data3 = data.frame(
data2,
group=c(rep('A', A), rep('B', B), rep('C', C), rep('D', D),rep('E',E),rep('F',f),rep('G',G))
)
# 加空列
> empty_bar2=4
> to_add2 = data.frame(matrix(NA, empty_bar2*nlevels(data3$group), ncol(data3)))
> colnames(to_add2) = colnames(data3)
> to_add2$group=rep(levels(data3$group), each=empty_bar2)
> data4 = rbind(data3, to_add2)
> data4 = data4 %>% arrange(group)
> data4$id=seq(1, nrow(data4))
# 加名字
> label_data2 = data4
> number_of_bar2 = nrow(label_data2)
> angle2= 90 - 360 * (label_data2$id-0.5) /number_of_bar2
> label_data2$hjust<-ifelse(angle2 < -90, 1, 0)
> label_data2$angle<-ifelse(angle2 < -90, angle2 +180, angle2)
# 畫圖
> p = ggplot(data4, aes(x=as.factor(id), y=degree.layout, fill=group)) +
geom_bar(stat="identity", alpha=0.5) +
ylim(-100,120) +
theme_minimal() +
theme(
legend.position = "none",
axis.text = element_blank(),
axis.title = element_blank(),
panel.grid = element_blank(),
plot.margin = unit(rep(-1,4), "cm")
) +
coord_polar() +
geom_text(data=label_data2, aes(x=id, y=degree.layout + 20, label=name, hjust=hjust), color="black", fontface="bold",alpha=0.6, size=2.5, angle= label_data2$angle, inherit.aes = FALSE )+
geom_text(aes(y = ifelse(degree.layout >= 10,8, (degree.layout + 3)), label = round(degree.layout, 2)), size = 1.5)
> p
現(xiàn)在漂亮多了~