带有自定义刻度的 GGplot 自定义比例转换 [英] GGplot custom scale transformation with custom ticks

查看:28
本文介绍了带有自定义刻度的 GGplot 自定义比例转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用自定义比例/轴转换,如下所示:

I'm attempting to use a custom scale/axis transformation, like so:

library(ggplot2)
library(scales)

dat <- data.frame(
  time.tot = c(407.17, 168.83, 127.8, 108.88, 69.04, 68.5, 59.76, 407.17, 
               168.83, 127.8, 108.88, 69.04, 68.5, 59.76, 407.17, 168.83, 127.8, 
               108.88, 69.04, 68.5, 59.76),
  error = c(0, 0.01, 0.05, 0.1, 0.5, 0.7, 1, 1.91e-06, 0.00229, 0.00884, 
            0.0172, 0.128, 0.128, 0.22, 1.43e-08, 0.000337, 0.00121, 0.00221, 
            0.0123, 0.0123, 0.0213, 0, 0.01, 0.05, 0.1, 0.5, 0.7, 1, 1.91e-06, 
            0.00229, 0.00884, 0.0172, 0.128, 0.128, 0.22, 1.43e-08, 0.000337, 
            0.00121, 0.00221, 0.0123, 0.0123, 0.0213),
  type = rep(c("A", "B", "C"), each=7))

eps <- 1e-8
tn <- trans_new("logpeps",
                function(x) log(x+eps),
                function(y) exp(y)-eps,
                domain=c(0, Inf),
                breaks=c(0, 0.1, 1))

ggplot(dat, aes(x=time.tot, y=error, color=type)) +
  geom_line() + geom_point() + coord_trans(y = tn)

如您所见,y 轴确实由我指定的函数y=log(x+eps) 进行了转换.breaks 参数虽然没有任何效果.我确定我指定错了,但我无法从文档中弄清楚如何使其工作.有什么建议吗?

As you can see, the y-axis is indeed transformed by my specified function, y=log(x+eps). The breaks argument isn't having any effect though. I'm sure I'm specifying it wrong, but I haven't been able to figure out from the docs how to make it work. Any suggestions?

我还想知道如何/是否修复"曲线 - 看起来它们是按原始比例绘制的,然后使用我的自定义 tn 之后进行转换.

I'm also wondering how/whether to "fix" the curvy lines - looks like they're drawn in the original scale and then transformed using my custom tn afterwards.

推荐答案

你可能需要直接设置y轴的刻度:

You may need to set the scale on the y axis directly:

ggplot(dat, aes(x=time.tot, y=error, color=type)) +
  geom_line() + geom_point() + coord_trans(y = tn)
  + scale_y_continuous(breaks = c(0,0.1,1))

此外,非直线是 coord_trans 的预期行为.来自帮助:coord_trans 与比例变换不同,它发生在统计变换之后,会影响几何图形的视觉外观 - 不能保证直线会继续保持直线."

Also, the non-straight lines are the expected behavior of coord_trans. from the help: "coord_trans is different to scale transformations in that it occurs after statistical transformation and will affect the visual appearance of geoms - there is no guarantee that straight lines will continue to be straight."

相反,请尝试:

b <- 10^-c(Inf, 8:0)
ggplot(dat, aes(x=time.tot, y=error, color=type)) +
  geom_line() + geom_point() + scale_y_continuous(breaks = b, labels=b, trans = tn)

这篇关于带有自定义刻度的 GGplot 自定义比例转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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