首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当在多个地块上使用y >1进行绘制时,Cowplot剪辑标签

当在多个地块上使用y >1进行绘制时,Cowplot剪辑标签
EN

Stack Overflow用户
提问于 2019-01-23 19:08:23
回答 2查看 731关注 0票数 3

首先,来看一些模拟图表:

代码语言:javascript
复制
df <- data.frame(x = 1:10, y = 2:11)

因为我的真实图形相当复杂,所以我不打算在这里提供它们,而是使用其他重现问题的图形。可以这么说,当在plot_grid中将align设置为"hv"时,它会在我的图形之间创建大量的空白。为了解决这个问题,我在原始绘图中设置了如下边距:

代码语言:javascript
复制
library(ggplot2)

plot1 <- ggplot(df, aes(x = x, y = y)) +
  geom_point() + 
  theme(plot.margin = unit(c(-1, 0, 0, 0), "cm"))
plot2 <- ggplot(df, aes(x = y, y = -x)) +
  geom_point() + 
  theme(plot.margin = unit(c(-1, 0, 0, 0), "cm"))
plot3 <- ggplot(df, aes(x = x, y = y)) +
  geom_point() + 
  theme(plot.margin = unit(c(-1, 0, 0, 0), "cm"))
plot4 <- ggplot(df, aes(x = y, y = -x)) +
  geom_point() + 
  theme(plot.margin = unit(c(-1, 0, 0, 0), "cm"))

它延伸了地块的顶部,所以它们填满了空白。一切都很好(在这个例子中,顶部和底部之间可能有一些重叠):

代码语言:javascript
复制
library(cowplot)

plot5 <- plot_grid(plot1, plot2, plot3, plot4, nrow = 2, ncol = 2, 
                   align = "hv")
plot5

直到我试着添加标签。因为我已经扩展了图,所以我想添加高于y = 1限制的标签。

代码语言:javascript
复制
plot5 <- plot_grid(plot1, plot2, plot3, plot4, nrow = 2, ncol = 2, 
                   align = "hv", labels = c("A", "B", "C", "D"), 
                 label_x = 0.1,
                 label_y = 1.15)+
  theme(plot.margin = unit(c(3, 1, 1, 1), "cm"))
plot5

返回此警告:

代码语言:javascript
复制
Removed 1 rows containing missing values (geom_text).  

这意味着它去掉了两个顶级标签,尽管似乎有足够的空间。因此,我想,也许只有在绘制完标签之后,才会应用打印页边距,或者可能是裁剪导致了问题。所以,我这样做了:

代码语言:javascript
复制
plot5 <- plot_grid(plot1, plot2, plot3, plot4, nrow = 2, ncol = 2, 
                   align = "hv") + 
  theme(plot.margin = unit(c(3, 0, 0, 0), "cm")) +
  coord_cartesian(clip = "off")

plot5 + 
  draw_plot_label(c("A", "B", "C", "D"), c(0, 0.5, 0, 0.5), c(1.1, 1.1, .5, .5))   

这将返回与前面相同的错误,尽管如您所见,在绘图顶部有更多可用空间。作为最后的手段,我也关闭了原始绘图中的剪辑,但这也没有解决任何问题,例如:

代码语言:javascript
复制
plot1 <- ggplot(df, aes(x = x, y = y)) +
  geom_point() + 
  theme(plot.margin = unit(c(-1, 0, 0, 0), "cm")) + 
  coord_cartesian(clip = "off")

只有当我绘制超过y = 1时,才会出现问题。例如,y = 1.01会带来问题。如何解决此问题?这似乎是一个相当简单的问题,除非ggplotcowplot有问题。

EN

回答 2

Stack Overflow用户

发布于 2019-02-11 12:29:57

通过遵循使用'egg‘包的建议,我解决了这个问题:删除图形之间的空格,并以一种合理的方式标记绘图。

此外,值得注意的是,在绘图方面,egg似乎比cowplot更快。

代码语言:javascript
复制
install.packages("egg")
library(egg)

df <- data.frame(x = 1:10, y = 2:11)

plot1 <- ggplot(df, aes(x = x, y = y)) +
  geom_point() + 
  theme(plot.margin = unit(c(0, 0, 0, 0), "cm"))
plot2 <- ggplot(df, aes(x = y, y = -x)) +
  geom_point() + 
  theme(plot.margin = unit(c(0, 0, 0, 0), "cm"))
plot3 <- ggplot(df, aes(x = x, y = y)) +
  geom_point() + 
  theme(plot.margin = unit(c(0, 0, 0, 0), "cm"))
plot4 <- ggplot(df, aes(x = y, y = -x)) +
  geom_point() + 
  theme(plot.margin = unit(c(0, 0, 0, 0), "cm"))

figure <- egg::ggarrange(plot1, plot2, 
         plot3, plot4, 
             nrow = 2,ncol=2,
             labels=c("A", "B", "C","D"),
             label.args = list(gp=gpar(font=2), x=unit(.5,"line")))
figure

票数 1
EN

Stack Overflow用户

发布于 2019-01-24 07:31:55

好了,我有了一个答案,它解决了标签的问题,但它不能解决无法绘制过去的y=1的问题。我没有减少原始绘图中的顶部页边距,而是减少了底部页边距:

代码语言:javascript
复制
plot1 <- ggplot(df, aes(x = x, y = y)) +
  geom_point() + 
  theme(plot.margin = unit(c(1, 0, -1, 0), "cm"))
plot2 <- ggplot(df, aes(x = y, y = -x)) +
  geom_point() + 
  theme(plot.margin = unit(c(1, 0, -1, 0), "cm"))
plot3 <- ggplot(df, aes(x = x, y = y)) +
  geom_point() + 
  theme(plot.margin = unit(c(1, 0, -1, 0), "cm"))
plot4 <- ggplot(df, aes(x = y, y = -x)) +
  geom_point() + 
  theme(plot.margin = unit(c(1, 0, -1, 0), "cm"))

plot5 <- plot_grid(plot1, plot2, plot3, plot4, nrow = 2, ncol = 2, 
                   align = "hv", labels = c("A", "B", "C", "D"), 
                   label_x = 0.1,
                   label_y = 1) +
  theme(plot.margin = unit(c(1, 1, 2, 1), "cm"))
plot5

这让我可以在一个合理的位置绘制标签。

不过,我仍然不明白为什么标签会被裁剪(而且在y<0时也会发生这种情况)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54325859

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档