2016-09-27 83 views
2

我試圖產生GGPLOT2軸線換行符(用白色段在軸線的線),我有一些麻煩定製GROB。GGPLOT2 - 在軸線

使用信息帖子annotate-ggplot-with-an-extra-tick-and-label我能夠在給定的位置生成自定義的grobs,同時關閉面板以在繪圖區域外「繪製」。

我對其他軟件包如plotrix也很熟悉,能夠在基礎上覆制斷開的軸,但是我更感興趣的是要了解爲什麼我創建的軸連線沒有覆蓋線。下面是一些示例代碼:

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) 

它創建這一形象:爲什麼annotation_custom grobs放置在軸線之下,是否有更好的解決方案,以添加自定義grobs使用 enter image description here

我很好奇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)