rggplot2customizationaxis-labels

Transposing labels on a continuous scale [waterfall graph, R]


I am customizing a waterfall graph and am almost at the end result, except for the labels on what is now the x-axis (after coord_flip()). My now x-axis simply sums to 100% and, while using scale_y_reverse() after coord_flip() has helped me keep the bar hierarchy intended, it set the scale at limits = c(100, 0) instead of what I wanted to still keep limits = c(0, 100). This is the correct way to handle it, except that for my use it does not matter as I am only trying to visualize increments in percentages.

Here is a reprex:

data_fig <- data.frame(class = rev(c("Class A", "Class B", "Class C", "Other Classes")),
                       values = rev(c(15, 25, 50, 10)))

waterfalls::waterfall(
    .data = data_fig,
    rect_width = 0.7, 
    fill_by_sign = FALSE,
    put_rect_text_outside_when_value_below = FALSE,
    rect_border = NA,
    total_rect_border_color = NA,
    draw_axis.x = "none") +
    coord_flip() +
    scale_y_reverse()

What I have tried:

I appreciate any ideas of how to keep limits = (0, 100) and the remaining of the graph elements as is. Thank you very much in advance!


Solution

  • Specifying breaks and labels in reverse order to each other seems to generate the result you are after:

    waterfalls::waterfall(
      .data = data_fig,
      rect_width = 0.7, 
      fill_by_sign = FALSE,
      put_rect_text_outside_when_value_below = FALSE,
      rect_border = NA,
      total_rect_border_color = NA,
      draw_axis.x = "none") +
      coord_flip() +
      scale_y_reverse(breaks=c(100, 75, 50, 25, 0), labels=c(0, 25, 50, 75, 100))
    

    waterfall with correct x-axis labels