繁体   English   中英

如何在 R 中将轴标签移动到 ggplot 的一侧

[英]How do I move my axis labels to the side in ggplot in R

我想在plot的右下角设置x轴label,左上角设置y轴label。

作为 plot 的示例,我编写了这段简短的代码:

#library(ggplot2)
library(titanic)
data("titanic_train", package = "titanic")
titanic <- titanic_train

ggplot(data = titanic, aes(x = Fare)) + 
  geom_histogram() +
 xlab("Want this on right side")

我尝试过的和我想要的我发现了一些东西可以将整个 y 轴向右移动 (scale_y_continuous(position = "right")) 但不是 x 标签。 我还找到了如何在此处移动标签上的数字,但我希望轴名称从居中移动到右侧。

我想要这样的东西: 轴标签都居中的图

这可以通过使用theme(axis.title = element_text(hjust = 1)) (或axis.title.xaxis.title.y )将轴标题右对齐来实现:

library(ggplot2)
library(titanic)
data("titanic_train", package = "titanic")
titanic <- titanic_train

ggplot(data = titanic, aes(x = Fare)) +
  geom_histogram() +
  xlab("Want this on right side") +
  theme(axis.title = element_text(hjust = 1))
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM