R 分割y轴以生成带有分数的零件<;25人在这一地区占多数

R 分割y轴以生成带有分数的零件<;25人在这一地区占多数,r,ggplot2,yaxis,R,Ggplot2,Yaxis,我想分割y轴,以便附图参与评分,这里是我能得到的最接近的地方,得到一个假轴打断,并调整绘图上部区域的大小。我仍然认为这是一个坏主意,如果这是我的情节,我更喜欢更直接的轴变换 首先,我们需要一个生成转换的函数,该转换将所有值压缩到某个阈值以上: library(ggplot2) library(scales) # Define new transform my_transform <- function(threshold = 25, squeeze_factor = 10) { fo

我想分割y轴,以便附图参与评分,这里是我能得到的最接近的地方,得到一个假轴打断,并调整绘图上部区域的大小。我仍然认为这是一个坏主意,如果这是我的情节,我更喜欢更直接的轴变换

首先,我们需要一个生成转换的函数,该转换将所有值压缩到某个阈值以上:

library(ggplot2)
library(scales)

# Define new transform
my_transform <- function(threshold = 25, squeeze_factor = 10) {
  force(threshold)
  force(squeeze_factor)
  my_transform <- trans_new(
    name = "trans_squeeze",
    transform = function(x) {
      ifelse(x > threshold, 
             ((x - threshold) * (1 / squeeze_factor)) + threshold, 
             x) 
    },
    inverse = function(x) {
      ifelse(x > threshold, 
             ((x - threshold) * squeeze_factor) + threshold, 
             x)
    }
  )
  return(my_transform)
}


您可能会发现,在不连续的轴上阅读会提供信息。人们有时会出现奇怪的y轴。

ggplot2不支持轴断开(出于我同意的原因),但您是否考虑过使用log或sqrt变换y轴?@teunbrand感谢您的输入。那么,有没有办法在另一个包中生成相同的图呢?我的资深同事不同意这种转变。坦克很多很多。这很有效。感谢你的努力和时间。向上投票。
library(ggplot2)
library(scales)

# Define new transform
my_transform <- function(threshold = 25, squeeze_factor = 10) {
  force(threshold)
  force(squeeze_factor)
  my_transform <- trans_new(
    name = "trans_squeeze",
    transform = function(x) {
      ifelse(x > threshold, 
             ((x - threshold) * (1 / squeeze_factor)) + threshold, 
             x) 
    },
    inverse = function(x) {
      ifelse(x > threshold, 
             ((x - threshold) * squeeze_factor) + threshold, 
             x)
    }
  )
  return(my_transform)
}
ggplot(data, aes(Year, Score)) +
  geom_point(color = "grey", shape = 21, size = 3) +
  geom_smooth(method = "loess", fill = "lightgray") +
  # Add fake axis lines
  annotate("segment", x = -Inf, xend = -Inf,
           y = c(-Inf, Inf), yend = c(24.5, 25.5)) +
  # Apply transform to y-axis
  scale_y_continuous(trans = my_transform(25, 10),
                     breaks = seq(0, 80, by = 10)) +
  scale_x_continuous(limits = c(1980, 2020), oob = oob_keep) +
  theme_classic() +
  # Turn real y-axis line off
  theme(axis.line.y = element_blank())