ggplot2 - 通过轴线自定义grob [英] ggplot2 - custom grob over axis lines

查看:150
本文介绍了ggplot2 - 通过轴线自定义grob的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在ggplot2中生成一个轴线断点(在轴线上有一个白色段),我遇到了一些麻烦。



使用内容丰富的帖子



我很好奇为什么annotation_custom grobs被放置在轴线之下,以及是否有更好的解决方案来使用ggplot2添加自定义的grobs。似乎有一个图形放置在绘图窗口中的顺序 - 这可能会如何交替,以便将定制grobs放置在轴线之后?

解决方案

你很近。布局数据框是您关闭裁剪。布局数据框中还有另一列,它给出了绘制各种绘图元素的顺序 - z。绘图面板(包括注释)在第二张(在背景之后)绘制,然后绘制坐标轴。

  library(ggplot2)#devtools图形面板的z值大于轴的z值。 :: install_github(hadley / ggplot2)
library(grid)
library(scales)


data(economics_long)
econ < ; - 经济_long
经济$ value01< - (经济$ 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(color =deepskyblue,show.legend = TRUE)+ theme_bw()

theme_white< - theme(panel.background = element_blank(),
panel.border = element_rect(color =transparent),
plot.margin =单位(c(0.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 (gline,ymin = .55,ymax = .575,xmin = -Inf,xmax = Inf)+
annotation_custom(gline,ymin = 5))

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

#grobs位于轴线下方。

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

g $ layout#注意面板的z是1.将其更改为更大的值。
$ b $ $ layout $ z [g $ layout $ name ==panel] = 17

grid.newpage()
grid.draw(g)


I'm trying to generate an axis line break in ggplot2 (with a white segment over the axis lines) and I'm having some trouble.

Using the informative post annotate-ggplot-with-an-extra-tick-and-label I was able to generate the custom grobs in given location, while also turning the panel off to "draw" outside of the plotting area.

I'm also familiar with other packages such as plotrix and am able to replicate broken axis in base, but more than anything I'm interested in learning why the axis grobs I'm creating aren't overwriting the line. Here is some sample code:

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)

Which creates this image:

I'm curious why the annotation_custom grobs are placed under the axis lines and whether there is a better solution to adding custom grobs using ggplot2. There appears to be an order in which graphics are placed in the plotting windows - how might this be alternated so that the custom grobs are placed after the axis lines?

解决方案

You were close. The layout data frame is were you turned off clipping. There is another column in the layout data frame that gives the order in which the various plot elements are drawn - z. The plot panel (including the annotation) is drawn second (after the background), then later the axes are drawn. Change the value of z for the plot panel to something larger than the z values for the axes.

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)

这篇关于ggplot2 - 通过轴线自定义grob的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆