将右侧的轴标签与 ggplot2 对齐 [英] Align axis label on the right with ggplot2

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

问题描述

考虑以下内容

d = data.frame(y=rnorm(120),x=rep(c("bar", "long category name", "foo"), each=40))ggplot(d,aes(x=x,y=y)) +geom_boxplot() +主题(axis.text.x=element_text(大小=15,角度=90))

x 轴标签按标签中心对齐.是否可以自动在右侧对齐,以便每个标签都在图表正下方结束?

解决方案

这正是 hjustvjust 参数在 ggplot 中的作用>.它们分别控制水平和垂直对齐,范围从 0 到 1.有关对齐及其值的更多详细信息,请参阅此问题 (

Consider the following

d = data.frame(y=rnorm(120), 
               x=rep(c("bar", "long category name", "foo"), each=40))

ggplot(d,aes(x=x,y=y)) + 
    geom_boxplot() + 
    theme(axis.text.x=element_text(size=15, angle=90))

The x-axis labels are aligned by the center of the label. Is it possible to automatically align on the right so that every label would end right below the graph?

解决方案

This is precisely what the hjust and vjust parameters are for in ggplot. They control the horizontal and vertical justification respectively and range from 0 to 1. See this question for more details on justifications and their values (What do hjust and vjust do when making a plot using ggplot?).

To get the labels the way you want you can use:

  • hjust = 0.95 (to leave some space between the labels and the axis)
  • vjust = 0.2 (to center them in this case)

ggplot(d,aes(x=x,y=y)) + geom_boxplot() + 
       theme(axis.text.x=element_text(size=15, angle=90,hjust=0.95,vjust=0.2))

这篇关于将右侧的轴标签与 ggplot2 对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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