Center Plot title in ggplot2

前端 未结 4 1796
臣服心动
臣服心动 2020-12-02 03:31

Hi this simple code (and all my scripts from this morning) has started giving me a off center title in ggplot2

Ubuntu version: 16.04

R studio version: Versi         


        
4条回答
  •  不知归路
    2020-12-02 04:14

    As stated in the answer by Henrik, titles are left-aligned by default starting with ggplot 2.2.0. Titles can be centered by adding this to the plot:

    theme(plot.title = element_text(hjust = 0.5))
    

    However, if you create many plots, it may be tedious to add this line everywhere. One could then also change the default behaviour of ggplot with

    theme_update(plot.title = element_text(hjust = 0.5))
    

    Once you have run this line, all plots created afterwards will use the theme setting plot.title = element_text(hjust = 0.5) as their default:

    theme_update(plot.title = element_text(hjust = 0.5))
    ggplot() + ggtitle("Default is now set to centered")
    

    To get back to the original ggplot2 default settings you can either restart the R session or choose the default theme with

    theme_set(theme_gray())
    

提交回复
热议问题