2011-05-02 149 views
2

我試圖控制多少個z標籤應該寫入我的等值線圖中,該圖用contourplot()從點陣庫中繪出。
我有30個輪廓線,但我只想要標記前5個。我試了一堆東西像控制輪廓圖中的z標籤

contourplot(z ~ z+y, data=d3, cuts=30, font=3, xlab="x axis", ylab="y axis", scales=list(at=seq(2,10,by=2))) 
contourplot(z ~ z+y, data=d3, cuts=30, font=3, xlab="x axis", ylab="y axis", at=seq(2,10,by=2)) 

但沒有任何工作。

另外,是否可以在同一個圖上繪製兩個contourplot()?我試過

contourplot(z ~ z+y, data=d3, cuts=30) 
par(new=T) 
contourplot(z ~ z+y, data=d3, cuts=20) 

但它不工作。
謝謝!

+0

'par(new = TRUE)'僅適用於基礎圖形。格是一個完全不同的圖形引擎。 – Marek 2011-05-02 16:16:24

回答

3

可以指定labels爲特徵向量參數,並與代表最後設置的值(「」,5),所以也許這個例子中你在前面一個問題提供約輪廓

x = seq(0, 10, by = 0.5) 
y = seq(0, 10, by = 0.5) 
z <- outer(x, y) 
d3 <- expand.grid(x=x,y=y); d3$z <- as.vector(z) 
contourplot(z~x+y, data=d3) 
# labeled '5'-'90' 
contourplot(z~x+y, data=d3, 
    at=seq(5,90, by=5), 
    labels=c(seq(5,25, by=5),rep("", 16)), 
    main="Labels only at the first 5 contour lines") 
# contourplot seems to ignore 'extra' labels 
# c() will coerce the 'numeric' elements to 'character' if any others are 'character' 
?contourplot # and follow the link in the info about labels to ?panel.levelplot 

The second plot:

5

這是我的看法:

library(lattice) 
x <- rep(seq(-1.5,1.5,length=50),50) 
y <- rep(seq(-1.5,1.5,length=50),rep(50,50)) 
z <- exp(-(x^2+y^2+x*y)) 

# here is default plot 
lp1 <- contourplot(z~x*y)  

# here is an enhanced one 
my.panel <- function(at, labels, ...) { 
    # draw odd and even contour lines with or without labels 
    panel.contourplot(..., at=at[seq(1, length(at), 2)], col="blue", lty=2) 
    panel.contourplot(..., at=at[seq(2, length(at), 2)], col="red", 
         labels=as.character(at[seq(2, length(at), 2)])) 
} 

lp2 <- contourplot(z~x*y, panel=my.panel, at=seq(0.2, 0.8, by=0.2)) 
lp3 <- update(lp2, at=seq(0.2,0.8,by=0.1)) 
lp4 <- update(lp3, lwd=2, label.style="align") 

library(gridExtra) 
grid.arrange(lp1, lp2, lp3, lp4) 

enter image description here

您可以調整自定義功能panel以最好地滿足您的需求(例如,用於調平z軸,顏色等的其他比例)。