Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/82.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中的x记号标签(移动标签和更改角度)_R_Ggplot2_Boxplot_Axis Labels - Fatal编程技术网

如何更改R中的x记号标签(移动标签和更改角度)

如何更改R中的x记号标签(移动标签和更改角度),r,ggplot2,boxplot,axis-labels,R,Ggplot2,Boxplot,Axis Labels,为了使我的图表更形象,我尝试在x轴上移动刻度标签。我想移动标签,这样标签就不会进入图形并覆盖数据。我当前的图形代码如下所示: ggplot(Duffel_plotdat, aes(Afkorting, est)) + geom_point() + geom_errorbar(aes(ymin=est-se, ymax=est+se)) + labs(title="Variance loggers for each AHS") + xlab("Artificial hibernation str

为了使我的图表更形象,我尝试在x轴上移动刻度标签。我想移动标签,这样标签就不会进入图形并覆盖数据。我当前的图形代码如下所示:

 ggplot(Duffel_plotdat, aes(Afkorting, est)) + geom_point() + geom_errorbar(aes(ymin=est-se, ymax=est+se)) + labs(title="Variance loggers for each AHS") + xlab("Artificial hibernation structures") + ylab("Variance") +
scale_x_discrete(breaks=c("BL","BW","H","K","MB","MCD","WK"),
                 labels=c("Loose in brick", "In brick closed\nwith cotton wool", "Square\nceiling box", "Wall logger\ndirectly on wall", "Wall logger\non wooden cube", "Middle of\nCD-rack", "Wall plate box")) +
  theme(axis.text.x = element_text(angle=45))
给出这个图表:

我发现了关于这个主题的另一个问题(),但由于数据不再可用,我无法确定这是否对我有帮助

最后,我可能想更改角度,以便标签名称以另一种方式倾斜(从左上角到右下角)。我试着使用

angle=135 

使角度正确,但将文本倒置。

使用以下代码:

ggplot(Duffel_plotdat, aes(Afkorting, est)) + geom_point() + geom_errorbar(aes(ymin=est-se, ymax=est+se)) + labs(title="Variance loggers for each AHS") + xlab("Artificial hibernation structures") + ylab("Variance") +
scale_x_discrete(breaks=c("BL","BW","H","K","MB","MCD","WK"),
                 labels=c("Loose in brick", "In brick closed\nwith cotton wool", "Square\nceiling box", "Wall logger\ndirectly on wall", "Wall logger\non wooden cube", "Middle of\nCD-rack", "Wall plate box")) +
  theme(axis.text.x = element_text(vjust=0.6, angle=-45))
给出了下图:

您需要在
元素\u text
中修改
hjust
和/或
vjust
。老实说,我永远都记不起正确的组合来将标签放在正确的位置,我总是要尝试2-3次才能得到正确的组合。你可以尝试通过调整偏移:对于角度,尝试-45(或315)。对于hjust和vjust,正确的值将介于0和1之间,但是,像@joran一样,我永远记不起正确的组合。谢谢这两个。vjust和使用负值表示角度非常有效。有关更新的代码和图,请参见答案。