我正在组合多个ggplot图,使用网格视口,这是必要的(我相信),因为我想要旋转绘图,这在标准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)
Run Code Online (Sandbox Code Playgroud)
如何找到(比如说)盒子图的第一个框的相对x和y位置?以及三角协方差矩阵的相对x和y位置.
我知道我必须查看grob对象boxplot,但我不知道如何在那里找到相关数据.
编辑:
这些线从底部图上的点到顶部图上的块.
这是一个老问题,所以答案可能不再相关,但无论如何......
这并不简单,但可以通过grid编辑工具来完成。人们需要在此过程中收集信息,这使得解决方案变得繁琐。这在很大程度上是一种一次性解决方案。很大程度上取决于两个 ggplots 的细节。但也许这里有足够的人使用。关于要绘制的线的信息不足;我将画两条红线:一条从第一个箱线图的横杆中心到热图左下图块的中心;一个从第一个箱线图的横杆中心到热图中的下一个图块。
几点:
grid函数grid.move.to()
和grid.line.to().# 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()
Run Code Online (Sandbox Code Playgroud)
相关位在与面板有关的部分中。热图 grob 的名称是:
geom_rect.rect.2
构成第一个箱线图的 grob 的名称是(数字可以不同):
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
Run Code Online (Sandbox Code Playgroud)
(注意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)
Run Code Online (Sandbox Code Playgroud)
晶须的 x 坐标为 0.115,横杆的 y 坐标为 .507
现在,在两个视口之间绘制线条。这些线是在面板视口中“绘制”的,但热图面板视口的名称与箱线图面板视口的名称相同。为了克服这个困难,我寻找箱线图视口,然后向下推到它的面板视口;同样,我寻找热图视口,然后向下推到其面板视口。
## 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"))
Run Code Online (Sandbox Code Playgroud)
享受。