gpt4 book ai didi

r - 对齐多个具有不同间距的图并在它们之间添加箭头

转载 作者:行者123 更新时间:2023-12-04 00:37:54 26 4
gpt4 key购买 nike

我有 6 个图,我想以两步方式整齐地对齐(见图)。最好,我想添加漂亮的箭头。

有任何想法吗?

更新。当我的问题开始收集负面反馈时,我想澄清一下,我已经检查了 SO 上的所有(部分)相关问题,但没有发现有关如何在“ Canvas ”上自由定位 ggplots 的指示。此外,我想不出一种在图之间绘制箭头的方法。我不是要求现成的解决方案。请指路。

enter image description here

最佳答案

这是您想要的布局的尝试。它需要手动进行一些格式化,但您可能可以通过利用绘图布局中内置的坐标系来自动化大部分工作。此外,您可能会发现 grid.curve优于grid.bezier (我用过的)使箭头曲线的形状完全符合您的要求。

我对 grid 的了解就够了很危险,所以我会对任何改进建议感兴趣。无论如何,这里...

加载我们需要的包,创建几个实用程序 grid对象,并创建一个图来布置:

library(ggplot2)
library(gridExtra)

# Empty grob for spacing
#b = rectGrob(gp=gpar(fill="white", col="white"))
b = nullGrob() # per @baptiste's comment, use nullGrob() instead of rectGrob()

# grid.bezier with a few hard-coded settings
mygb = function(x,y) {
grid.bezier(x=x, y=y, gp=gpar(fill="black"),
arrow=arrow(type="closed", length=unit(2,"mm")))
}

# Create a plot to arrange
p = ggplot(mtcars, aes(wt, mpg)) +
geom_point()

创建主要情节安排。使用空的 grob b我们在上面创建的用于间隔图的:
grid.arrange(arrangeGrob(p, b, p, p, heights=c(0.3,0.1,0.3,0.3)),
b,
arrangeGrob(b, p, p, b, p, heights=c(0.07,0.3, 0.3, 0.03, 0.3)),
ncol=3, widths=c(0.45,0.1,0.45))

添加箭头:
# Switch to viewport for first set of arrows
vp = viewport(x = 0.5, y=.75, width=0.09, height=0.4)
pushViewport(vp)

#grid.rect(gp=gpar(fill="black", alpha=0.1)) # Use this to see where your viewport is located on the full graph layout

# Add top set of arrows
mygb(x=c(0,0.8,0.8,1), y=c(1,0.8,0.6,0.6))
mygb(x=c(0,0.6,0.6,1), y=c(1,0.4,0,0))

# Up to "main" viewport (the "full" canvas of the main layout)
popViewport()

# New viewport for lower set of arrows
vp = viewport(x = 0.6, y=0.38, width=0.15, height=0.3, just=c("right","top"))
pushViewport(vp)

#grid.rect(gp=gpar(fill="black", alpha=0.1)) # Use this to see where your viewport is located on the full graph layout

# Add bottom set of arrows
mygb(x=c(1,0.8,0.8,0), y=c(1,0.9,0.9,0.9))
mygb(x=c(1,0.7,0.4,0), y=c(1,0.8,0.4,0.4))

这是结果图:

enter image description here

关于r - 对齐多个具有不同间距的图并在它们之间添加箭头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35631889/

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