1

I currently have a plot that looks like this 1 and would like to move the Y axis title horizontally to the right so that Salary is over the top of 150000. Below is the current code.

ggplot(data=SalaryGender, aes(y=Salary, x=Age, color=Gender)) + geom_point() + 
 xlab("Age") + ylab("salary") + ggtitle("Relationship Salary, Age, and Gender") + stat_smooth(method="lm", se=FALSE) + theme_bw() + theme(plot.title = element_text(face = "bold", size = 14),axis.text = element_text(face = "bold", size = 13),axis.title = element_text(face = "bold", size = 11),axis.title.y=element_text(angle=0)) 
urtt2520
  • 13
  • 3
  • You can change the position of the axis title by adding "hjust" for horizontal and "vjust" for vertical movement. axis.title.y=element_text(angle=0, hjust = -1, vjust = 1)) This is an example, but I don't think you can go on top of the values. – Bloxx Oct 12 '21 at 21:45
  • 1
    You can also set a negative right margin on the axis title through the theme element. – teunbrand Oct 12 '21 at 21:57

1 Answers1

0

Just wonder if annotate can do the trick. You may need to adjust x and y values as well as add size on your side

library(tidyverse)
df <- data.frame(
  Salary = c(14000, 146000, 138000, 121000, 135000, 90000, 5000, 50), 
  Age = c(20, 40, 50, 79, 40, 30, 80, 50), 
  Gender = c("Female", "Male", "Female", "Male","Female", "Male","Female", "Male")
)

ggplot(data=df, 
       aes(y=Salary, x=Age, color=Gender)) + 
  geom_point() + 
  xlab("Age") + ylab("salary") + 
  ggtitle("Relationship Salary, Age, and Gender") + 
  stat_smooth(method="lm", se=FALSE) + theme_bw() + 
  theme(plot.title = element_text(face = "bold", size = 14),
        axis.text = element_text(face = "bold", size = 13),
        axis.title = element_text(face = "bold", size = 11),
        axis.title.y = element_blank()
        ) +
  coord_cartesian(clip = "off", ylim = c(0, 150000), xlim = c(20, NA))+ 
  annotate("text", x = 12, y = 161000, label = "Salary")

enter image description here

Zhiqiang Wang
  • 6,206
  • 2
  • 13
  • 27