2017-03-06 57 views
-1

我有一個具體問題(至少我喜歡認爲:))。我想對齊兩列兩行的三個地塊。對齊應該在中心圖之後(見圖;圖C),其中圖A應該在圖C的x軸之後對齊,並且圖D應該與圖C的y軸對齊。注意,沒有圖B,這應該保持空虛。對齊垂直和水平繪圖R ggplot2

數據:

a <- data.frame(
    id = 1:15, 
    GO = c(
    "phosphoglycerate kinase", "phosphoglycerate kinase", 
    "phosphoglycerate kinase", "phosphoglycerate kinase", "phosphoglycerate kinase", 
    "phenylalanine-tRNA ligase", "phenylalanine-tRNA ligase", "phenylalanine-tRNA ligase", 
    "phenylalanine-tRNA ligase", "phenylalanine-tRNA ligase", "allantoicase", 
    "allantoicase", "allantoicase", "allantoicase", "allantoicase"), 
    variable = c(
    "d64", "d31", "d16", "d9", "d0", "d64", "d31", "d16", "d9", "d0", "d64", "d31", "d16", "d9", "d0"), 
    value = c(
    154.28239, 226.04355, 245.67728, 271.82375, 270.83519, 289.01809, 491.66461, 
    485.28291, 351.3759, 510.96043, 22.75253, 31.66546, 129.50564, 206.6651, 32.43769), 
    relAbundByGO = c(
    13.201624, 19.342078, 21.022096, 23.259395, 23.174806, 13.57975, 23.101262, 
    22.801413, 16.509683, 24.007892, 5.378513, 7.485456, 30.614078, 48.853948, 7.668005), 
    GOd = c(
    "phosphoglycerate kinase", "phosphoglycerate kinase", 
    "phosphoglycerate kinase", "phosphoglycerate kinase", "phosphoglycerate kinase", 
    "phenylalanine-tRNA ligase", "phenylalanine-tRNA ligase", "phenylalanine-tRNA ligase", 
    "phenylalanine-tRNA ligase", "phenylalanine-tRNA ligase", "allantoicase", 
    "allantoicase", "allantoicase", "allantoicase", "allantoicase" 
)) 

b <- data.frame(
    id = 1:15, 
    Compound = c(
    "C5-C10", "C5-C10", "C5-C10", 
    "C5-C10", "C5-C10", "C10-C20", "C10-C20", "C10-C20", "C10-C20", 
    "C10-C20", "BTEX", "BTEX", "BTEX", "BTEX", "BTEX"), 
    Degradation = c(
    100, 100, 23.5, 5.6, 0, 100, 100, 67.2, 19, 0.6, 100, 100, 88.7, 43.3, 0.1), 
    st()dev = c(
    0, 0, 35, 12.4, 0, 0, 0, 19.3, 13.1, 0.6, 0, 0, 33.4, 43.4, 0.2), 
    day = c(
    "NSWOD-0", "NSWOD-64", "NSOD-9", 
    "NSOD-16", "NSOD-31", "NSWOD-0", "NSWOD-64", "NSOD-9", "NSOD-16", 
    "NSOD-31", "NSWOD-0", "NSWOD-64", "NSOD-9", "NSOD-16", "NSOD-31")) 

這是cowplot一試:

a$GO <- factor(a$GO, levels = a$GO) #keep the same order as in table 
a$variable <- factor(a$variable, levels = c("d0", "d64", "d9", "d16", "d31")) 
p1 <- ggplot(data = a, aes(x=variable, y=GO)) + 
    geom_tile(aes(fill=relAbundByGO), colour = "white") + ylab("Gene ontology") + 
    scale_fill_gradient(name="Relative\nabundance of TPM", low = "green", high = "red", limits=c(0, 100), na.value="transparent") + 
    scale_x_discrete("Sample", 
        breaks = c("d0", "d64", "d9", "d16", "d31"), 
        labels = c("CTRL-0", "CTRL-64", "CEWAF-9","CEWAF-16","CEWAF-31")) + 
    theme(legend.position="left") 
p1 

a$GO <- factor(a$GO, levels = a$GO) #keep the same order as in table 
p2 <- ggplot(data = a, aes(x=GO, y=value)) + 
    geom_bar(stat = "identity") + 
    theme(axis.text.x = element_text(angle = 0)) + 
    ylab("Cumulative TPM abundance") + 
    theme(axis.text.y=element_blank(), 
     axis.title.y=element_blank()) + 
    coord_flip() 
p2 

b$day <- factor(b$day, levels = b$day) #keep the same order as in table 
p3 <- ggplot(b, aes(x=day, y=Degradation, color=Compound, group=Compound)) + 
    geom_point(size=4, shape=21, fill="white") + 
    geom_line(size=0.7) + 
    ylab("Hydrocarbon content (%)") + 
    geom_errorbar(aes(ymax=Degradation + stdev, ymin=Degradation-stdev), linetype="dashed", lwd=.6, width=.4) + 
    theme_bw(base_size = 12, base_family = "Helvetica") + 
    theme(axis.text.x=element_blank() 
     ,axis.ticks=element_blank() 
     ,axis.title.x=element_blank(), 
     legend.position="left") + 
    scale_color_discrete(name="Hydrocarbon\ngroup", 
         breaks=c("C5-C10", "C10-C20", "BTEX", "PAHs")) 
p3 

legend_p1 <- get_legend(p1) 
legend_p3 <- get_legend(p3) 

p1 <- p1 + theme(legend.position='none') 
p3 <- p3 + theme(legend.position='none') 

cowplot::plot_grid(
    cowplot::plot_grid(legend_p3, legend_p1, ncol = 1), 
    cowplot::plot_grid(p3, NULL, p1, p2, ncol = 2, nrow = 2, rel_widths = c(1, 0.75, 1, 0.75), labels = c('A', '', 'C', 'D'), align = "hv"), 
    rel_widths = c(0.16, 1)) 

enter image description here

我想有情節d非常接近曲線C

傑尼

+0

您是否使用了A和C的x軸相同的價值觀?看到這個例子,它很好地被alligned:'library(ggplot2);庫(cowplot); a < - ggplot(cars,aes(x = speed))+ geom_bar(); c < - ggplot(cars,aes(x = speed,y = dist))+ geom_point(); d < - ggplot(cars,aes(x = dist,y = speed))+ geom_point(); plot_grid(a,NULL,c,d,ncol = 2,nrow = 2,rel_widths = c(10/16,6/16,10/16,6/16),labels = c('A','', 'C','D'),align =「hv」)' –

+3

_「希望有人能夠嘗試重現某些東西」,嗯,我認爲這是對你的一個可重複的例子。你是否收到關於對齊的消息或警告? – Axeman

+0

@ m-dz,無論X軸值如何,繪圖區域將會/應該對齊。傳說可能會提供問題。 – Axeman

回答

1

由於@Axeman提到的,它是由傳說引起的,cowplot::get_legend()可以解決這個問題(見?cowplot::get_legend()爲您確切的情況下):

legend_p1 <- get_legend(p1) 
legend_p3 <- get_legend(p3) 

p1 <- p1 + theme(legend.position='none') 
p3 <- p3 + theme(legend.position='none') 

cowplot::plot_grid(
    cowplot::plot_grid(legend_p1, legend_p3, ncol = 1), 
    cowplot::plot_grid(p3, NULL, p1, p2, ncol = 2, nrow = 2, rel_widths = c(1, 0.75, 1, 0.75), labels = c('A', '', 'C', 'D'), align = "hv"), 
    rel_widths = c(0.1, 1)) 

但它需要相當多的工作,使其「可讀性」。

數據( 「原始」,適用於從OP postt全部轉換):

a <- data.frame(
    id = 1:15, 
    GO = c(
    "phosphoglycerate kinase", "phosphoglycerate kinase", 
    "phosphoglycerate kinase", "phosphoglycerate kinase", "phosphoglycerate kinase", 
    "phenylalanine-tRNA ligase", "phenylalanine-tRNA ligase", "phenylalanine-tRNA ligase", 
    "phenylalanine-tRNA ligase", "phenylalanine-tRNA ligase", "allantoicase", 
    "allantoicase", "allantoicase", "allantoicase", "allantoicase"), 
    variable = c(
    "d64", "d31", "d16", "d9", "d0", "d64", "d31", "d16", "d9", "d0", "d64", "d31", "d16", "d9", "d0"), 
    value = c(
    154.28239, 226.04355, 245.67728, 271.82375, 270.83519, 289.01809, 491.66461, 
    485.28291, 351.3759, 510.96043, 22.75253, 31.66546, 129.50564, 206.6651, 32.43769), 
    relAbundByGO = c(
    13.201624, 19.342078, 21.022096, 23.259395, 23.174806, 13.57975, 23.101262, 
    22.801413, 16.509683, 24.007892, 5.378513, 7.485456, 30.614078, 48.853948, 7.668005), 
    GOd = c(
    "phosphoglycerate kinase", "phosphoglycerate kinase", 
    "phosphoglycerate kinase", "phosphoglycerate kinase", "phosphoglycerate kinase", 
    "phenylalanine-tRNA ligase", "phenylalanine-tRNA ligase", "phenylalanine-tRNA ligase", 
    "phenylalanine-tRNA ligase", "phenylalanine-tRNA ligase", "allantoicase", 
    "allantoicase", "allantoicase", "allantoicase", "allantoicase" 
)) 
b <- data.frame(
    id = 1:15, 
    Compound = c(
    "C5-C10", "C5-C10", "C5-C10", 
    "C5-C10", "C5-C10", "C10-C20", "C10-C20", "C10-C20", "C10-C20", 
    "C10-C20", "BTEX", "BTEX", "BTEX", "BTEX", "BTEX"), 
    Degradation = c(
    100, 100, 23.5, 5.6, 0, 100, 100, 67.2, 19, 0.6, 100, 100, 88.7, 43.3, 0.1), 
    st()dev = c(
    0, 0, 35, 12.4, 0, 0, 0, 19.3, 13.1, 0.6, 0, 0, 33.4, 43.4, 0.2), 
    day = c(
    "NSWOD-0", "NSWOD-64", "NSOD-9", 
    "NSOD-16", "NSOD-31", "NSWOD-0", "NSWOD-64", "NSOD-9", "NSOD-16", 
    "NSOD-31", "NSWOD-0", "NSWOD-64", "NSOD-9", "NSOD-16", "NSOD-31")) 
+0

非常感謝!現在看起來更多的是我真正想要的東西。你能指點我嗎?米分別進行數據轉換? –

+0

沒問題!我可能不確定'各自的數據轉換'是什麼意思,你能解釋一下,也許在上面的問題中增加一個例子嗎? –

+0

我的意思是說,有一種簡潔的方式可以將原始文章中的數據調整爲您爲「可讀」繪圖創建的內容? –