[英]How do I move my axis labels to the side in ggplot in R
I want to set the x-axis label under the right corner of the plot, and the y-axis label to the left upper corner.我想在plot的右下角设置x轴label,左上角设置y轴label。
As an example of a plot, I made up this short piece of code:作为 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")
What I tried and what I want I found something that moves the whole y-axis to the right (scale_y_continuous(position = "right")) but not the x-label.我尝试过的和我想要的我发现了一些东西可以将整个 y 轴向右移动 (scale_y_continuous(position = "right")) 但不是 x 标签。 I also found how to move the numbers on the labels here but I want the axis name to move from centered to right.我还找到了如何在此处移动标签上的数字,但我希望轴名称从居中移动到右侧。
This could be achieved by aligning the axis title to the right using theme(axis.title = element_text(hjust = 1))
(or axis.title.x
and axis.title.y
):这可以通过使用theme(axis.title = element_text(hjust = 1))
(或axis.title.x
和axis.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.