在做基因富集时,有些通路特别长,以至于使图片的大小不好控制,这种情况可以用stringr包的str_wrap来完成文本自动换行。如使用clusterProfiler的barplot时,因为clusterProfiler是基于ggplot2,所以更改ggplot中scale_x_discrete或者scale_y_discrete
1.用到stringr包里的str_wrap函数
library(stringr)
library(ggplot2)
library(clusterProfiler)
x = enrichGO(OrgDb="org.Hs.eg.db",gene = as.vector(gene_input$ENTREZID),ont = "BP", pvalueCutoff = 0.05, readable= TRUE)
p <- barplot(x)
p
效果图:
p + scale_x_discrete(labels=function(x) str_wrap(x, width=20))
如果是dotplot则需要改变scale_y_discrete
p <- dotplot(x)
p
p + scale_y_discrete(labels=function(x) str_wrap(x, width=20))
2.如果不想用到stringr包
get_wraper <- function(width) {
function(x) {
lapply(strwrap(x, width = width, simplify = FALSE), paste, collapse="\n")
}
}
p<- barplot(x)
p+scale_x_discrete(labels = get_wraper(10))
欢迎关注!
参考:
https://mp.weixin.qq.com/s?__biz=MzI5NjUyNzkxMg==&mid=2247483972&idx=1&sn=fa1f4b6299f94eeb52baf5bda6a5ff6b&chksm=ec43b303db343a150806b3b1ab22a1df1c90405235bb399b155f7c50de2a0a23faf2f4ad0beb#rd
https://stackoverflow.com/questions/21878974/auto-wrapping-of-labels-via-labeller-label-wrap-in-ggplot2