Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/66.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 增加y轴上文本和标题之间的距离_R_Layout_Plot_Ggplot2 - Fatal编程技术网

R 增加y轴上文本和标题之间的距离

R 增加y轴上文本和标题之间的距离,r,layout,plot,ggplot2,R,Layout,Plot,Ggplot2,y轴标题显示得太靠近轴文本 ggplot(mpg, aes(cty, hwy)) + geom_point() 我曾尝试使用theme()更改许多参数的值,但似乎没有任何帮助。从ggplot2.0.0可以使用元素的margin=参数来更改轴标题和数字之间的距离。在元素的top、right、bottom和left侧设置margin的值 ggplot(mpg, aes(cty, hwy)) + geom_point()+ theme(axis.title.y = element_text(m

y轴标题显示得太靠近轴文本

ggplot(mpg, aes(cty, hwy)) + geom_point()


我曾尝试使用
theme()
更改许多参数的值,但似乎没有任何帮助。

ggplot2.0.0
可以使用
元素的
margin=
参数来更改轴标题和数字之间的距离。在元素的
t
op、
r
ight、
b
ottom和
l
eft侧设置
margin
的值

ggplot(mpg, aes(cty, hwy)) + geom_point()+
  theme(axis.title.y = element_text(margin = margin(t = 0, r = 20, b = 0, l = 0)))

边距
也可用于其他
元素\文本
元素(请参见
主题
),例如
axis.text.x
axis.text.y
title

添加


为了在轴具有不同位置时设置轴标题的边距(例如,使用
scale\ux…(position=“top”)
,您需要不同的主题设置-例如
axis.title.x.top
。请参阅。

基于此论坛帖子:

听起来最简单的方法是在x轴标签之前和y轴标签之后添加一个换行符(\n)。似乎比上面发布的解决方案简单得多(尽管更愚蠢)

ggplot(mpg, aes(cty, hwy)) + 
    geom_point() + 
    xlab("\nYour_x_Label") + ylab("Your_y_Label\n")

希望有帮助!

出于某种原因,Didzis Elferts建议的边距参数对我不起作用。因此,我使用了一种不同的方法,它比添加空行更灵活,但需要放弃轴刻度

myplot + theme(axis.ticks.x = element_blank(), axis.ticks.length.x = unit(3.25, "cm")

我想,你可以用
geom_segment
手动添加记号。另一种可能是
[ggalt::annotation_ticks][1]
,但我也没有费心尝试(注意,CRAN(0.4)上当前版本的ggalt不支持此功能,github(0.6)上的版本支持此功能).

我通常使用这种方法,速度更快,除非我需要一些特定的调整,否则不需要再向ggplot添加另一个选项。这种方法似乎不适用于
facet\u grid
,而Adam B建议的答案适用(即使用\n的换行符)@Anonymous你能举个例子说明这个
margin=
参数不起作用吗?我用
theme\u bw
试过了。对不起,发布整个代码是不可读的/凌乱的…@Anonymous如果你使用
theme\u bw()
那么
theme\u bw()
应该在
theme()之前使用
函数,因为在预定义的主题中,axis标题有allready参数。感谢您的澄清!