0

Hi lovely fellow R users,

I have a bar graph that demonstrates the reaction time for two different groups on two item types, starting from 0 ms and ending at 1250 ms.

I now want to break the axis from 0 to 500 to capture better the groups x item differences on reaction time from 700 ms onwards.

Attached you can see an example of an bar graph I want to plot:

Example

How can I achieve that with ggplot2? When I use scale_y_break() nothing happens. I am new to the world of ggplot so any help is welcome.

  • 1
    An option to zoom in on your plot would be to make use of `coord_cartesian(ylim = c(500, NA))`. However, be aware that with bar charts this is probably not a good idea and not recommended as it violates the "rule of proportional ink". – stefan Jul 18 '21 at 18:22
  • 1
    Related question on broken axes, likely duplicate. https://stackoverflow.com/questions/7194688/using-ggplot2-can-i-insert-a-break-in-the-axis – Jon Spring Jul 18 '21 at 23:26
  • Another alternative: https://stackoverflow.com/a/44697832/12957340 – jared_mamrot Jul 19 '21 at 05:22

1 Answers1

3

ggplot2 is an opinionated framework and so it generally does not include options that the creators have discouraged, (I think) including broken axes.

You could get around this by starting your y axis at a higher level, e.g. with the 2nd example below. coord_cartesian lets you specify the "viewport" of what you want to see. This is a little different from scales_y_continuous(limits = ....) which instead filters out data outside the range, and would filter out the bars here which start outside the zoomed in range.

library(ggplot2)
df1 <- data.frame(x = 1:3, y = 1000 + 100*(0:2))

ggplot(df1, aes(x, y)) +
  geom_col()

enter image description here

ggplot(df1, aes(x, y)) +
  geom_col() +
  coord_cartesian(ylim = c(800, NA))

enter image description here

Jon Spring
  • 55,165
  • 4
  • 35
  • 53