A_Schoenberger
A_Schoenberger

Reputation: 1

How to break Y axis of a ggplot bar graph

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.

Upvotes: 0

Views: 1302

Answers (1)

Jon Spring
Jon Spring

Reputation: 66415

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

Upvotes: 3

Related Questions