2016-09-27 4 views
2

축선 위에 흰색 세그먼트가있는 ggplot2에서 축선 나누기를 생성하려고하는데 몇 가지 문제가 있습니다.ggplot2 - 축선 위의 맞춤 grob

유익한 게시물 annotate-ggplot-with-an-extra-tick-and-label을 사용하여 주어진 위치에서 사용자 정의 grobs를 생성 할 수 있었고, 패널을 플롯 영역 밖에서 "그리기"로 해제 할 수있었습니다.

나는 또한 plotrix과 같은 다른 패키지에 익숙하며 기초에서 깨진 축을 복제 할 수 있지만 무엇보다 제가 작성중인 축 grobs가 선을 덮어 쓰지 않는 이유를 배우는 데 관심이 있습니다. 여기에 몇 가지 예제 코드는 다음 annotation_custom의 grobs가 축 라인 아래에 배치하고 사용 grobs 사용자를 추가하는 더 나은 솔루션이 있는지 왜 enter image description here

궁금 해요 :이 이미지를 생성

library(ggplot2) # devtools::install_github("hadley/ggplot2") 
library(grid) 
library(scales) 


data("economics_long") 
econ <- economics_long 
econ$value01 <- (econ$value01/2) 
x <- ggplot(econ, aes(date, value01,group=1)) + scale_y_continuous(labels=c(0.0,0.1,0.2,0.3,0.4,0.5,1.0), breaks=c(0.0,0.1,0.2,0.3,0.4,0.5,0.6),limits = c(0,.6),expand = c(0, 0)) + 
    geom_smooth(colour="deepskyblue", show.legend = TRUE) + theme_bw() 

theme_white <- theme(panel.background=element_blank(), 
        panel.border=element_rect(color="white"), 
        plot.margin = unit(c(.2, 0, .2, .2), "cm"), 
        panel.grid.major.y=element_blank(), 
        panel.grid.major.x=element_blank(), 
        panel.grid.minor.x=element_blank(), 
        panel.grid.minor.y=element_blank(), 
        axis.title.y = element_blank(), 
        axis.line.x=element_line(color="gray", size=1), 
        axis.line.y=element_line(color="gray", size=1), 
        axis.text.x=element_text(size=12), 
        axis.text.y=element_text(size=12), 
        axis.ticks=element_line(color="gray", size=1), 
        legend.position="none" 
) 
x <- x + theme_white 


gline = linesGrob(y = c(0, 1.5),x = c(-.015, .015), gp = gpar(col = "black", lwd = 2.5)) 
gline2 = linesGrob(y = c(-0.25, 0.5),x = c(0, 0), gp = gpar(col = "red", lwd = 5)) 

p = x + annotation_custom(gline, ymin=.55, ymax=.575, xmin=-Inf, xmax=Inf) + 
    annotation_custom(gline, ymin=.525, ymax=.55, xmin=-Inf, xmax=Inf) + 
    annotation_custom(gline2, ymin=.55, ymax=.575, xmin=-Inf, xmax=Inf) 

# grobs are placed under the axis lines.... 

g = ggplotGrob(p) 
g$layout$clip[g$layout$name=="panel"] <- "off" 
grid.draw(g) 

ggplot2. 그래픽이 플로팅 윈도우에 배치되는 순서가있는 것처럼 보입니다 - 어떻게 사용자 정의 grobs가 축 선 다음에 배치 될 수 있습니까?

답변

1

가까운 사이. 레이아웃 데이터 프레임은 클리핑을 해제 한 것입니다. 레이아웃 데이터 프레임에는 다양한 플롯 요소가 그려지는 순서를 나타내는 또 다른 열이 있습니다 (예 : z). 플롯 패널 (주석 포함)이 두 번째 (배경 뒤)에 그려지면 나중에 축이 그려집니다. 플롯 패널의 z 값을 축의 z 값보다 큰 값으로 변경하십시오.

library(ggplot2) # devtools::install_github("hadley/ggplot2") 
library(grid) 
library(scales) 


data("economics_long") 
econ <- economics_long 
econ$value01 <- (econ$value01/2) 


x <- ggplot(econ, aes(date, value01,group=1)) + scale_y_continuous(labels=c(0.0,0.1,0.2,0.3,0.4,0.5,1.0), breaks=c(0.0,0.1,0.2,0.3,0.4,0.5,0.6),limits = c(0,.6),expand = c(0, 0)) + 
    geom_smooth(colour="deepskyblue", show.legend = TRUE) + theme_bw() 

theme_white <- theme(panel.background=element_blank(), 
        panel.border=element_rect(color="transparent"), 
        plot.margin = unit(c(.2, 0, .2, .2), "cm"), 
        panel.grid.major.y=element_blank(), 
        panel.grid.major.x=element_blank(), 
        panel.grid.minor.x=element_blank(), 
        panel.grid.minor.y=element_blank(), 
        axis.title.y = element_blank(), 
        axis.line.x=element_line(color="gray", size=1), 
        axis.line.y=element_line(color="gray", size=1), 
        axis.text.x=element_text(size=12), 
        axis.text.y=element_text(size=12), 
        axis.ticks=element_line(color="gray", size=1), 
        legend.position="none" 
) 
x <- x + theme_white 


gline = linesGrob(y = c(0, 1.5),x = c(-.015, .015), gp = gpar(col = "black", lwd = 2.5)) 
gline2 = linesGrob(y = c(-0.25, 0.5),x = c(0, 0), gp = gpar(col = "red", lwd = 5)) 

p = x + annotation_custom(gline, ymin=.55, ymax=.575, xmin=-Inf, xmax=Inf) + 
    annotation_custom(gline, ymin=.525, ymax=.55, xmin=-Inf, xmax=Inf) + 
    annotation_custom(gline2, ymin=.55, ymax=.575, xmin=-Inf, xmax=Inf) 

# grobs are placed under the axis lines.... 

g = ggplotGrob(p) 
g$layout$clip[g$layout$name=="panel"] <- "off" 

g$layout # Note that z for panel is 1. Change it to something bigger. 

g$layout$z[g$layout$name=="panel"] = 17  

grid.newpage() 
grid.draw(g)