ggplot上的混合比例 [英] Mixed scale on a ggplot

查看:62
本文介绍了ggplot上的混合比例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想绘制一个图(最好使用ggplot2),其中x轴的缩放比例不同. 更准确地说,我希望我的量表的对数范围从大约0.001到0.05,非对数范围从0.05到1.0

I would like to make a plot (preferably with ggplot2), where the x-axis has different scaling. More precisely I want my scale to be logarithmic from approximately 0.001 to 0.05 and non-logaritmic from 0.05 to 1.0

我当前的情节代码是:

ggplot(DF, aes(x=DF$RAW.PVAL, col=DF$sample))+stat_ecdf()+geom_abline()+xlim(0,1)+ylim(0,1)+xlab("P Value")+ylab("Frequecy")+ggtitle("Type I error control")+labs(colour="Sample")

在这里您可以看到结果:

And here you can see the result:

Type1ErrorControl

反正我能做到吗?我想,我可以定义一个新的转换,但是我没有找到有关执行此操作的任何说明.

Is there anyway I can achieve this? I suppose, that I could define a new transformation, but I didn't find any instructions on how to do this.

谢谢您!

推荐答案

您可以创建自己的比例尺,请参见(

You can create your own scale, see (1,2) for other examples. Here is an example for your particular discontinuous function.

library(scales)
library(ggplot2)

MixLogF <- function(x){ 
    if(x < 0.05){r <- log(x) - log(0.05) + 0.05}
    else {r <- x}
    return(r) 
}
MixLogV <- Vectorize(MixLogF)
InvMixLogF <- function(x){ 
    if(x < 0.05){r <- exp(x - 0.05 + log(0.05))}
    else {r <- x}
    return(r)
}
InvMixLogV <- Vectorize(InvMixLogF)
MixLogV_trans <- function() trans_new("MixLogV",MixLogV,InvMixLogV,domain = c(0.001, Inf))

y <- (1:100)/100
x <- MixLogV(y)
ExpDat <- data.frame(x,y)
orig <- ggplot(data=ExpDat, aes(x=y,y=y)) + geom_point()
orig
orig + scale_x_continuous(trans="MixLogV", limits=c(0.01, 1), breaks=c(0.01,0.02,0.03,0.04,0.05,0.30,0.80))

同意eipi10,谨慎使用-可能会产生误导.

Agree with eipi10, use with care - could be misleading.

出于标记目的,通过在绘图空间中具有相等的间隔,可以更轻松地查看函数对比例的影响.下面的示例显示,上部非常紧凑-p值不能超过1,在0.05处折断时,p值与exp(log(0.05) - 0.95)〜0.02相同.

For labeling purposes, it may be easier to see the effects the function has on the scale by having equal breaks in the plot space. The example below shows that the upper part is quite squished - you can't have a p-value over 1, which when breaking at 0.05 is the same distance away as exp(log(0.05) - 0.95) ~ 0.02.

#For nice even breaks
blog <- c(0.01,0.02,0.03,0.04)
blin <- 0.05 + log(0.05) - log(rev(blog))
orig + scale_x_continuous(trans="MixLogV", limits=c(0.01, blin[4]), 
       breaks=c(blog,0.05,blin), labels = format(c(blog,0.05,blin),digits=2,scientific = FALSE))

这篇关于ggplot上的混合比例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆