2012-09-24 12 views
2

我發現coord_trans,但我想將log10reverse應用於我的x軸。我試過應用兩次轉化在一個軸上應用兩個轉換

ggplot(table) + aes(color=Vowel, x=F1, y=F2) + geom_point() + coord_trans(x="log10", y="log10") + coord_trans(x="reverse", y="reverse") 

但只有第一個應用。所以我試圖鏈接它們

ggplot(table) + aes(color=Vowel, x=F2, y=F1) + geom_point() + coord_trans(x=c("log10", "reverse"), y=c("log10", "reverse")) 

這給了我一個明顯的錯誤。

'c("log10_trans", "reverse_trans")' is not a function, character or symbol 

我該如何鏈接它們?

回答

3

您可以使用trans_new定義新轉換。

library(scales) 
log10_rev_trans <- trans_new(
    "log10_rev", 
    function(x) log10(rev(x)), 
    function(x) rev(10^(x)), 
    log_breaks(10), 
    domain = c(1e-100, Inf) 
) 

p <- ggplot(mtcars, aes(wt, mpg)) + 
    geom_point() 

p + coord_trans(y = log10_rev_trans) 
+0

座標是相反的,但標籤不是。 – Reactormonk

+0

哪些標籤?改變點的順序不會改變軸標籤。你有沒有其他的代碼來標記各個點? –

+0

哦,完全忘了那個。我其實想要反轉軸,而不是點。 – Reactormonk

1

一種快速簡單的方法是將其中一種轉換直接應用於數據,另一種使用繪圖功能。

例如

ggplot(iris, aes(log10(Sepal.Length), log10(Sepal.Width), colour = Species)) + 
geom_point() + coord_trans(x="reverse", y="reverse") 

注意:逆向轉換不適用於虹膜數據,但您明白了。

+0

...它顯示錯誤的標籤。 – Reactormonk

0

我徘徊在這裏尋找'尺度構圖'功能。我認爲人們可能會寫如下的東西:

# compose transforms a and b, applying b first, then a: 
`%::%` <- function(atrans,btrans) { 
    mytran <- scales::trans_new(name  = paste(btrans$name,'then',atrans$name), 
           transform = function(x) { atrans$transform(btrans$transform(x)) }, 
           inverse = function(y) { btrans$inverse(atrans$inverse(y)) }, 
           domain = btrans$domain, # this could use improvement... 
           breaks = btrans$breaks, # not clear how this should work, tbh 
           format = btrans$format) 

} 

ph <- ggplot(mtcars, aes(wt, mpg)) + 
    geom_point() + 
    scale_y_continuous(trans=scales::reverse_trans() %::% scales::log10_trans()) 
print(ph) 
+0

這可能是_should_將'breaks','format'和'domain'組合在一起。 'domain'可能應該是'btrans'域和'atrans'域的b inverse的交集,等等...... –