ggplot2 axis.text边距,比例尺位置已修改 [英] `ggplot2` axis.text margin with modified scale position

查看:420
本文介绍了ggplot2 axis.text边距,比例尺位置已修改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定是否遇到错误或执行不正确.在ggplot中指定axis.text边距并移动轴的位置时,设置不会持久.

I'm not sure if I've encountered a bug or am doing incorrectly. When specifying axis.text margins in ggplot, and moving the position of the axis, the settings to not persist.

在不移动轴文本的情况下,轴周围有足够的空间:

Without moving the axis text, there is plenty of space around the axis:

library(ggplot)

ggplot(mtcars, aes(mpg, wt)) +
    geom_point() +
    theme(axis.text.y = element_text(color = "red", margin = margin(40, 40, 40, 40)))

但是,当头寸改变时,边距不适用:

But, when the position is changed, the margins do not apply:

ggplot(mtcars, aes(mpg, wt)) +
    geom_point() +
    scale_y_continuous(position = "right") + #This is the new line
    theme(axis.text.y = element_text(color = "red", margin = margin(40, 40, 40, 40))) 

我希望无论axis.text是在右边还是在左边,边距都将保留下来.我在做错什么吗?

I'd expect the margins to carry over regardless of whether the axis.text is on the right or left. Am I doing something wrong?

推荐答案

我相信,这是因为右侧y轴标签的外观由theme()中的axis.text.y.right决定,而它是从它仅继承axis.text.y.right本身未声明的参数.

I believe this arises because the appearance of the right side y-axis label is dictated by axis.text.y.right in theme(), and while it inherits from axis.text.y, it only inherits arguments that are not stated in axis.text.y.right itself.

根据?theme中的详细信息,axis.text.y.right的继承链如下:

According to details in ?theme, the chain of inheritance for axis.text.y.right goes as follows:

axis.text.y.right-> axis.text.y-> axis.text-> text

axis.text.y.right -> axis.text.y -> axis.text -> text

ggplot中的默认主题为theme_grey.在控制台中输入theme_grey(末尾没有()),您将看到完整的功能.让我们看一下相关的位:

The default theme in ggplot is theme_grey. Enter theme_grey (without () at the end) into your console, and you'll see the full function. Let's look at the relevant bits:

function(base_size = 11, base_family = "", base_line_size = base_size/22, 
         base_rect_size = base_size/22) {

  half_line <- base_size/2

  theme(text = element_text(family = base_family, 
                            face = "plain", 
                            colour = "black",
                            size = base_size, 
                            lineheight = 0.9, 
                            hjust = 0.5, 
                            vjust = 0.5, 
                            angle = 0, 
                            margin = margin(), 
                            debug = FALSE), 

        axis.text = element_text(size = rel(0.8), 
                                 colour = "grey30"), 

        axis.text.y = element_text(margin = margin(r = 0.8 * half_line/2), 
                                   hjust = 1), 

        axis.text.y.right = element_text(margin = margin(l = 0.8 * half_line/2), 
                                         hjust = 0), 
        ...
        complete = TRUE)
}

?element_text显示element_text期望的参数的完整列表:

?element_text shows the full list of arguments that element_text expects:

element_text(family = NULL, face = NULL, colour = NULL, size = NULL,
  hjust = NULL, vjust = NULL, angle = NULL, lineheight = NULL,
  color = NULL, margin = NULL, debug = NULL, inherit.blank = FALSE)

给出所有继承,theme_greyaxis.text.y.right的实际参数是什么?

Given all the inheritance, what are axis.text.y.right's actual arguments in theme_grey?

  • 家庭= base_family(来自text)
  • face = "plain"(来自text)
  • colour = "grey30"(来自axis.text,覆盖了text"black")
  • 大小= base_size的80%(摘自axis.text base_sizeaxis.text rel(0.8))
  • just = 0(来自axis.text.y.right,它覆盖了axis.text.y1,它覆盖了text0.5)
  • vjust = 0.5(来自text)
  • angle = 0(来自text)
  • lineheight = 0.9(来自text)
  • 保证金= margin(l = 0.8 * half_line/2)(来自axis.text.y.right,覆盖了axis.text.ymargin = margin(r = 0.8 * half_line/2,覆盖了textmargin())
  • debug = FALSE(来自text)
  • inherit.blank = FALSE(element_text的默认参数)
  • family = base_family (from text)
  • face = "plain" (from text)
  • colour = "grey30" (from axis.text, which overrides text's "black")
  • size = 80% of base_size (from axis.text's rel(0.8) modification of text's base_size)
  • hjust = 0 (from axis.text.y.right, which overrides axis.text.y's 1, which overrides text's 0.5)
  • vjust = 0.5 (from text)
  • angle = 0 (from text)
  • lineheight = 0.9 (from text)
  • margin = margin(l = 0.8 * half_line/2) (from axis.text.y.right, which overrides axis.text.y's margin = margin(r = 0.8 * half_line/2, which overrides text's margin())
  • debug = FALSE (from text)
  • inherit.blank = FALSE (element_text's default parameter)

这样,给定一段类似以下的代码,axis.text.y.right将继承color = "red"(覆盖axis.textcolour = "grey30").但是,由于它具有自己的margin参数,因此不会继承margin = margin(40, 40, 40, 40):

As such, given a piece of code like below, axis.text.y.right will inherit color = "red" (which overrides axis.text's colour = "grey30"). But since it has its own margin argument, it will not inherit margin = margin(40, 40, 40, 40):

ggplot(mtcars, aes(mpg, wt)) +
    geom_point() +
    scale_y_continuous(position = "right") +
    theme(axis.text.y = element_text(color = "red", margin = margin(40, 40, 40, 40))) 

指定axis.text.y.right而不是axis.text.y可以解决问题:

Specifying axis.text.y.right instead of axis.text.y would do the trick:

ggplot(mtcars, aes(mpg, wt)) +
  geom_point() +
  scale_y_continuous(position = "right") +
  theme(axis.text.y.right = element_text(color = "red", margin = margin(40, 40, 40, 40))) 

这篇关于ggplot2 axis.text边距,比例尺位置已修改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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