gpt4 book ai didi

r - ggplot 和网格 : Find the relative x and y positions of a point in a ggplot grob

转载 作者:行者123 更新时间:2023-12-01 16:33:44 30 4
gpt4 key购买 nike

我正在组合 ggplot 的多个绘图,使用网格视口(viewport),这是必要的(我相信),因为我想旋转绘图,这在标准 ggplot 中是不可能的,甚至可能是 gridExtra 包。

我想在两个图上画一条线,以使相关性更清晰。但要确切知道线条在哪里,我需要 ggplot 图中某个点的相对位置(grob?)。

我做了以下例子:

require(reshape2)
require(grid)
require(ggplot2)


datamat <- matrix(rnorm(50), ncol=5)
cov_mat <- cov(datamat)
cov_mat[lower.tri(cov_mat)] <- NA

data_df <- melt(datamat)
cov_df <- melt(cov_mat)

plot_1 <- ggplot(data_df, aes(x=as.factor(Var2), y=value)) + geom_boxplot()
plot_2 <- ggplot(cov_df, aes(x=Var1, y=Var2, fill=value)) +
geom_tile() +
scale_fill_gradient(na.value="transparent") +
coord_fixed() +
theme(
legend.position="none",
plot.background = element_rect(fill = "transparent",colour = NA),
panel.grid=element_blank(),
panel.background=element_blank(),
panel.border = element_blank(),
plot.margin = unit(c(0, 0, 0, 0), "npc"),
axis.ticks=element_blank(),
axis.title=element_blank(),
axis.text=element_text(size=unit(0,"npc")),
)

cov_heatmap <- ggplotGrob(plot_2)
boxplot <- ggplotGrob(plot_1)

grid.newpage()

pushViewport(viewport(height=unit(sqrt(2* 0.4 ^2), 'npc'),
width=unit(sqrt(2* 0.4 ^2), 'npc'),
x=unit(0.5, 'npc'),
y=unit(0.63, 'npc'),
angle=-45,
clip="on")
)
grid.draw(cov_heatmap)
upViewport(0)
pushViewport(viewport(height=unit(0.5, 'npc'),
width=unit(1, 'npc'),
x=unit(0.5, 'npc'),
y=unit(0.25, 'npc'),
clip="on")
)
grid.draw(boxplot)

生成一个图 enter image description here

如何找到(比方说)箱线图第一个框的相对 x 和 y 位置?以及三角协方差矩阵的相对 x 和 y 位置。

我知道我必须查看 grob 对象 boxplot,但我不知道如何在那里找到相关数据。

编辑:

我被要求提供一个绘图示例,其中手动添加了线条,如下所示:enter image description here

这些线从底部图上的点到顶部图上的 block 。

最佳答案

这是一个老问题,所以答案可能不再相关,但无论如何......

这并不简单,但可以使用网格编辑工具来完成。人们需要一路收集信息,这使得解决方案变得繁琐。这在很大程度上是一种一次性解决方案。很大程度上取决于两个 ggplots 的具体情况。但也许这里已经足够供某人使用了。关于要划定的界限的信息不足;我将绘制两条红线:一条从第一个箱线图的横线中心到热图左下图 block 的中心;另一条从第一个箱线图的横线中心到热图左下图 block 的中心;一个从第一个箱线图的横线中心到热图中的下一个图 block 。

几点:

  1. 线条将在不同的视口(viewport)中绘制。通常,GROB 是在视口(viewport)内绘制的,但有几种方法可以跨视口(viewport)绘制线条。我将使用 grid 函数 grid.move.to()grid.line.to()
  2. 通过 grobs 的坐标可以找到 grobs 的结构。那是,人们可以提取第一个箱线图,并查看其结构。这结构将给出晶须段的位置,a线段代表横梁,多边形代表盒子。
  3. 类似地,我们可以提取热图,并且结构将给出每个矩形左上角的坐标(即热图中的每个图 block ),以及每个图 block 的宽度和高度长方形。一些简单的算术就可以给出坐标瓷砖的中心。
  4. 但是,矩形的坐标是根据未旋转的视口(viewport)。选择相关矩形时需要小心。


# Draw the plot
require(reshape2)
require(grid)
require(ggplot2)

set.seed(4321)
datamat <- matrix(rnorm(50), ncol=5)
cov_mat <- cov(datamat)
cov_mat[lower.tri(cov_mat)] <- NA

data_df <- melt(datamat)
cov_df <- melt(cov_mat)

plot_1 <- ggplot(data_df, aes(x=as.factor(Var2), y=value)) + geom_boxplot()
plot_2 <- ggplot(cov_df, aes(x=Var1, y=Var2, fill=value)) +
geom_tile() +
scale_fill_gradient(na.value="transparent") +
coord_fixed() +
theme(
legend.position="none",
plot.background = element_rect(fill = "transparent",colour = NA),
panel.grid=element_blank(),
panel.background=element_blank(),
panel.border = element_blank(),
plot.margin = unit(c(0, 0, 0, 0), "npc"),
axis.ticks=element_blank(),
axis.title=element_blank(),
axis.text=element_text(size=unit(0,"npc")))

cov_heatmap <- ggplotGrob(plot_2)
boxplot <- ggplotGrob(plot_1)

grid.newpage()

pushViewport(viewport(height=unit(sqrt(2* 0.4 ^2), 'npc'),
width=unit(sqrt(2* 0.4 ^2), 'npc'),
x=unit(0.5, 'npc'),
y=unit(0.63, 'npc'),
angle=-45,
clip="on",
name = "heatmap"))
grid.draw(cov_heatmap)
upViewport(0)
pushViewport(viewport(height=unit(0.5, 'npc'),
width=unit(1, 'npc'),
x=unit(0.5, 'npc'),
y=unit(0.25, 'npc'),
clip="on",
name = "boxplot"))
grid.draw(boxplot)
upViewport(0)


# So that grid can see all the grobs
grid.force()

# Get the names of the grobs
grid.ls()

相关部分位于与面板有关的部分中。热图 grob 的名称是:

geom_rect.rect.2

构成第一个箱线图的 grobs 的名称是(数字可以不同):

geom_boxplot.gTree.40
       GRID.segments.34
       geom_crossbar.gTree.39
          geom_polygon.polygon.37
          GRID.segments.38

获取热图中矩形的坐标。

names = grid.ls()$name
HMmatch = grep("geom_rect", names, value = TRUE)
hm = grid.get(HMmatch)

str(hm)
hm$x
hm$y
hm$width # heights are equal to the widths
hm$gp$fill

(请注意,just 设置为 "left", "top")热图是一个 5 X 5 的矩形网格,但只有上半部分是彩色的,因此在图中可见。选中的两个矩形的坐标分别为:(0.045,0.227)和(0.227,0.409),每个矩形的宽度和高度均为0.182

获取第一个箱线图中相关点的坐标。

BPmatch = grep("geom_boxplot.gTree", names, value = TRUE)[-1]
box1 = grid.gget(BPmatch[1])
str(box1)

晶须的 x 坐标为 0.115,横梁的 y 坐标为 0.507

现在,在两个视口(viewport)之间绘制线条。这些线条是在面板视口(viewport)中“绘制”的,但热图面板视口(viewport)的名称与箱线图面板视口(viewport)的名称相同。为了克服这个困难,我寻找箱线图视口(viewport),然后向下推到其面板视口(viewport);同样,我寻找热图视口(viewport),然后向下推至其面板视口(viewport)。

## First Line (and points)
seekViewport("boxplot")
downViewport("panel.7-5-7-5")
grid.move.to(x = .115, y = .503, default.units = "native")
grid.points(x = .115, y = .503, default.units = "native",
size = unit(5, "mm"), pch = 16, gp=gpar(col = "red"))


seekViewport("heatmap")
downViewport("panel.7-5-7-5")
grid.line.to(x = 0.045 + .5*.182, y = 0.227 - .5*.182, default.units = "native", gp = gpar(col = "red", lwd = 2))
grid.points(x = 0.045 + .5*.182, y = 0.227 - .5*.182, default.units = "native",
size = unit(5, "mm"), pch = 16, gp=gpar(col = "red"))


## Second line (and points)
seekViewport("boxplot")
downViewport("panel.7-5-7-5")
grid.move.to(x = .115, y = .503, default.units = "native")


seekViewport("heatmap")
downViewport("panel.7-5-7-5")
grid.line.to(x = 0.227 + .5*.182, y = 0.409 - .5*.182, default.units = "native", gp = gpar(col = "red", lwd = 2))
grid.points(x = 0.227 + .5*.182, y = 0.409 - .5*.182, default.units = "native",
size = unit(5, "mm"), pch = 16, gp=gpar(col = "red"))

享受吧。

enter image description here

关于r - ggplot 和网格 : Find the relative x and y positions of a point in a ggplot grob,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44782477/

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