R / ggplot2: collapse or remove the Y-axis segment from the scatter plot

I am trying to make a scatter plot in R with ggplot2, where the middle of the y axis is minimized or deleted because there is no data. I did this in Photoshop below, but is there a way to create a similar plot with ggplot? This is continuous scale data:enter image description here

But I'm trying to do something like this: enter image description here

Here is the code:

ggplot(data=distance_data) +
    geom_point(
        aes(
            x = mdistance,
            y = maxZ,
            shape = factor(subj),
            color = factor(side),
            size = (cSA)
        )
    ) +
    scale_size_continuous(range = c(4, 10)) +
    theme(
        axis.text.x = element_text(colour = "black", size = 15),
        axis.text.y = element_text(colour = "black", size = 15),
        axis.title.x = element_text(colour = "black", size= 20, vjust = 0),
        axis.title.y = element_text(colour = "black", size= 20),
        legend.position = "none"
    ) +
    ylab("Z-score") +
    xlab("Distance")
+4
source share
1 answer

You can do this by specifying a coordinate transformation. A standard example is the logarithmic coordinates that can be reached in ggplotwith scale_y_log10().

, trans scale_y_continuous() ( scale_x_continuous()). trans_new() scales. .

OP, , .

OP

OP -2 2. ( ), 4 :

library(scales)
trans <- function(x) {
  ifelse(x > 2, x - 1.5, ifelse(x < -2, x + 1.5, x/4))
}
inv <- function(x) {
  ifelse(x > 0.5, x + 1.5, ifelse(x < -0.5, x - 1.5, x*4))
}
my_trans <- trans_new("my_trans", trans, inv)

. , :

x_val <- 0:250
y_val <- c(-6:-2, 2:6)
set.seed(1234)
data <- data.frame(x = sample(x_val, 30, replace = TRUE),
                   y = sample(y_val, 30, replace = TRUE))

:

p <- ggplot(data, aes(x, y)) + geom_point()
p + scale_y_continuous(breaks = seq(-6, 6, by = 2))

enter image description here

scale_y_continuous() :

p + scale_y_continuous(trans = my_trans,
                       breaks = seq(-6, 6, by = 2))

enter image description here

, trans() inv() trans_new(). , inv() inv(). :

x <- runif(100, -100, 100)
identical(x, trans(inv(x)))
## [1] TRUE

, , , , . trans, scale_y_continuous:

library(scales)
squish_trans <- function(from, to, factor) {

  trans <- function(x) {

    # get indices for the relevant regions
    isq <- x > from & x < to
    ito <- x >= to

    # apply transformation
    x[isq] <- from + (x[isq] - from)/factor
    x[ito] <- from + (to - from)/factor + (x[ito] - to)

    return(x)
  }

  inv <- function(x) {

    # get indices for the relevant regions
    isq <- x > from & x < from + (to - from)/factor
    ito <- x >= from + (to - from)/factor

    # apply transformation
    x[isq] <- from + (x[isq] - from) * factor
    x[ito] <- to + (x[ito] - (from + (to - from)/factor))

    return(x)
  }

  # return the transformation
  return(trans_new("squished", trans, inv))
}

:

p + scale_y_continuous(trans = squish_trans(-2, 2, 4),
                       breaks = seq(-6, 6, by = 2))

, , :

df <- data.frame(class = LETTERS[1:4],
                 val = c(1, 2, 101, 102))
ggplot(df, aes(x = class, y = val)) + geom_bar(stat = "identity") +
  scale_y_continuous(trans = squish_trans(3, 100, 50),
                     breaks = c(0, 1, 2, 3, 50, 100, 101, 102))

enter image description here

, , : !

+6

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


All Articles