2013-07-22 5 views
0

ggplot 그래프를지도의 지정된 위치에 배치하려고합니다. ggplot2 패키지를 선택한 이유는 grid에 대해 더 잘 알고 있기 때문입니다. 누군가가 그러한 일에 grid을 사용하는 방법에 대한 작은 예제를 통해 나를 도울 수 있다면, 나는 그러한 대답에 대해서도 높이 평가할 것이다. 여기 annotation_custom()을 사용하여 지정된 좌표에 여러 개의 그림을 그립니다.

간단한 예이다

# create base plot 
g <- ggplot(data.frame(x=c(-104,-94), y=c(33,38)), aes(x=x, y=y)) + 
    geom_blank() 

# create theme 
tm <- theme(axis.title = element_blank(), 
      axis.text = element_blank(), 
      axis.ticks = element_blank(), 
      axis.line = element_blank(), 
      panel.background = element_blank(), 
      panel.grid = element_blank(), 
      panel.border = element_rect(color="grey", fill=NA), 
      title = element_text(size=5)) 

# create two plot which should be placed on the base plot 
p1 <- ggplot(data.frame(x=c(-104,-94), y=c(33,38)), aes(x=x, y=y)) + 
    geom_point() + tm 
p2 <- ggplot(data.frame(x=c(-100,-98), y=c(34,37)), aes(x=x, y=y)) + 
    geom_point() + tm 

# place them using annotation_custom() function 
a1 <- annotation_custom(grob = ggplotGrob(p1), 
         xmin = -104, xmax = -102, 
         ymin = 33, ymax = 35) 
a2 <- annotation_custom(grob = ggplotGrob(p2), 
         xmin = -100, xmax = -98, 
         ymin = 35, ymax = 37) 

# draw 
g + a1 
g + a2 
g + a1 + a2 

enter image description here enter image description here

그러나 g + a1 + a2의 경우 I 삽입 첫 번째 줄거리와 p1 제 사진을 얻었다. 무엇이 잘못 되었나요? annotation_custom()을 사용하여 두 개 이상의 플롯을 그리는 방법은 무엇입니까?

+1

해결 방법은 grobs를 'gTree'로 감싸는 것입니다. 'g1 = grobTree (ggplotGrob (p1)); g2 = grobTree (ggplotGrob (p2))'를 호출하고'annotation_custom'에 grobs를 사용합니다. 왜 나 한테 묻지 마! – baptiste

+0

이 문제에 대한 설명은 https://github.com/hadley/ggplot2/issues/817을 참조하십시오. ggplot2 dev는 잠시 멈추었 기 때문에 그 문제에 대한 빠른 대응이 아닐 수도 있습니다. – baptiste

답변

0

최근에 내가 알아 차 렸던 이상한 버그입니다. 유사한 고려 ggplot2 일부 grobs를 들어, 위치는 무시 될 수 있으며이 중첩 결국 :

myGrob <- rectGrob(gp=gpar(fill="red", alpha=0.5), name="whatever") 
myGrob2 <- rectGrob(gp=gpar(fill="blue", alpha=0.5)) 

# this is fine 
qplot(1:10, 1:10) + theme(plot.margin=unit(c(0, 3, 0, 0), "cm")) + 
    annotation_custom(myGrob, xmin=5, xmax=6, ymin=3.5, ymax=5.5) + 
    annotation_custom(myGrob2, xmin=8, xmax=12, ymin=3.5, ymax=5.5) 

# using twice the same grob, they just sit on top of each other 
p <- qplot(1:10, 1:10) + theme(plot.margin=unit(c(0, 3, 0, 0), "cm")) + 
    annotation_custom(myGrob, xmin=5, xmax=6, ymin=3.5, ymax=5.5) + 
    annotation_custom(myGrob, xmin=8, xmax=12, ymin=3.5, ymax=5.5) 

g <- ggplotGrob(p) 
grid.ls(g$grobs[[4]]) # two of them 

나는이 원인을 무엇 아무 생각이 없지만, 아마도이 문제에 관련이.

+0

설명과 제안 된 해결 방법을 보내 주셔서 감사합니다! – DrDom

+0

안녕하세요. # 817과는 별개로 보입니다. 같은 기본 버그 일 가능성이 있습니다. 어쨌든, 당신은 그것을 별도의 문제로보고하는 것이 좋습니다. –

관련 문제