2012-02-27 23 views
5

我有一個多變量時間序列是一些變量有相當大的範圍。我希望做一個單頁面圖,每個變量有多個堆積圖,其中一些變量具有log10的y軸縮放比例。我對於晶格相對來說比較陌生,一直未能弄清楚如何有效地將log10縮放比例與非轉換軸混合並獲得出版質量圖。如果使用print.trellis,則繪圖不會對齊,並且填充需要一些工作,如果使用c.trellis,則佈局很好,但僅使用僅來自一個繪圖的y縮放。任何關於高效解決方案的建議,我可以使用每個(原始)對象的不同y縮放來複制c.trellis的輸出?下面 例子:lattice或latticeExtra結合多個陰謀不同的yscaling(log10和非轉換)

require(lattice) 
    require(latticeExtra) 

    # make data.frame 
    d.date <- as.POSIXct(c("2009-12-15", "2010-01-15", "2010-02-15", "2010-03-15", "2010-04-15"))     
    CO2dat <- c(100,200,1000,9000,2000) 
    pHdat <- c(10,9,7,6,7) 
    tmp <- data.frame(date=d.date ,CO2dat=CO2dat ,pHdat=pHdat) 

    # make plots 
    plot1 <- xyplot(pHdat ~ date, data=tmp 
    , ylim=c(5,11) 
    , ylab="pHdat" 
    , xlab="Date" 
    , origin = 0, border = 0 
    , scales=list(y=list(alternating=1)) 
    , panel = function(...){ 
    panel.xyarea(...) 
    panel.xyplot(...) 
    } 
    ) 

    # make plot with log y scale 
    plot2 <- xyplot(CO2dat ~ date, data=tmp 
      , ylim=c(10,10^4) 
      , ylab="CO2dat" 
      , xlab="Date" 
      , origin = 0, border = 0 
      , scales=list(y=list(alternating=1,log=10)) 
           , yscale.components = yscale.components.log10ticks 
      , panel = function(...){ 
       panel.xyarea(...) 
       panel.xyplot(...) 
       # plot CO2air uatm 
       panel.abline(h=log10(390),col="blue",type="l",...) 
      } 
      ) 

     # plot individual figures using split 
     print(plot2, split=c(1,1,1,2), more=TRUE) 
     print(plot1, split=c(1,2,1,2), more=F) 

     # combine plots (more convenient) 
     comb <- c(plot1, plot2, x.same=F, y.same=F, layout = c(1, 2)) 

     # plot combined figure 
     update(comb, ylab = c("pHdat","log10 CO2dat")) 
+0

您是否嘗試過與領先的空白(爲'print.trellis'法)填充在plot1 y軸刻度標籤,或者你在找比這更少的臨時解決方案? – joran 2012-02-27 18:58:27

+0

嗨@joran:看看我下面的想法。 – Aaron 2012-02-28 21:29:58

回答

1

使用@ joran的想法,我可以得到軸更接近,但不準確;同樣,減少填充使它們更接近,但改變了寬高比。在下面的圖片中,我已經減少了填充,可能太多以至於顯示不準確;如果希望收尾,那麼您顯然也希望刪除頂部的x軸標籤。

我查看了設置佈局的代碼,並且左側的邊距是根據標籤的寬度計算出來的,因此@ joran的想法可能是基於使用split進行打印的唯一工具,除非一個是重寫plot.trellis命令。也許c方法可以工作,但我還沒有找到一種方法來根據面板分別設置比例組件。儘管如此,這似乎更有希望。

mtheme <- standard.theme("pdf") 
mtheme$layout.heights$bottom.padding <- -10 
plot1b <- update(plot1, scales=list(y=list(alternating=1, at=5:10, labels=paste(" ",c(5:10))))) 
plot2b <- update(plot2, par.settings=mtheme) 
pdf(file="temp.pdf") 
print(plot2b, split=c(1,1,1,2), more=TRUE) 
print(plot1b, split=c(1,2,1,2), more=F) 

enter image description here

+0

我想如果你只有一個空白空間,它會非常接近,或者可能完全對齊。 – joran 2012-02-28 21:33:43

+0

@joran和@Aaron你的解決方案基本上適用於我。爲了澄清這是我關心的y軸的對齊,但@Aarons示例很好地指出了縱橫比的變化(我之前沒有注意到)。我已經將這個應用於我的真實數據,按照@joran的建議調整(眼睛)'trellis.par.set(layout.widths = list(ylab.axis.padding = VALUE))'。我發現編寫規則很困難,因爲y軸標籤的寬度不是常量(對於真實的數據標籤更爲複雜),但是我會看看設置左邊距佈局的代碼。 – 2012-02-29 12:44:34

+0

在將圖與「c」組合後,是否可以簡單地更換y軸標籤? – 2012-02-29 13:07:58