Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 将轴标题与轴边完全对齐_R_Ggplot2 - Fatal编程技术网

R 将轴标题与轴边完全对齐

R 将轴标题与轴边完全对齐,r,ggplot2,R,Ggplot2,对于ggplot2绘图,hjust和vjust是相对于整个绘图定义的对齐函数。因此,当它们应用于轴标签时,它们不会相对于轴线进行定义 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)) ) 但是,相对于

对于
ggplot2
绘图,
hjust
vjust
是相对于整个绘图定义的对齐函数。因此,当它们应用于轴标签时,它们不会相对于轴线进行定义

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))
  )
但是,相对于轴线调整轴标题更为自然

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))
  )
具体来说,我正在寻找一种方法来左对齐
x
轴标题,并在顶部对齐和旋转
y
轴标题

怎么能这样呢


如问题所述,
hjust
vjust
是相对于整个绘图定义的对齐函数

因此,
hjust=0
将无法实现相对于
x
轴线起点的完美左对齐。但是,它可以与
expand\u limits
scale\u x\u continuous
结合使用

同样地,轴标题的
y
也采用
scale\u y\u continuous

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

... +
  expand_limits(x = 0, y = 0) +
  scale_x_continuous(expand = c(0, 0)) + 
  scale_y_continuous(expand = c(0, 0))
然后,我们可以在此处指定调整,并为
y
轴标题添加旋转,这要求我们使用
hjust
而不是
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)
)
我们固定我们的斧头。为了更好地控制轴,我还定义了轴范围(
限制


这是一个很好的答案。有几点建议:你应该在问题中提到它是关于
ggplot2
,并且你应该在回答中包含完整的代码(从
library(ggplot2)
开始)。谢谢你,我很快就会更新它。
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))
  )
graph <-
  graph +
  xlab("This is the x axis") + 
  ylab("This is the y axis")
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")