rggplot2plotgraphvisualization

Problem with settings scale breaks with ggplot2 in R


I'd like to make a line plot for longitudinal data in a design with two groups. Since the group means range from 30-43 I want my plot to start at 28. Nevertheless, I want the scale of the y-axis to start at 0 and insert a break. I found the scale_y_break() function from ggbreak but sadly the output looks weird, since the y-axis ticks are not displayed until 28, there is a lot of spacing added between and a duplicate of the graph is visable.

Here is a reproducible example:

plotdata <- data.frame(
  Group = c("Intervention", "Intervention", "Intervention", "Control", "Control", "Control"),
  Mean = c(30, 35, 43, 31, 33, 34),
  Std_Error = c(0.66779, 0.91275, 1.11335, 0.73653, 0.82869, 0.91173),
  Timepoint = c("Baseline", "6 Weeks", "12 Weeks", "Baseline", "6 Weeks", "12 Weeks")
)

plotdata$Timepoint <- factor(plotdata$Timepoint, levels = c("Baseline", "6 Weeks", "12 Weeks"))

plot <- ggplot(plotdata, aes(x = Timepoint, y = Mean, color = Group, group = Group)) +
  geom_errorbar(aes(ymin = Mean - Std_Error, ymax = Mean + Std_Error), width = 0.1, size = 0.8,  color = "grey60") + 
  geom_line(linewidth = 1.8) +  
  geom_point(size = 2.5) +  
  labs(x = "Timepoint", y = "Outcome", title = "Trajectory") +
  scale_color_manual(values = c("Intervention" = "#D81A61", "Control" = "#1E88E5")) + 
  theme_bw() +  
  theme(
    axis.title.x = element_text(margin = margin(t = 15), face = "italic"),  
    axis.title.y = element_text(margin = margin(r = 15), face = "italic"), 
    axis.text.x = element_text(margin = margin(t = 5)),  
    panel.grid.major.x = element_blank(), 
    panel.grid.minor.x = element_blank(), 
    panel.border = element_blank(), 
    plot.title = element_text(size = 14, face = "bold")
  )

plot
plot +  scale_y_break(
  breaks = c(0, 28)
)  

And the outcome looks like this: Without scale break: enter image description here With scale break: enter image description here

Can Anyone help me out how to fix this? Thank you very much in advance!


Solution

  • I would never do this, but if you must:

    Set your scale_y to include the 0, 45 range and explicitly set the breaks to include 0 as follows

    scale_y_continuous(limits = c(0, 45), breaks  = c(0, seq(25, 45, 5))) +
    

    then use

    plot +  scale_y_break(
      breaks = c(1, 28),
      scales = "fixed"
    )