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))

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

, trans() inv() trans_new(). , inv() inv(). :
x <- runif(100, -100, 100)
identical(x, trans(inv(x)))
, , , , . trans, scale_y_continuous:
library(scales)
squish_trans <- function(from, to, factor) {
trans <- function(x) {
isq <- x > from & x < to
ito <- x >= to
x[isq] <- from + (x[isq] - from)/factor
x[ito] <- from + (to - from)/factor + (x[ito] - to)
return(x)
}
inv <- function(x) {
isq <- x > from & x < from + (to - from)/factor
ito <- x >= from + (to - from)/factor
x[isq] <- from + (x[isq] - from) * factor
x[ito] <- to + (x[ito] - (from + (to - from)/factor))
return(x)
}
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))

, , : !