简体   繁体   中英

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.

As an example of a plot, I made up this short piece of code:

#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. I also found how to move the numbers on the labels here but I want the axis name to move from centered to right.

I want something like this: 轴标签都居中的图

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 ):

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`.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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