gpt4 book ai didi

r - ggplot2 添加跨越小平面边缘的注释

转载 作者:行者123 更新时间:2023-12-03 01:18:38 30 4
gpt4 key购买 nike

我想绘制一个矩形注释,该注释将跨越 ggplot 中的面边界。

library(ggplot2)
myPlot <- ggplot(mpg, aes(displ, hwy)) +
geom_point() +
facet_grid(class ~ .)

# add annotation
myPlot +
annotate("rect", xmin = 3, xmax = 4, ymin = -Inf, ymax = Inf, fill = "green", alpha = 1/5)

到目前为止我所拥有的:

What I have so far

我想绘制 1 个跨越小面边缘的大矩形,如下所示:

Messy paint edit of what I want

有没有办法使用内置的 ggplot2 代码或 ggforce 来做到这一点,或者我是否必须使用网格?我的理想用例仍然允许我将 myPlot 作为 ggplot 对象,这就是为什么我到目前为止一直避免使用任何复杂的网格内容。

最佳答案

一种使用网格函数来编辑绘图的方法。

在网格视口(viewport)中绘制矩形很容易。是否可以构造一个网格视口(viewport)来精确地覆盖 ggplot 面板集?答案是"is"。绘制矩形的技巧是将 ggplot_build 信息中的 x 轴“ native ”坐标获取到网格视口(viewport)中。

library(ggplot2)
library(grid)

plot <- ggplot(mpg, aes(displ, hwy)) +
geom_point() +
facet_grid(class ~ .)

plot

# Construct a grid.layout that is the same as the ggplot layout
gt = ggplotGrob(plot)
lay = grid.layout(nrow = length(gt$heights), ncol = length(gt$widths),
widths = gt$widths, heights = gt$heights)

# Push to this viewport
pushViewport(viewport(layout = lay))

# Within the layout, push to a viewport that spans the plot panels.
pos = gt$layout[grep("panel", gt$layout$name), c("t", "l")] # Positions of the panels
pushViewport(viewport(layout.pos.col = pos$l, layout.pos.row = min(pos$t):max(pos$t)))

# Get the limits of the ggplot's x-scale, including any expansion.
## For ggplot ver 2.2.1
# x.axis.limits = ggplot_build(plot)$layout$panel_ranges[[1]][["x.range"]]

## For ver 3.0.0
# axis.limits = ggplot_build(plot)$layout$panel_params[[1]]$x.range
# But possibly the following will be more immune to future changes to ggplot
x.axis.limits = summarise_layout(ggplot_build(plot))[1, c('xmin', 'xmax')]


# Set up a data viewport,
# so that the x-axis units are, in effect, "native",
# but y-axis units are, in effect, "npc".
# And push to the data viewport.
pushViewport(dataViewport(yscale = c(0, 1),
xscale = x.axis.limits))

# Draw the rectangle
grid.rect(x = 3, y = 0,
width = 1, height = 1,
just = c("left", "bottom"), default.units = "native",
gp = gpar(fill = "green", col = "transparent", alpha = .2))

# Back to the root viewport
upViewport(0)

enter image description here

关于r - ggplot2 添加跨越小平面边缘的注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51088379/

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