在ggplot中压缩轴

问题描述 投票:0回答:3

我有以下情节

plot

p1<-ggplot(data.frame(x=c(-10, 30),y=c(0,250)), aes(x,y))
p1<-p1 +stat_function(fun=function(x)12+180/(1+exp(-.759*(x-7.69))),size = 2,color="yellow")+coord_cartesian(ylim=c(0, 250))+geom_abline(intercept = 44, slope = 0,lty=2)+scale_x_continuous(limits=c(-5,25))+ geom_ribbon(aes(ymin=0, ymax=80))
print(p1)   

我正在尝试更改轴,以便虚线上方的部分被“压扁”,虚线下方的部分被扩展(但不改变轴)。换句话说,我希望图下部的比例更宽,上部的比例更窄,以便图的虚线下方的部分更突出地显示。但是,我似乎不知道如何做到这一点。有什么建议吗?

谢谢。

r ggplot2
3个回答
4
投票

我希望我正确理解了您的需求。

我不知道@42-评论是什么,但我认为坐标转换绝对是正确的选择。

library(scales)
magnify_trans <- function(intercept, reducer) {

    trans <- function(x, i = intercept, r = reducer) {
        sapply(x, function(x) {
            if (x < i) x
            else x / r + i
        })
    }

    inv <- function(x, i = intercept, r = reducer) {
        sapply(x, function(x) {
            if(!is.na(x)) {
                if (x < i) x
                else (x - i) * r
            }
        })
    }

    trans_new(name = 'custom',
              transform = trans,
              inverse = inv
              )
}

我们定义了一个

transformation
,它将
intercept
以上的值除以
reducer
arg。

library(ggplot2)
ggplot(data.frame(x = c(-10, 30), y = c(0, 250)), aes(x, y)) +
    stat_function(fun = function(x) 12 + 180 / (1 + exp(-.759*(x - 7.69))),
                  size = 2,
                  color = "yellow") +
    coord_cartesian(ylim = c(0, 250))+
    geom_abline(intercept = 44, slope = 0, lty = 2)+
    scale_x_continuous(limits = c(-5, 25)) +
    coord_trans(y = magnify_trans(intercept = 44, reducer = 20))

如果您想要的话,可以在 x 轴上使用相同的变换:

ggplot(data.frame(x = c(-10, 30), y = c(0, 250)), aes(x, y)) +
    stat_function(fun = function(x) 12 + 180 / (1 + exp(-.759 * (x - 7.69))),
                  size = 2,
                  color = "yellow") +
    coord_cartesian(ylim = c(0, 250))+
    geom_abline(intercept = 44, slope = 0, lty = 2)+
    scale_x_continuous(limits = c(-5, 25)) +
    coord_trans(x = magnify_trans(intercept = 5.675, reducer = 20))


1
投票

这是对区间外压缩的概括。我希望它对某人有用。

magnify_trans <- function(interval_low = 0, interval_high = 7,  reducer = 20) {

    trans <- function(x, i_low = interval_low, i_high = interval_high, r = reducer) 
        sapply(x, function(x) {
            if(x >= i_low & x <= i_high ) x
            else if(x < i_low) x / r + i_low
            else (x - i_high) / r + i_high
        })

    inv <- function(x, i_low = interval_low, i_high = interval_high, r = reducer) 
        sapply(x, function(x) {
            if(x >= i_low & x <= i_high ) x
            else if(x < i_low) (x - i_low) * r
            else ((x - i_high) * r) + i_high
        })

    trans_new(name = 'custom',  transform = trans,inverse = inv )
}

0
投票

我使用自定义功能

magnify_trans
。但是,它返回一个错误。

>   ggplot(data.frame(x = c(-10, 30), y = c(0, 250)), aes(x, y)) +
+     stat_function(fun = function(x) 12 + 180 / (1 + exp(-.759*(x - 7.69))),
+                   size = 2,
+                   color = "yellow") +
+     coord_cartesian(ylim = c(0, 250))+
+     geom_abline(intercept = 44, slope = 0, lty = 2)+
+     scale_x_continuous(limits = c(-5, 25)) +
+     coord_trans(y = magnify_trans(intercept = 44, reducer = 20))   
Coordinate system already present. Adding new coordinate system, which will replace the existing one.
Error in UseMethod("rescale") : 
  no applicable method for 'rescale' applied to an object of class "list"

我的ggplot2版本是3.5.0。 如何修复这个错误?

© www.soinside.com 2019 - 2024. All rights reserved.