ggbreak shifts axis labelling

381 Views Asked by At

When using ggbreak together with ggplot2, the axis labels are shifted. When plotting the graph without inserting a break, the x-axis label is centered on the axis and the y-axis label is close to the y-axis. After introducing the break, the x-axis label shifts to the right, such that it is positioned in the middle of the whole graph including the legend. The y-axis label is moved away from the axis. Both seem to be caused by an introduction of a frame around the whole graph, which can be made invisible, but whose effec remains.

See the following MWE

library(ggplot2)
library(ggbreak)

theme_set(theme_light())

plot <- ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, color=Species)) +
  geom_point()
plot #without using ggbreak

plot_withbreak <- ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, color=Species))+
  geom_point()+
  scale_y_break(c(3.5, 3.7))
plot_withbreak #with ggbreak

plot_withbreak + theme_update(
  panel.border = element_blank()
) #removes additional frame, but problem persists.
2

There are 2 best solutions below

1
On

We can organize it by hand:

library(ggplot2)
library(ggbreak)

theme_set(theme_light())

ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, color=Species)) +
  geom_point()


ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, color=Species))+
  geom_point()+
  scale_y_break(c(3.5, 3.7)) +
  theme(plot.margin = margin(5.5, 5.5, 5.5, 5.5, "mm"),legend.position = "top") 

enter image description here

1
On

Not a real fix for the ggbreak issue. But a workaround to achieve your desired result would be to use vanilla ggplot2 and facet_grid:

library(ggplot2)

p <- ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, color=Species))+
  geom_point()+
  theme_light()

p +
  facet_grid(Sepal.Width < 3.7~., scales = "free_y", space = "free_y") +
  theme(strip.text.y = element_blank())