Search code examples
rgraphingsjplot

R sjplot: How to angle the X axis labels and move them down?


In sjplot you can angle the x axis labels by adding axis.angle = # in the theme. However, this makes the label overlap the x-axis. How do you shift them down so everything is more easy to read? In ggplot, you can use hjust in the axis.text.x theme line, but this does not seem to work for sjplot.

I would add an image, but I do not have enough points on Stack overflow yet to do so. See the code below for an example.

Sample Code:

# load libraries
library(sjPlot)  # for plotting
library(sjmisc)  # for sample data
library(ggplot2) # to access ggplot-themes

# load sample data set
data(efc)

set_theme(
  geom.outline.color = "antiquewhite4", 
  geom.outline.size = 1, 
  geom.label.size = 2,
  geom.label.color = "grey50",
  title.color = "red", 
  title.size = 1.5, 
  axis.angle.x = 45, 
  axis.textcolor = "blue", 
  base = theme_bw()
)

plot_grpfrq(
  efc$e42dep, 
  efc$e16sex, 
  title = NULL, 
  geom.colors = c("cadetblue", "coral"), 
  geom.size = 0.4
)

Solution

  • I modified the sjPlot::set_theme function adding the axis.hjust.x option.

    my_set_theme <- function (base = theme_grey(), theme.font = NULL, title.color = "black", 
        title.size = 1.2, title.align = "left", title.vjust = NULL, 
        geom.outline.color = NULL, geom.outline.size = 0, geom.boxoutline.size = 0.5, 
        geom.boxoutline.color = "black", geom.alpha = 1, geom.linetype = 1, 
        geom.errorbar.size = 0.7, geom.errorbar.linetype = 1, geom.label.color = NULL, 
        geom.label.size = 4, geom.label.alpha = 1, geom.label.angle = 0, 
        axis.title.color = "grey30", axis.title.size = 1.1, axis.title.x.vjust = NULL, 
        axis.title.y.vjust = NULL, axis.angle.x = 0, axis.angle.y = 0, 
        axis.angle = NULL, axis.textcolor.x = "grey30", axis.textcolor.y = "grey30", 
        axis.textcolor = NULL, axis.linecolor.x = NULL, axis.linecolor.y = NULL, 
        axis.linecolor = NULL, axis.line.size = 0.5, axis.textsize.x = 1, axis.hjust.x=NULL,
        axis.textsize.y = 1, axis.textsize = NULL, axis.tickslen = NULL, 
        axis.tickscol = NULL, axis.ticksmar = NULL, axis.ticksize.x = NULL, 
        axis.ticksize.y = NULL, panel.backcol = NULL, panel.bordercol = NULL, 
        panel.col = NULL, panel.major.gridcol = NULL, panel.minor.gridcol = NULL, 
        panel.gridcol = NULL, panel.gridcol.x = NULL, panel.gridcol.y = NULL, 
        panel.major.linetype = 1, panel.minor.linetype = 1, plot.backcol = NULL, 
        plot.bordercol = NULL, plot.col = NULL, plot.margins = NULL, 
        legend.pos = "right", legend.just = NULL, legend.inside = FALSE, 
        legend.size = 1, legend.color = "black", legend.title.size = 1, 
        legend.title.color = "black", legend.title.face = "bold", 
        legend.backgroundcol = "white", legend.bordercol = "white", 
        legend.item.size = NULL, legend.item.backcol = "grey90", 
        legend.item.bordercol = "white") 
    {
        sjtheme <- NULL
        if (is.null(geom.label.color)) {
            geom.label.color <- "black"
        }
        if (!is.null(axis.angle)) {
            axis.angle.x <- axis.angle.y <- axis.angle
        }
        else {
            axis.angle <- axis.angle.x
        }
        if (!is.null(axis.textcolor)) {
            axis.textcolor.x <- axis.textcolor.y <- axis.textcolor
        }
        else {
            if (is.null(axis.textcolor.x)) 
                axis.textcolor <- axis.textcolor.y
            else axis.textcolor <- axis.textcolor.x
        }
        if (!is.null(axis.linecolor)) {
            axis.linecolor.x <- axis.linecolor.y <- axis.linecolor
        }
        else {
            if (is.null(axis.linecolor.x)) 
                axis.linecolor <- axis.linecolor.y
            else axis.linecolor <- axis.linecolor.x
        }
        if (!is.null(axis.textsize)) {
            axis.textsize.x <- axis.textsize.y <- axis.textsize
        }
        else {
            if (is.null(axis.textsize.x)) 
                axis.textsize <- axis.textsize.y
            else axis.textsize <- axis.textsize.x
        }
        if (!is.null(panel.gridcol)) {
            panel.major.gridcol <- panel.minor.gridcol <- panel.gridcol
        }
        else {
            if (is.null(panel.major.gridcol)) 
                panel.gridcol <- panel.minor.gridcol
            else panel.gridcol <- panel.major.gridcol
        }
        if (!is.null(panel.col)) {
            panel.backcol <- panel.bordercol <- panel.col
        }
        else {
            if (is.null(panel.backcol)) 
                panel.col <- panel.bordercol
            else panel.col <- panel.backcol
        }
        if (!is.null(title.align)) {
            if (title.align == "left" || title.align == "l") 
                title.align <- 0
            if (title.align == "right" || title.align == "r") 
                title.align <- 1
            if (title.align == "center" || title.align == "c") 
                title.align <- 0.5
        }
        else {
            title.align <- 0
        }
        if (!is.null(plot.col)) {
            plot.backcol <- plot.bordercol <- plot.col
        }
        else {
            if (is.null(plot.backcol)) 
                plot.col <- plot.bordercol
            else plot.col <- plot.backcol
        }
        if (legend.inside) {
            if (is.character(legend.pos)) {
                if (legend.pos == "top right") 
                    legend.pos <- c(1, 1)
                else if (legend.pos == "bottom right") 
                    legend.pos <- c(1, 0)
                else if (legend.pos == "bottom left") 
                    legend.pos <- c(0, 0)
                else if (legend.pos == "top left") 
                    legend.pos <- c(0, 1)
                if (is.null(legend.just)) 
                    legend.just <- legend.pos
            }
        }
        if (is.null(legend.just)) 
            legend.just <- "center"
        if (!is.null(theme) && any(class(theme) == "theme") && any(class(theme) == 
            "gg")) {
            theme_set(theme)
        }
        else if (!is.null(base) && any(class(base) == "theme") && 
            any(class(base) == "gg")) {
            sjtheme <- base + theme(plot.title = element_text(size = rel(title.size), 
                colour = title.color, hjust = title.align), axis.text = element_text(angle = axis.angle, 
                size = rel(axis.textsize), colour = axis.textcolor), 
                axis.text.x = element_text(angle = axis.angle.x, 
                    size = rel(axis.textsize.x), colour = axis.textcolor.x, hjust=axis.hjust.x), 
                axis.text.y = element_text(angle = axis.angle.y, 
                    size = rel(axis.textsize.y), colour = axis.textcolor.y), 
                axis.title = element_text(size = rel(axis.title.size), 
                    colour = axis.title.color), legend.position = legend.pos, 
                legend.justification = legend.just, legend.text = element_text(size = rel(legend.size), 
                    colour = legend.color), legend.title = element_text(size = rel(legend.title.size), 
                    colour = legend.title.color, face = legend.title.face), 
                legend.background = element_rect(colour = legend.bordercol, 
                    fill = legend.backgroundcol))
            if (!is.null(theme.font)) {
                sjtheme <- sjtheme + theme(text = element_text(family = theme.font))
            }
            if (!is.null(legend.item.backcol)) {
                sjtheme <- sjtheme + theme(legend.key = element_rect(colour = legend.item.bordercol, 
                    fill = legend.item.backcol))
            }
            if (!is.null(legend.item.size)) {
                sjtheme <- sjtheme + theme(legend.key.size = unit(legend.item.size, 
                    "cm"))
            }
            if (!is.null(axis.linecolor)) {
                sjtheme <- sjtheme + theme(axis.line = element_line(colour = axis.linecolor, 
                    size = axis.line.size), axis.line.x = element_line(colour = axis.linecolor.x), 
                    axis.line.y = element_line(colour = axis.linecolor.y))
            }
            if (!is.null(axis.tickscol)) {
                sjtheme <- sjtheme + theme(axis.ticks = element_line(colour = axis.tickscol))
            }
            if (!is.null(axis.tickslen)) {
                sjtheme <- sjtheme + theme(axis.ticks.length = unit(axis.tickslen, 
                    "cm"))
            }
            if (!is.null(axis.ticksmar)) {
                sjtheme <- sjtheme + theme(axis.text = element_text(margin = margin(t = axis.ticksmar, 
                    unit = "cm")))
            }
            if (!is.null(axis.ticksize.x)) {
                sjtheme <- sjtheme + theme(axis.ticks.x = element_line(size = axis.ticksize.x))
            }
            if (!is.null(axis.ticksize.y)) {
                sjtheme <- sjtheme + theme(axis.ticks.y = element_line(size = axis.ticksize.y))
            }
            if (!is.null(plot.col)) {
                sjtheme <- sjtheme + theme(plot.background = element_rect(colour = plot.bordercol, 
                    fill = plot.backcol))
            }
            if (!is.null(panel.col)) {
                sjtheme <- sjtheme + theme(panel.background = element_rect(colour = panel.bordercol, 
                    fill = panel.backcol), panel.border = element_rect(colour = panel.bordercol))
            }
            if (!is.null(panel.gridcol)) {
                sjtheme <- sjtheme + theme(panel.grid.minor = element_line(colour = panel.minor.gridcol, 
                    linetype = panel.minor.linetype), panel.grid.major = element_line(colour = panel.major.gridcol, 
                    linetype = panel.major.linetype))
            }
            if (!is.null(plot.margins)) {
                sjtheme <- sjtheme + theme(plot.margin = plot.margins)
            }
            if (!is.null(plot.margins)) {
                sjtheme <- sjtheme + theme(plot.margin = plot.margins)
            }
            if (!is.null(title.vjust)) {
                sjtheme <- sjtheme + theme(plot.title = element_text(vjust = title.vjust))
            }
            if (!is.null(axis.title.x.vjust)) {
                sjtheme <- sjtheme + theme(axis.title.x = element_text(vjust = axis.title.x.vjust))
            }
            if (!is.null(axis.title.y.vjust)) {
                sjtheme <- sjtheme + theme(axis.title.y = element_text(vjust = axis.title.y.vjust))
            }
            if (!is.null(panel.gridcol.x)) {
                sjtheme <- sjtheme + theme(panel.grid.minor.x = element_line(colour = panel.gridcol.x, 
                    linetype = panel.minor.linetype), panel.grid.major.x = element_line(colour = panel.gridcol.x, 
                    linetype = panel.major.linetype))
            }
            if (!is.null(panel.gridcol.y)) {
                sjtheme <- sjtheme + theme(panel.grid.minor.y = element_line(colour = panel.gridcol.y, 
                    linetype = panel.minor.linetype), panel.grid.major.y = element_line(colour = panel.gridcol.y, 
                    linetype = panel.major.linetype))
            }
            theme_set(sjtheme)
        }
        else {
            warning("Either `theme` or `base` must be supplied as ggplot-theme-object to set global theme options for sjPlot.", 
                call. = F)
        }
        sjPlot:::sj.theme_geoms(geom.alpha, geom.linetype, geom.outline.size, 
            geom.outline.color, geom.boxoutline.size, geom.boxoutline.color, 
            geom.errorbar.size, geom.errorbar.linetype, geom.label.size, 
            geom.label.color, geom.label.alpha, geom.label.angle)
        invisible(sjtheme)
    }
    

    The code for generating your plot is

    my_set_theme(
      geom.outline.color = "antiquewhite4", 
      geom.outline.size = 1, 
      geom.label.size = 2,
      geom.label.color = "grey50",
      title.color = "red", 
      title.size = 1.5, 
      axis.angle.x = 45, 
      axis.textcolor = "blue", 
      axis.hjust.x = 1,         # <- This is the new option !
      base = theme_bw()
    )
    
    plot_grpfrq(
      efc$e42dep, 
      efc$e16sex, 
      title = NULL, 
      geom.colors = c("cadetblue", "coral"), 
      geom.size = 0.4
    )
    

    enter image description here