Align axis label on the right with ggplot2

后端 未结 2 1568
野趣味
野趣味 2020-12-16 11:47

Consider the following

d = data.frame(y=rnorm(120), 
               x=rep(c(\"bar\", \"long category name\", \"foo\"), each=40))

ggplot(d,aes(x=x,y=y)) + 
          


        
2条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-16 12:22

    This is precisely what the hjust and vjust parameters are for in ggplot. They control the horizontal and vertical justification respectively and range from 0 to 1. See this question for more details on justifications and their values (What do hjust and vjust do when making a plot using ggplot?).

    To get the labels the way you want you can use:

    • hjust = 0.95 (to leave some space between the labels and the axis)
    • vjust = 0.2 (to center them in this case)

    ggplot(d,aes(x=x,y=y)) + geom_boxplot() + 
           theme(axis.text.x=element_text(size=15, angle=90,hjust=0.95,vjust=0.2))
    

提交回复
热议问题