簡體   English   中英

如何將x軸上的長標簽插入ggplot2窗口?

[英]How to fit long labels on the x-axis into the ggplot2 window?

我正在嘗試為我的GO richemnt類別創建圖像。 不幸的是,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圖例問題


這是創建繪圖所需的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)) 

如評論中所建議+使用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()
  )

修訂圖

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM