2016-06-16 12 views
1

levelplot에 의해 생성 된 색상 키에 sp.points 레이어의 키를 추가 할 수 있습니까?levelplot colorkey에 sp.points 키 추가

은 다음 예를 보자

library(rasterVis) 
library(latticeExtra) 
library(sp) 

r <- as.factor(raster(matrix(rbinom(100, 1, 0.5), 10))) 
levels(r)[[1]] <- data.frame(ID=0:1, z=c('a', 'b')) 
p <- SpatialPoints(matrix(runif(20), 10)) 

levelplot(r, margin=list(draw=FALSE), scales=list(draw=FALSE), 
      col.regions=c('white', 'gray90')) + 
    latticeExtra::layer(sp.points(p, pch=20, col=1)) 

enter image description here

나는 기존의 컬러 키 아래에있는 점에 대한 주요 항목을 추가하고 싶습니다.

미봉책 용액은 다음과 같이 원하는 위치에있어까지 xy 값을 조정은 levelplot 통화 키를 추가하지만, (1) 우측 xy 값을 찾는 것은 상호 작용을 필요로하는 통증, (2) 오른쪽 패딩이 키를 수용하기 위해 크기가 조정되지 않으며, (3) 글꼴 크기가 색상 키와 일치하도록 자동으로 조정되지 않습니다. 내가 lattice 그래픽을 고수 할 필요가 가정

k <- list(x = 1.02, y = 0.4, corner = c(0, 0), points=list(pch=20, col=1), 
      text=list('foo', cex=0.9)) 

levelplot(r, margin=list(draw=FALSE), scales=list(draw=FALSE), 
      col.regions=c('white', 'gray90'), key=k) + 
    latticeExtra::layer(sp.points(p, pch=20, col=1)) 

enter image description here

, 내가 위에 나열된 문제를 해결하는 가장 좋은 방법은 무엇인가? 당신이 제기하는 모든 문제를 해결하지는 않지만

답변

1

, 어쩌면 latticeExtra::mergedTrellisLegendGrob 기능을 사용하면 유용합니다 :

p1 <- levelplot(r, scales=list(draw=FALSE), 
       col.regions=c('white', 'gray90')) 

myPoints <- SpatialPoints(matrix(runif(20), 10)) 
p2 <- spplot(myPoints, pch=20) 

## Merge graphics 
p <- p1 + p2 

## Merge legends 
l1 <- p1$legend$right 
l2 <- p2$legend$bottom 
ll <- mergedTrellisLegendGrob(l1, l2) 
p$legend$right$fun <- ll 

p 

mergedLegends

+0

감사 오스카. 'p2' 범례를 레벨 플롯 키와 나란히 놓을 수 있습니까 (즉, 위 또는 아래 대신에)? – jbaums

+1

'mergedTrellisLegendGrob'에서'vertical' 인자를 시도해야합니다. 기본값은 (이 예제에서와 같이)'FALSE '입니다. 코드를 살펴보면'vertical = TRUE'로이 함수는'packGrob'를 사용하여 첫 번째 범례를 맨 위에, 두 번째 범례를 맨 아래에 놓습니다. 어쩌면 이것은 정확히 당신이 필요로하는 것이 아닙니다. 대신에'packGrob'의'row'와'col' 인수를 사용하여이 함수의 마지막 라인을 재사용 할 수 있습니다. –