ggbreak scale_y_break with a standard-error going below 0 is changing the y axis of the graph

86 Views Asked by At

My ggplot regression graph has multiple 0 values that makes the standard error going below 0 at the maximum x. It can be true with my model so the negative values are not wanted and my y axis lowermost value is 0.

library(ggplot)
library(ggbreak)

x_values <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13)

y_values <- c(40, 12, 11, 10, 9, 9, 9, 8, 7.5, 0, 0, 0, 1)

custom_data <- data.frame(x = x_values, y = y_values)


graph <- ggplot(custom_data, aes(x = x_values, y = y_values)) +
    coord_cartesian(ylim = c(0, 45)) +
    scale_y_continuous(breaks = seq(0, 50,  5)) +
    geom_point(size = 2, colour= "blue") +
    stat_smooth(formula = y ~ x, method = "lm", col = "black", se = TRUE) +
    labs(x = "Distance à la source", y = "Nombre de colonies") +
    theme_classic() 

I tried to use scale_y_break to reduce space between an "outlier point in my ggplot graph. It correctly removed my space in axis y in the defined range, but it moved up my regression in the Y axis so we see the lower values of the standard error. It make the function totaly inefficient in my case because I end up having a correctly Y break but a general messed Y axis. Is there a solution to keap my Y axis at 0 in the bottom down of my graph?

(graph <- graph + scale_y_break(c(30, 35)) )

I tried the ggplot stat_smooth correction to ajust the ylim=0 but the ggbreak object is not ggplot anymore after the scale_y_break function applied.

1

There are 1 best solutions below

1
On BEST ANSWER

I've found the solution here on stack: Plotly-R: How to make a gapped y axis?

jamesjin63 suggests to divide the figure axis in 2 and to combine them together thereafter.