Changing axial discontinuities without sequencing - ggplot

Is there a way to set the gap step size in ggplot without determining the sequence. For instance:

x <- 1:10
y <- 1:10

df <- data.frame(x, y)

# Plot with auto scale
ggplot(df, aes(x,y)) + geom_point()

# Plot with breaks defined by sequence
ggplot(df, aes(x,y)) + geom_point() +
  scale_y_continuous(breaks = seq(0,10,1))

# Plot with automatic sequence for breaks
ggplot(df, aes(x,y)) + geom_point() +
  scale_y_continuous(breaks = seq(min(df$y),max(df$y),1))

# Does this exist?
ggplot(df, aes(x,y)) + geom_point() +
  scale_y_continuous(break_step = 1)

You can say that I'm lazy, but there were several occasions when I had to change the limits minand maxmy seqdue to the addition of error bars. So I just want to say ... use the fault size x, with automatic scale limits.

+4
source share
1 answer

You can define your own function to jump to the breaks argument. An example that will work in your case would be

f <- function(y) seq(floor(min(y)), ceiling(max(y)))

Then

ggplot(df, aes(x,y)) + geom_point() + scale_y_continuous(breaks = f)

gives

enter image description here

You can change this to go through the gap step, for example.

f <- function(k) {
        step <- k
        function(y) seq(floor(min(y)), ceiling(max(y)), by = step)       
}

then

ggplot(df, aes(x,y)) + geom_point() + scale_y_continuous(breaks = f(2))

y 2, 4,.., 10 ..

,

my_scale <- function(step = 1, ...) scale_y_continuous(breaks = f(step), ...)

ggplot(df, aes(x,y)) + geom_point() + my_scale()

, , :)

+3

Source: https://habr.com/ru/post/1651794/


All Articles