2017-08-02 33 views
1

似乎自ggplot2.2.2 update以來沒有涵蓋此主題,其中this onethis one等舊解決方案不再適用。幸運的是,這個過程比以前簡單得多。一行代碼,並且您有第二個Y軸(as shown here)。添加具有小平面的輔助X軸

但我不能讓我的曲線的二次X軸...

我比較沿沉積物芯金屬濃度的深度輪廓。我想在金屬濃度後顯示碳和磷酸鹽濃度爲geom_area。問題是碳和磷酸鹽的濃度都不和金屬相同。因此我需要第二個軸。

的主題爲:(從this website拍攝):

theme_new <- theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), axis.line = element_line(colour = "black"), strip.text.x = element_text(size=10, angle=0, vjust=0), strip.background = element_blank(), strip.text.y = element_text(angle = 0), legend.position="none",panel.border = element_blank(), axis.text.x=element_text(angle=45,hjust=1)) # Axis tick label angle 

而這個代碼給了我,即使我指定它在X軸的第二Y軸。

ggplot(MasterTable)+ 
    geom_line(aes(Depth,Conc.nM))+ 
    geom_area(aes(Depth,Conc.uM, fill=Variable))+ 
    scale_x_continuous("Depth (cm)", sec.axis = sec_axis(~ . *100, name = "Carbon & Phosphate"))+ 
    scale_y_continuous("Metal concentration (nM)")+ 
    coord_flip()+ 
    theme_new+ 
    theme(legend.position = "right")+ 
    facet_grid(. ~ Assay, scales = "free") 

ggplot2 figure

誰能幫我把中間軸線上圖的頂部?

謝謝!

dput我MasterTable的是以下幾點:

結構(列表(深度= C(15L,5L,2L,-1L,-3L,-5L,-7L,-9L, -11L, - 13L,-15L,-17L,-19L,-21L,-23L,-25L,-27L,-29L,-31L, 15L,5L,2L,-1L,-3L,-5L,-7L,-9L ,-11L,-13L,-15L,-17L, -19L,-21L,-23L,-25L,-27L,-29L,-31L),Conc.nM = c(24L,24L, 24L,100L ,100L,75L,75L,85L,85L,120L,300L,1000L,200L, 240L,240L,800L,1100L,1500L,2300L,0L,10L,0L,50L,200L, 200L,50L,50L, ,15L,0L,0L,10L,120L,200L,1500L,2100L, 2000L,2000L),測定=結構(c(1L,1L,1 L,1L,1L,1L,1L, 1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,2L,2L,2L,2L, 2L,2L, 2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L),標籤= c(「儀器1」, 「儀器2」 0L,0L,1L, 4L,10L,10L,10L,5L,7L,10L,14L,14L,14L,14L,13L,12L, 12L,12L,1L,1L,1L 4L,9L,9L,11L,11L,8L,8L,8L,20L, , 2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L, 2L,1L,1L,1L,1L,1L,1L,1L,1L, 1L,1L,1L,1L,1L,1L,1L, 1L,1L,1L,1L),標籤= c(「碳」,「磷酸鹽」),等級=「因子」)),。名稱= c 「, 」Conc.nM「,」Assay「,」Conc.uM「,」Variab樂 「)的class = 」data.frame「,row.names = C(NA, -38L))

回答

1

感謝Brian的回答,並修改上面提出的主題,我得到了如下圖所示。

Sediment core

正如他所說,你必須先手動修改數據使用這樣的事情:

MasterTable$Conc.uM <- MasterTable$Conc.uM *100 

然後,在代碼中,以相同的修正係數爲一個調整軸以上使用。這是製作這個數字的代碼。

ggplot(MasterTable)+ 
    geom_line(aes(Depth,Conc.nM))+ 
    geom_area(aes(Depth,Conc.uM, fill=Variable), alpha=0.6)+       #Area for second X-axis 
    geom_area(aes(Depth,Conc.nM), alpha=0.95)+          
    geom_point(aes(Depth,Conc.uM), size=1, shape=16, alpha=0.3)+      #Adding points for second X-axis 
    geom_point(aes(Depth,Conc.nM), size=1, shape=16, alpha=0.8)+ 
    scale_fill_manual(values=colours) + scale_colour_manual(values=colours) +  

    labs(title="Sediment core", color="",fill="") +         #Place legend title in both color="" and fill="" 

    scale_y_continuous("Metal concentration (nM)", 
       sec.axis = sec_axis(~ . /100, name = "[Pi] (uM)            DOC (mg/L)"))+  
    scale_x_continuous("Depth (cm)", breaks=pretty_breaks(n=7))+ 

    coord_flip()+                  #Required to make a proper depth profile 
    theme_new+                   #Reference to custom theme 
    facet_grid(. ~ Assay, scales = "free")            #Scales makes that the axis size can change 

現在我只剩下一個問題需要解決。我希望刻度線和標籤在這個方面。看起來更符合邏輯,並且不像在圖的頂部那樣繁忙。

+0

'+ theme(strip.placement =「outside」)' – Brian

+0

完美,謝謝 – Marty999

0

從代碼:

... 
    scale_x_continuous("Depth (cm)", sec.axis = sec_axis(~ . *100, name = "Carbon & Phosphate"))+ 
    scale_y_continuous("Metal concentration (nM)") + 
    coord_flip() ... 

考慮你想要的主軸線」 碳&磷酸鹽「與...平行。還要考慮在使用coord_flip的情況下「x軸」和「y軸」的含義。

TL; DR:將您的輔助軸移動到scale_y_continuous

+0

喜布賴恩,感謝您對本。良好的捕獲,它現在顯示在頂部的X軸。但數據並沒有遵循這個軸。它仍然與底部的x軸相關聯。有沒有辦法指定我希望Conc.uM數據遵循sec_axis? – Marty999

+0

數據是數據,它們不「跟隨」特定軸。它們應該被預先轉換成一個普通的規模(例如乘以1000來將你的微摩爾放入納摩爾中)。那麼你的第二個軸簡單*重新標記*它們。如果沒有至少一部分數據,無法做更多的工作。 – Brian

+0

是的,我也是這麼看的。這是我的數據文件:https://www.dropbox.com/s/9t1ppyfvm983try/LAS2016_Master-table8.csv?dl=0 – Marty999