R语言与医学统计图形-【21】ggplot2标题
阅读原文时间:2023年07月08日阅读:1

在期刊杂志中,需要设置的图形标题并不多。

除了图形标题,还有坐标轴标题(标签)、图例标题、脚注等。

  • 标题函数:ggtitle,labs
  • 坐标轴标题函数:xlab,ylab

labs的参数可以是title,subtitle,caption,x,y,也可以是映射属性,如color,size,shape等来表示图例标题。

p <- ggplot(mtcars,aes(mpg,wt,color=factor(cyl)))+
  geom_point()
p+ggtitle(label='New plot title',subtitle = 'subtitle')+
  labs(color='legend')+ #size/shape,映射属性表示图例标题
  xlab('new x label')+
  ylab('new y label')+
  labs(caption = '(based on mtcars data)') #脚注

标题调整

通过theme函数的参数plot.title,plot.background,plot.margin

p+labs(title='New plot title')+ labs(color='legend')+
  labs(caption = '(based on mtcars data)')+
  theme(plot.title = element_text(color = 'red',
                                  size=9,
                                  hjust=0.5),
        plot.caption = element_text(color='blue'))

hjust参数取值范围0-1,0表示最左侧,1表示最右侧。