2017-05-15 44 views
0

我正在嘗試爲我的GO增強類別創建一個圖像。不幸的是ggplot2沒有做我想做的事情。 我的腳本如下:如何將x軸上的長標籤放入ggplot2窗口?

sub <- readRDS("sub.RDS") 
zsc.col <- c("firebrick1", "white", "dodgerblue1") 

leg <- theme(legend.position = "bottom") 
g <- ggplot(sub, aes(x = factor(term, levels = stats::reorder(term, adj_pval)), y = adj_pval, fill = zscore)) + 
    geom_bar(stat = "identity", colour = "black") + 
    scale_fill_gradient2("z-score", space = "Lab", low = zsc.col[3], mid = zsc.col[2], high = zsc.col[1], guide = guide_colourbar(title.position = "top", title.hjust = 0.5), breaks = c(min(sub$zscore), max(sub$zscore)), labels = c("decreasing", "increasing")) + 
    labs(title = "title", x = "", y = "-log (adj p-value)") + 
    leg + 
    theme(axis.text.x = element_text(angle = 45, vjust = 0.5 , hjust = 1), 
       axis.line = element_line(colour = "grey80"), axis.ticks = element_line(colour = "grey80"), 
       axis.title = element_text(size = 8, face = "bold"), axis.text = element_text(size = 8) #, 
       #    panel.background = element_blank(), panel.border = element_blank(), 
       #    panel.grid.major = element_blank(), panel.grid.minor = element_blank(), 
       #    plot.background = element_blank() 
) 

g 

結果顯示在下面。第一個問題是x軸和圖之間的空間。我怎樣才能消除這個空間,以更好地適應劇情的文字? 比我想窗口足夠大,以適應x軸上的整個文本? 這可能嗎?

感謝

阿薩

積結果: ggplot2 legend problem


這裏是一個需要創建的情節sub相關列的dput()

sub <- structure(list(term = c("mitochondrion", "ribosome", "nematode larval development", 
"embryo development ending in birth or egg hatching", "structural constituent of ribosome", 
"translation", "mitochondrial small ribosomal subunit", "receptor-mediated endocytosis", 
"reproduction", "mitochondrial translation", "mitochondrial large ribosomal subunit", 
"mitochondrial inner membrane", "intracellular ribonucleoprotein complex", 
"determination of adult lifespan", "mitochondrial respiratory chain complex I", 
"mitochondrial ribosome", "ATP synthesis coupled proton transport", 
"mitochondrial proton-transporting ATP synthase complex, coupling factor F(o)", 
"tRNA aminoacylation for protein translation", "aminoacyl-tRNA ligase activity", 
"respiratory chain", "NADH dehydrogenase (ubiquinone) activity", 
"protein import into mitochondrial matrix", "intracellular", 
"iron-sulfur cluster binding", "hydrogen ion transmembrane transporter activity", 
"NADH dehydrogenase activity", "oxidoreductase activity, acting on NAD(P)H", 
"rRNA binding", "proton-transporting ATP synthase activity, rotational mechanism", 
"ligase activity", "mitochondrial respiratory chain complex III assembly", 
"oxidation-reduction process", "4 iron, 4 sulfur cluster binding", 
"large ribosomal subunit", "mitochondrial membrane", "mitochondrial matrix", 
"hydrogen ion transmembrane transport", "RNA binding", "cytochrome-c oxidase activity", 
"mitochondrial electron transport, NADH to ubiquinone", "mitochondrial outer membrane", 
"electron carrier activity", "mitochondrion organization", "tricarboxylic acid cycle", 
"growth", "mitochondrial outer membrane translocase complex", 
"proton-transporting ATP synthase complex, coupling factor F(o)", 
"endoplasmic reticulum unfolded protein response", "mitochondrial fusion", 
"mitochondrial inner membrane presequence translocase complex", 
"integral component of mitochondrial outer membrane", "protein transport", 
"mitochondrial electron transport, ubiquinol to cytochrome c", 
"proton transport", "mitochondrial electron transport, cytochrome c to oxygen", 
"positive regulation of multicellular organism growth", "GTPase activity", 
"protein channel activity", "hermaphrodite genitalia development", 
"nucleotide binding", "ATP biosynthetic process", "tRNA aminoacylation" 
), adj_pval = c(-1.84472129469146, -1.66688191109855, -1.62368742458875, 
-1.61251572036108, -1.49190847373791, -1.43330416336608, -1.3289293432033, 
-1.31769812469356, -1.29791331210987, -1.24849426411943, -1.13689595417821, 
-1.11242658986886, -1.05590022744309, -0.821808984933817, -0.816532663342381, 
-0.69535971673032, -0.60705167217058, -0.504866337850967, -0.50177430528028, 
-0.453524900193783, -0.445747643713484, -0.358775899528351, -0.279592150254413, 
-0.162917071897043, -0.150167590044187, -0.0852269401349886, 
-0.0807932210009663, -0.0135543009023059, 0.189719894742601, 
0.289792271789456, 0.291668388670687, 0.913510098585626, NaN, 
NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, 
NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, 
NaN, NaN, NaN, NaN), zscore = c(8.48528137423857, 6.557438524302, 
9.64365076099295, 10.3440804327886, 5.91607978309962, 6.08276253029822, 
4, 6.85565460040104, 8.60232526704263, 3.87298334620742, 3.46410161513775, 
4.24264068711929, 4.35889894354067, 5.91607978309962, 3, 2.44948974278318, 
2.64575131106459, 2.23606797749979, 2.64575131106459, 2.64575131106459, 
2.44948974278318, 2.44948974278318, 2.23606797749979, 3.87298334620742, 
2.44948974278318, 2.23606797749979, 2, 2, 2.23606797749979, 2, 
2.82842712474619, 1.73205080756888, 3.87298334620742, 2, 1.73205080756888, 
2, 2.23606797749979, 2, 3.16227766016838, 1.73205080756888, 1.73205080756888, 
1.73205080756888, 2, 1.73205080756888, 1.73205080756888, 2.44948974278318, 
1.41421356237309, 1.41421356237309, 2, 1.41421356237309, 1.41421356237309, 
1.41421356237309, 2.23606797749979, 1.41421356237309, 1.73205080756888, 
1.41421356237309, 2.82842712474619, 2, 1.41421356237309, 3.60555127546399, 
4, 1.41421356237309, 1.41421356237309)), .Names = c("term", "adj_pval", 
"zscore"), class = "data.frame", row.names = c(1L, 73L, 116L, 
209L, 316L, 351L, 388L, 404L, 451L, 525L, 540L, 552L, 570L, 589L, 
624L, 633L, 639L, 646L, 651L, 658L, 665L, 671L, 677L, 682L, 697L, 
703L, 708L, 712L, 716L, 721L, 725L, 733L, 736L, 751L, 755L, 758L, 
762L, 767L, 771L, 781L, 784L, 787L, 790L, 794L, 797L, 800L, 806L, 
808L, 810L, 814L, 816L, 818L, 820L, 825L, 827L, 830L, 832L, 840L, 
844L, 846L, 859L, 875L, 877L)) 
+2

你更容易得到幫助,不需要人來下載東西的例子。一般來說,我建議使用「coord_flip」使圖形保持水平,然後用'strwrap'將這些標籤包裝在多行上。 – Axeman

+1

另外,我認爲'-log(p)'的值應該是從'0'到'Inf'的範圍,你怎麼得到負值呢? P值大於1? – Axeman

+2

在'axis.text.x = element_text(angle = 45,vjust = 0.5,hjust = 1)'設置vjust爲1以擺脫空白 – timfaber

回答

2

由於使用coord_flip(@Axeman)在評論+建議:

ggplot(sub, aes(x = factor(term, levels = stats::reorder(term, adj_pval)), y = adj_pval, fill = zscore)) + 
    geom_bar(stat = "identity", colour = "black") + 
    scale_fill_gradient2("z-score", space = "Lab", low = zsc.col[3], mid = zsc.col[2], high = zsc.col[1], guide = guide_colourbar(title.position = "top", title.hjust = 0.5), breaks = c(min(sub$zscore), max(sub$zscore)), labels = c("decreasing", "increasing")) + 
    labs(title = "title", x = "", y = "-log (adj p-value)") + 
    leg + coord_flip() + 
    theme(axis.text.x = element_text(angle = 0, vjust = 1 , hjust = 1), 
     axis.line = element_line(colour = "grey80"), axis.ticks = element_line(colour = "grey80"), 
     axis.title = element_text(size = 8, face = "bold"), axis.text = element_text(size = 8), 
     legend.position = "bottom",legend.margin = margin(t=0.5)#, 
     #    panel.background = element_blank(), panel.border = element_blank(), 
     #    panel.grid.major = element_blank(), panel.grid.minor = element_blank(), 
     #    plot.background = element_blank() 
) 

revised plot