2012-01-19 36 views
10

我有一些數據顯示幾何關係,但有異常值。例如:無法設置限制與coord_trans

x = seq(0.1, 1, 0.01) 
dat = data.frame(x=x, y=10^x) 
dat[50:60, 2] = 10 

qplot(x, y, data=dat, geom='line') 

enter image description here

我想這個使用對數變換,並同時放大了的部分數據繪製。我知道我可以用coord_trans(y='log10')做第一部分,或者用coord_cartesian(ylim=c(2,8))做第二部分,但我無法將它們結合起來。此外,我需要保持這些點,所以簡單地用scale_y_continuous(limits=c(2,8))剪下它們將不適用於我。

有沒有辦法做到這一點,而不必訴諸以下可怕的黑客?也許是一種無證的方式來通過限制coord_trans

pow10 <- function(x) as.character(10^x) 

qplot(x, log10(y), data=dat, geom='line') + 
    scale_y_continuous(breaks=log10(seq(2,8,2)), formatter='pow10') + 
    coord_cartesian(ylim=log10(c(2,8))) 

enter image description here

+0

我的不好。我以爲我已經解決了這個問題,但我今天只是沒有想到。 – joran

+0

如果您查看'+ .ggplot',則清楚地表明您一次只能應用1個協調原始對象。更多隻是覆蓋舊的。 – James

+0

@詹姆斯當然是。我試圖在coord_trans中設置限制,這很難(不可能?),即使它們很容易分別執行它們... –

回答

4

這可能是一個稍微簡單的解決方法:

library(ggplot2) 

x = seq(0.1, 1, 0.01) 
dat = data.frame(x=x, y=10^x) 
dat[50:60, 2] = 10 

plot_1 = ggplot(dat, aes(x=x, y=y)) + 
     geom_line() + 
     coord_cartesian(ylim=c(2, 8)) + 
     scale_y_log10(breaks=c(2, 4, 6, 8), labels=c("2", "4", "6", "8")) 

png("plot_1.png") 
print(plot_1) 
dev.off() 

enter image description here

+0

是的,這將很好地工作。小蜱也在他們的正確位置。謝謝! –

1

我有同樣的問題,努力去解決它,直到?coord_trans密切關注更多(在ggplot2的v1.0.0中):

使用

coord_trans(xtrans = 「身份」,ytrans = 「身份」,limx = NULL,石灰= NULL)

所以,你可以設置轉換,同時限制時間如下:

ggplot(dat, aes(x=x, y=y)) + geom_line() + 
    coord_trans(ytrans="log10", limy=c(2,8)) 
+0

使用coord_trans層內的限制會影響摘要嗎?或者它們是否像coord_cartesian圖層一樣應用(即純粹限制顯示的範圍)? – JMichael