使轴标题与轴边缘完美对齐 [英] Perfectly aligning axis titles to axis edges

查看:67
本文介绍了使轴标题与轴边缘完美对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于 ggplot2 绘图, just vjust 是相对于整个绘图定义的对齐函数.因此,将它们应用于轴标签时,它们并不是相对于轴线定义的.

For ggplot2 plots, hjust and vjust are alignment functions defined relatively to the entire plot. So when they are applied to axis labels, they are not defined relatively to the axis lines.

但是,相对于轴线调整轴标题更为自然.

However, it is more natural to adjust the axis titles relatively to the axis lines.

具体来说,我正在寻找一种方法,可以使 x 轴标题左对齐,并使顶部的y_code轴标题对齐并旋转.

Specifically, I'm looking for a way to left-align the x axis title and to top-align and rotate the y axis title.

这怎么办?

推荐答案

根据问题中的说明, just vjust 是相对于整个图定义的对齐函数.

As specified in the question, hjust and vjust are alignment functions defined relatively to the entire plot.

因此,相对于 x 轴线的起点, just = 0 不会实现完美的左对齐.但是,它可以与 expand_limits scale_x_continuous 结合使用.

Hence, hjust=0 will not achieve a perfect left-alignment relatively to beginning of the x axis line. However, it can work in combination with expand_limits and scale_x_continuous.

并且类似地,对于 y 轴标题使用 scale_y_continuous .

And similarly with scale_y_continuous for the y axis title.

在问题的附加图中,情节从原点开始.因此,首先,我们必须强制情节从原点实际开始:

In the question's attached image, the plot begins in the origin. So first, we must force the plot to actually start in the origin:

... +
  expand_limits(x = 0, y = 0) +
  scale_x_continuous(expand = c(0, 0)) + 
  scale_y_continuous(expand = c(0, 0))

然后,我们可以指定调整项-在此处还添加了 y 轴标题的旋转,然后需要我们使用 just 而不是 vjust :

We can then specify the adjustments—here also added with the rotation for the y axis title, which then requires that we use hjust instead of vjust:

... +
theme(
  axis.title.x = element_text(hjust = 0),
  axis.title.x = element_text(angle = 90, hjust = 1)
)


完整的示例

首先,我们加载 ggplot2 并创建一个数据集:

library(ggplot2) 

df <- data.frame(
  x = c(0, 50, 100),
  y = c(20, 40, 80)
)

我们创建情节并添加 geom_line().

graph <- 
  ggplot(data=df, mapping = aes(x=x, y=y)) +
  geom_line()

我们固定轴.为了更好地控制轴,我还定义了轴范围(限制).

We fix our axes. As an added bonus for better control of the axes, I also define the axes ranges (limits).

graph <-
  graph +
  scale_x_continuous(expand = c(0,0), limits = c(min(df$x), max(df$x))) +
  scale_y_continuous(expand = c(0,0), limits = c(min(df$y), max(df$y)))

然后,我们格式化轴标题.为了获得更好的视觉表示效果,我还添加了一个边距,以使标题稍微偏离轴线.

Then, we format the axis titles. For better visual representation, I've also added a margin to move the titles slightly away from the axis lines.

graph <-
  graph +
  theme(
    axis.title.x = element_text(hjust = 0, margin = margin(t=6)),
    axis.title.y = element_text(angle = 90, hjust = 1, margin=margin(r=6))
  )

最后,我们添加轴标题:

And finally, we add the axis titles:

graph <-
  graph +
  xlab("This is the x axis") + 
  ylab("This is the y axis")

结果:

但是,这会截断最后的标签文本(100中的最后0仅部分可见).要解决此问题,我们必须增加绘图边距,此处示例为在所有侧面周围留出1厘米的边距:

However, this cuts off the last label text (with the last 0 in 100 being only partially visible). To handle this, we must increase the plot margin, here exemplified with a 1 cm margin around all sides:

graph <-
  graph +
  theme(plot.margin = margin(1, 1, 1, 1, "cm"))

最终结果:

library(ggplot2)


df <- data.frame(
  x = c(0, 50, 100),
  y = c(20, 40, 80)
)

graph <-
  ggplot(data=df, mapping = aes(x=x, y=y)) +
  geom_line() +
  expand_limits(x = 0, y = 0) +
  scale_x_continuous(expand = c(0,0), limits = c(min(df$x), max(df$x))) +
  scale_y_continuous(expand = c(0,0), limits = c(min(df$y), max(df$y))) +
  theme(
    axis.title.x = element_text(hjust = 0, margin = margin(t=6)),
    axis.title.y = element_text(angle = 90, hjust = 1, margin=margin(r=6)),
    plot.margin = margin(1, 1, 1, 1, "cm")
  ) +
  xlab("This is the x axis") + 
  ylab("This is the y axis")
    
  

这篇关于使轴标题与轴边缘完美对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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