【发布时间】:2020-07-15 13:23:36
【问题描述】:
对于大多数值在 -10 到 100 之间的数据集,我想跳过部分 y 轴,然后又在 400 处跳过一些。所以我想挤压这个空白区域。我已经在我的情节中为 3 个不同的场景使用了分面网格,所以我宁愿只“挤压”Y 轴而不是创建多个情节。
我在 RPubs (https://rpubs.com/huanfaChen/squash_remove_y_axix_ggplot_) 上找到了“squash_axis”功能,或许可以帮到我。但我无法让它与我自己的数据集一起使用,甚至无法与示例数据集一起使用。
示例数据集(我的看起来很相似,只是有另一列有时间)
dat <- data.frame(group=rep(c('A', 'B', 'C', 'D'), each = 10),
value=c(rnorm(10), rnorm(10)+100)
)
然后是Squash轴函数:
require(ggplot2)
squash_axis <- function(from, to, factor) {
# A transformation function that squashes the range of [from, to] by factor on a given axis
# Args:
# from: left end of the axis
# to: right end of the axis
# factor: the compression factor of the range [from, to]
#
# Returns:
# A transformation called "squash_axis", which is capsulated by trans_new() function
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("squash_axis", trans, inv))
}
以及示例中的情节:
ggplot(dat,aes(x=group,y=value))+
geom_point()+
scale_y_continuous(trans = squash_axis(5, 95, 10))
然后我得到错误: x[isq]
我不明白,因为我的数据中没有 NA,示例数据中也没有。
发生了什么事?
【问题讨论】: