Search code examples
rggplot2facet-grid

How can a facet_grid() be set up so that the y-axis only shows the uppermost value in each instance and nothing in between?


I have a facet_grid set up showing survival rates of people aboard the Titanic based on their titles.

enter image description here

Each row shows a title range (such as "Mr.", "Dr.", etc.).

I have set scales="free" within facet_grid(), so that each y-axis is tailored to the group which it identifies.

However, as there are lots of groups/titles, I want the y-axis on each facet grid to only show only the uppermost value and not everything in between. For example, if 40 people died who were known as "Mr.", then I want the top of the y-axis on the "Mr." facet grid to show the value 40 and nothing else below that. However, if only one person has died, I don't want the y-axis to read: 0.00, 0.25, 0.50, 1.00 as these "in-between" values are not of any value.

Can this be achieved?


Solution

  • try this

    custom_breaks <- function(x) range(x)
    
    ggplot(mpg, aes(displ, cty)) + geom_point() +
      facet_grid( cyl~., scales="free") +
      scale_y_continuous(breaks = custom_breaks)
    

    enter image description here