从Cowplot用plot_grid布置地块时,较长的地块标签向右移动 [英] Longer plot labels move to the right when arranging plots with plot_grid from cowplot

查看:138
本文介绍了从Cowplot用plot_grid布置地块时,较长的地块标签向右移动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人问我在Twitter上问这个问题,并认为在这里提出来可能很好

I was asked this question on Twitter and thought it might be good to have it here.

使用plot_grid()制作带标签的并排图时,单字母标签的工作按预期进行:

When making labeled, side-by-side plots with plot_grid(), things work as expected for single-letter labels:

library(cowplot)

p1 <- ggplot(iris, aes(x = Sepal.Length, fill = Species)) + 
  geom_density(alpha = 0.7) +
  ggtitle("") + theme_minimal()

p2 <- ggplot(iris, aes(x = Sepal.Length, fill = Species)) + 
  geom_density(alpha = 0.7) +
  ggtitle("") +
  scale_fill_grey() + theme_minimal()

plot_grid(p1, p2, labels = c("A", "B"))

但是,如果我们使用更长的字符串作为标签,则标签会向右移动,并且它们移动得越长,字符串越长:

However, if we're using longer strings as labels, the labels move to the right, and they move the more the longer the strings are:

plot_grid(p1, p2, labels = c("Density plot in color", "In gray"))

如何解决?

免责声明:我是包装的作者.希望将其附在此处并给出答案,希望对您有所帮助.

Disclaimer: I'm the author of the package. Posting this here with answer in the hope it will be useful.

推荐答案

plot_grid()中的参数hjustlabel_x的默认设置针对单字母标签进行了优化,并且它们的使用时间不再更长标签.覆盖设置可以解决此问题:

The default settings for the parameters hjust and label_x in plot_grid() are optimized for single-letter labels, and they don't work for longer labels. Overriding the settings fixes the problem:

plot_grid(p1, p2, labels = c("Density plot in color", "In gray"),
          hjust = 0, label_x = 0.01)

尤其是,默认的hjust设置是hjust = -0.5.这会将标签向右移动相当于其宽度一半的量.这对于单个字母标签是有意义的,因为然后可以通过设置label_x = 0使字母出现在距左边框一半字母宽度的位置,并且不管标签字体大小或用户选择的任何其他打印功能如何,该字母都可以工作

In particular, the default hjust setting is hjust = -0.5. This moves the label to the right by an amount equivalent to half its width. This makes sense for single letter labels, because then we can have the letters appear half a letter width away from the left border by setting label_x = 0, and this will work irrespective of label font size or any other plot features the user may have chosen.

但是,对于较长的标签,尤其是长度不同的标签,将标签移动一半宽度完全没有任何意义.

However, moving a label by half its width doesn't make any sense at all for longer labels, and in particular labels of differing lengths.

这篇关于从Cowplot用plot_grid布置地块时,较长的地块标签向右移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆