1

ggplot

I'm trying to limit my ggplot output to only the range of interest and exclude the extraneous grey margin on either side of the heatmap. What argument should I add to my function?

heatmap <- function(df, .x_var, .y_var, .z_var) {

  x_var <- sym(.x_var)
  y_var <- sym(.y_var)
  z_var <- sym(.z_var)

  ggplot(df, aes(x = !! x_var, y = !! y_var, fill = !! z_var)) + 
    geom_tile(colour = "black") +
    scale_x_discrete(limits = c(2008:2018)) +
    theme(axis.text.y = element_text(size = 6))
}
  • 2
    Duplicate: [How to remove space between axis & area-plot in ggplot2?](https://stackoverflow.com/questions/22945651/how-to-remove-space-between-axis-area-plot-in-ggplot2) – tjebo Jan 22 '20 at 17:09

2 Answers2

5

Adding scale_x_discrete(expand = c(0,0)) to the plot should do the trick.

0

Inserted Jonathan's expand = c(0,0) option into scale_x_discrete

  heatmap <- function(df, .x_var, .y_var, .z_var) {

      x_var <- sym(.x_var)
      y_var <- sym(.y_var)
      z_var <- sym(.z_var)

  ggplot(df, aes(x = !! x_var, y = !! y_var, fill = !! z_var)) + 
    geom_tile(colour = "black") +
    scale_x_discrete(limits = c(2008:2018), expand = c(0,0)) +
    theme(axis.text.y = element_text(size = 6))
}