Remove last label and slash character "/" from coord_polar x axis labels

Viewed 102

This is bugging me slightly - how can I remove this unwanted label including the /. Simply sth like c("", my actual labels) doesn't help because the / still remains.

I thought maybe scale_x_date might help, but this actually draws the entire date range and doesn't give the desired "by month" figure.

library(ggplot2)
# modified from mdeaths data set
lungdeath <-
  structure(list(deaths = c( 2134, 1863, 1877, 1877, 1492, 1249, 1280, 1131, 1209, 1492, 1621, 1846, 1846
  ), month = c( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 0
  ), year = c( 1974, 1974, 1974, 1974, 1974, 1974, 1974, 1974, 1974, 1974, 1974, 1974, 1974
  )), row.names = c( "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "121"
  ), class = "data.frame")

ggplot(lungdeath) +
  geom_line(aes(month, deaths)) +
  coord_polar() +
  scale_x_continuous(breaks = 0:12,  labels = c("undesired",1:12))

Created on 2021-04-21 by the reprex package (v2.0.0)

2 Answers

Within scale_x_continuous() change the breaks to 1:12:

ggplot(lungdeath) +
  geom_line(aes(month, deaths)) +
  coord_polar() +
  scale_x_continuous(breaks = c(1:12))

enter image description here

Probably overthinking this a bit; but it seems this is hardcoded in the render_fg of the CoordPolar class. Writing your own subclass with a space as separator (changes noted in code comments):

library(ggplot2)
library(grid)

# Constructor
alt_coord_polar <- function(...) {
  polar_coord <- coord_polar(...)
  # Very ad-hoc changes to the previous class
  coord <- ggproto(
    NULL, polar_coord,
    render_fg = function(self, panel_params, theme) {
      if (is.null(panel_params$theta.major)) {
        return(element_render(theme, "panel.border"))
      }
      
      theta <- ggplot2:::theta_rescale(self, panel_params$theta.major, 
                                       panel_params)
      labels <- panel_params$theta.labels
      
      theta <- theta[!is.na(theta)]
      ends_apart <- (theta[length(theta)] - theta[1]) %% (2*pi)
      if (length(theta) > 0 && ends_apart < 0.05 && !is.null(labels)) {
        n <- length(labels)
        if (is.expression(labels)) {
          combined <- substitute(paste(a, " ", b), # <- changed middle
                                 list(a = labels[[1]], b = labels[[n]]))
        } else {
          combined <- paste(labels[1], labels[n], sep = " ") # <- changed sep
        }
        labels[[n]] <- combined
        labels <- labels[-1]
        theta <- theta[-1]
      }
      
      grobTree(
        if (length(labels) > 0) element_render(
          theme, "axis.text.x",
          labels,
          unit(0.45 * sin(theta) + 0.5, "native"),
          unit(0.45 * cos(theta) + 0.5, "native"),
          hjust = 0.5, vjust = 0.5
        ),
        element_render(theme, "panel.border")
      )
    }
  )
}

Then using your own custom polar coordinates:

lungdeath <-
  structure(list(deaths = c( 2134, 1863, 1877, 1877, 1492, 1249, 1280, 1131, 1209, 1492, 1621, 1846, 1846
  ), month = c( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 0
  ), year = c( 1974, 1974, 1974, 1974, 1974, 1974, 1974, 1974, 1974, 1974, 1974, 1974, 1974
  )), row.names = c( "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "121"
  ), class = "data.frame")

ggplot(lungdeath) +
  geom_line(aes(month, deaths)) +
  alt_coord_polar() +
  scale_x_continuous(breaks = 0:12,  labels = c("undesired",1:12))

Created on 2021-04-21 by the reprex package (v1.0.0)

Related