Home > OS >  Place y axis label in the right without changing the scale in ggplot
Place y axis label in the right without changing the scale in ggplot

Time:01-27

I have a plot enter image description here

and I would like to move the y axis label in the right but without changing the scale. Is there a way to do it? My code is:

ggplot(co, aes(x=model1,y = model2, color=ex))  
    geom_point()

CodePudding user response:

One option to achieve your desired result would be to first duplicate the y axis, then use theme options to get rid of the title on the left, the ticks and of course the labels on the right:

Using a simple plot based on mtcars as an example:

library(ggplot2)

p <- ggplot(mtcars, aes(hp, mpg))  
  geom_point()

# Move only the label to the right
p  
  scale_y_continuous(sec.axis = dup_axis(labels = NULL))  
  theme(axis.title.y.left = element_blank(),
        axis.ticks.y = element_blank())

EDIT If you only want the title on the right, then switch the position and get rid of the text and ticks using theme options:

# Move the label to the right and get rid of everything else
p  
  scale_y_continuous(position = "right")  
  theme(
    axis.ticks.y = element_blank(),
    axis.text.y = element_blank()
  )

CodePudding user response:

moving the axis to the right hand side can be accomplished using

  scale_y_continuous(position = "right")
  • Related