原文
来自生信菜鸟团,又添加了三个配色!
## load data
load(url("https://raw.githubusercontent.com/x2yline/Rdata/master/data%20visualization%20R/go_enrich_df.Rdata"))
head(go_enrich_df)
## numbers as data on x axis
go_enrich_df$number <- factor(rev(1:nrow(go_enrich_df)))
## shorten the names of GO terms
shorten_names <- function(x, n_word=4) {
if (length(strsplit(x, " ")[[1]]) > n_word)
{
return(paste(paste(strsplit(x, " ")[[1]][1:n_word],
collapse=" "), "...", sep=""))
}
else
{
return(x)
}
}
labels=(sapply(
levels(go_enrich_df$Description)[as.numeric(go_enrich_df$Description)],
shorten_names))
names(labels) = rev(1:nrow(go_enrich_df))
## colors for bar
CPCOLS <- c("#66C3A5", "#8DA1CB", "#FD8D62")
#CPCOLS<-c("#CC6666", "#9999CC", "#66CC99")
#CPCOLS<-c("#999999", "#E69F00", "#56B4E9")
#CPCOLS<-alpha(c("blue", "red", "yellow"), .8)
library(ggplot2)
ggplot(data=go_enrich_df, aes(x=number, y=GeneNumber, fill=type)) +
geom_bar(stat="identity", width=0.8) + coord_flip() +
scale_fill_manual(values = CPCOLS) + theme_bw() +
scale_x_discrete(labels=labels) +
xlab("GO term") +
theme(axis.text=element_text(face = "bold", color="gray50")) +
labs(title = "The Most Enriched GO Terms")
备注:
#CPCOLS<-c("#CC6666", "#9999CC", "#66CC99")
#CPCOLS<-c("#999999", "#E69F00", "#56B4E9")
#CPCOLS<-alpha(c("blue", "red", "yellow"), .8)
## 为新添加的配色, alpha选择透明度。
参考
http://www.biotrainee.com/thread-242-1-1.html
http://www.cookbook-r.com/Graphs/Colors_(ggplot2)/
https://zhuanlan.zhihu.com/p/27093478
https://zhuanlan.zhihu.com/p/25173606