2015-10-16 49 views
1

我們可以在rasterVis::levelplot帶標籤中包含下標/上標字符嗎?rasterVis :: levelplot strip labels中的下標/上標字符

考慮以下RasterStacks

library(rasterVis) 
s <- stack(replicate(2, raster(matrix(runif(9), 3)))) 

的默認打印方法Raster*對象允許表情傳遞給main說法:

plot(s, main=expression(Something, Something[2])) 

enter image description here

隨着rasterVis::levelplot,條帶名稱通過names.attr的論點,但似乎這些被強制爲character,然後最終被傳遞到lattice::levelplotstrip.custom(factor.levels = names.attr)

結果是:

levelplot(s, names.attr=expression(Something, Something[2])) 

enter image description here

短修改源的,有沒有使用表達式(或以其他方式實現下標/上標字符)中的rasterVis::levelplot帶標籤的方法嗎?

+1

FPR文檔rasterVis :: contourplot不建議htat筆者預期的p21蛋白表達對象。我說'names.attr'參數的「字符」。事實證明這是一種翻轉的S4方法,因此需要大量的挖掘才能找出實際發生的事情。 –

回答

1

您可能會認爲rasterVis::levelplot使用的代碼是lattice::levelplot,事實證明情況是這樣,但是首先發生了一堆數據轉換。通過使用x =「RasterBrick」的簽名無法看到S4方法,而是需要x =「Raster」。

showMethods("levelplot", classes="RasterStack", includeDefs=TRUE) 

getMethod("levelplot", signature=c(x="Raster", data="missing")) 

這表明,構成條的代碼被定義爲:

.... 
      strip = strip.custom(factor.levels = names.attr), 
    .... 

我也能猜到factor.levels本來將表達式傳遞到正確的參數。有一個導致失敗的強制步驟。所以你需要破解代碼以允許表達式通過。如果我註釋掉與as.character脅迫:

 else { 
     # names.attr <- as.character(names.attr) 
     if (length(names.attr) != nlayers(object)) 
      stop("Length of names.attr should match number of layers.") 

使用:

setMethod("levelplot: { 
    function code 
    }, signature= c(x="Raster", data="missing")) 

而且複製從rasterVis 2個未導出的函數來對全球環境我獲得成功:

drawMargin <- rasterVis:::drawMargin 
constructMargin <- rasterVis:::constructMargin 

又通通過names.attr

012對'factor.levels'的非強迫表達

enter image description here

+0

感謝您抽出寶貴時間。我發現了這個方法的來源,以及對角色的強制性,但是希望可以有一個巧妙的方法來繞過它(除了評論它外)。也就是說,你的方法工作正常,所以我感謝你的帖子。也許@OscarPerpiñán可能會重新考慮'as.character';) – jbaums

+1

@jbaums有一個github問題報告:https://github.com/oscarperpinan/rastervis/issues – 2015-10-17 06:38:44

+0

好點,@帕斯卡......我很懶。現在已經發布了一個問題。 – jbaums