我正在尝试使用自定义比例/轴转换,如下所示:
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
论点虽然没有任何效果。我确定我指定的错误,但是我无法从文档中找出如何使其工作。有什么建议?
我还想知道如何/是否“修复”弯曲的线条-看起来它们是按原始比例绘制的,然后使用我的自定义样式进行转换tn
。
您可能需要直接在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)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句