使用自定义刻度的GGplot自定义比例转换

7

我试图使用自定义比例尺/轴变换,如下:

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)

enter image description here

如您所见,y轴确实被我指定的函数y=log(x+eps)转换了。不过,breaks参数没有起到任何作用。我确定我指定有误,但是我无法从文档中找出如何使其工作。有什么建议吗?
我还想知道如何/是否“修复”曲线 - 看起来它们是在原始比例尺上绘制,然后在使用我的自定义tn后进行转换。
1个回答

4
你可能需要直接设置y轴的比例尺:
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不同于比例尺变换,因为它发生在统计变换之后,并且会影响几何对象的视觉外观——无法保证直线将继续保持直线状态。”

相反,可以尝试使用:

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)

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接