用R画Circular barplot图(环状柱形图)
参考文章:可视化之circular barplot
之前利用WGCNA分析得到了一组感兴趣的基因(WGCNA学习笔记),然后又用cytoscape软件得到一个表,里面有每一个node(基因)对应了多少条edge的文件:
这个表里一共有115个基因,degree.layout表示的是每一个基因有多少条edge与其相连。现在想把这个表可视化一下。但是如果做个柱形图,这个表就太长了,而且难看:
所以就想着把它掰弯。。。
(一)方法一
#导入数据,就是上面那个表格
> b = read.csv("weight_morethan0.35_node_degree.csv",header = T,sep = ",")
# 设置几列空表,因为我不想让环闭合,如果你想要闭合的环,可以不用这一步
> empty_bar=10
# 把空表加到原始数据里
> 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 #这一步是设置环的角度
> 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) +
#这一行是给环外侧加注释
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 )+
#这一行是给环内侧加数字
geom_text(aes(y = ifelse(degree.layout >= 10,8, (degree.layout + 3)), color = "red",label = round(degree.layout, 2)), size = 1.5)
> p
(二)方法二
如果我想根据degree.layout的值,把这些基因分成几组,分别用不同的颜色应该怎么弄?
> library(tidyverse)
#导入数据
> data2 = read.csv("weight_morethan0.35_node_degree.csv",header = T,sep = ",")
#把每一组的基因数统计出来
> 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)
#把分组情况加到原始数据里
> 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
现在漂亮多了~
共有 0 条评论