2013-04-03 99 views
1

我在GGPLOT2(通過rpy2),其格式上的log 2刻度x軸的曲線圖:指定ggplot2中的刻度標籤的格式而不使用刻度進行變換?

p += ggplot2.scale_x_continuous(trans=scales.log2_trans(), 
            breaks=scales.trans_breaks("log2", 
                   robj.r('function(x) 2^x'), 
                   n=8), 
            labels=scales.trans_format("log2", robj.r('math_format(2^.x)'))) 

如果x值已經在LOG 2,我如何才能申請的scales格式轉化,使值以2^x的格式顯示,而不是十進制的log2值?即如果我放棄trans=參數,我怎樣才能正確地格式化蜱?

+1

也許用''trans_breaks''中的''identity'替換''log2''?雖然它可能會更容易,只是爲了不轉換它們,讓ggplot完成所有這些工作。 – joran 2013-04-03 19:47:17

回答

1

猜測這math_format()scales(不能檢查它現在),並根據Brian的回答rpy2版本應該是以下幾點:

from rpy2.robjects.lib import ggplot2 
from rpy2.robjects.packages import importr 
scales = importr("scales") 
p = ggplot2.ggplot(mtcars) + \ 
     ggplot2.aes_string(x="wt", y="mpg")) + \ 
     ggplot2.geom_point() + \ 
     ggplot2.scale_x_continuous(labels = scales.math_format("2^.x")) 
p.plot() 
2

我可以在純R中給出答案,但我不知道rpy2能夠翻譯它。

實際上,您只需指定控制標籤顯示方式的參數labels;請勿更改影響整體縮放比例以及中斷出現位置的參數transbreaks。使用mtcars爲例:

library("ggplot2") 
library("scales") 
ggplot(mtcars, aes(wt, mpg)) + 
    geom_point() + 
    scale_x_continuous(labels = math_format(2^.x)) 

enter image description here

(顯然,這是沒有意義的,因爲重量是不是已經對數底2的規模,但這個概念的作品。)