Search code examples
rggplot2axis-labels

Shortcut for axis labels in ggplot


ggplot labels are typically added by the augument xlab/ylab, e.g.

 ggplot(mtcars,aes(mpg, wt)) + geom_point()+ xlab('x_label')+ ylab('y_label')

Is there any shortcut to predefine the label and call them? e.g.: mylabels is predefined . This would make repetitive use of certain labels more efficient.

mylabels <- xlab('x_label')+ylab('y_label') 
ggplot(mtcars,aes(mpg, wt)) + geom_point() + mylabels

Solution

  • You could store the label information in a list as follows

    mylabels <- list(
      xlab("x_label"), 
      ylab("y_label")
    )
    ggplot(mtcars,aes(mpg, wt)) + geom_point() + mylabels