Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
772 views
in Technique[技术] by (71.8m points)

r - GGplot custom scale transformation with custom ticks

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)

enter image description here

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?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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))

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."

Instead, try:

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...