gpt4 book ai didi

r - ggplot2 - 在轴线上自定义 grob

转载 作者:行者123 更新时间:2023-12-04 11:44:47 27 4
gpt4 key购买 nike

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

使用信息性帖子 annotate-ggplot-with-an-extra-tick-and-label我能够在给定位置生成自定义grob,同时还关闭面板以在绘图区域外“绘制”。

我也熟悉其他软件包,例如 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)

创建此图像:
enter image description here

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

最佳答案

你很接近。布局数据框是您关闭剪辑的。布局数据框中的另一列给出了绘制各种绘图元素的顺序 - 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)

关于r - ggplot2 - 在轴线上自定义 grob,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39733972/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com