2012-02-10 75 views
12

我寫了下面的代碼,使劇情如何使scale_y_log10有刻度線在0.01,0.1,1在ggplot

pd<- position_dodge(.2) # # move them .05 to the left and right 
pm25 <- ggplot(data, aes(x=CombSEG, y=conc,shape=A,color=A, lty=A,  group=A)) + 
geom_point() + 
geom_line() + 
geom_errorbar(aes(ymin=conc-se, ymax=conc+se), 
       width=.1, position=pd) + 
theme_bw()+ 
limits(c(0 
scale_y_log10(breaks=c(0.01,0.1,1),labels=c(0.01,0.1,1)) 

自動規模休息是10^-1.8,10^-1.6, 10^-1.4 ... 10^-0.4。我實際上喜歡最低的刻度爲0.01,最高的刻度爲1.

謝謝你的幫助。

編輯:這是我試過你的代碼後的情節。 enter image description here

+0

可不可以給一個小的可重複的例子(即數據)?即使你只是做了一些模擬你的問題? – 2012-02-10 05:26:07

回答

12

使用的scale_y_log10breakslabels參數(閱讀它們here)。

# make up some sample data 
df <- data.frame(x=1:100,y=10^-(2*runif(100))) 
ggplot(df,aes(x=x,y=y)) + geom_point() + scale_y_log10() 

的樣子: enter image description here

然後修改日誌10規模定製處斷裂0.01,0.1和1,使用breaks參數:

ggplot(df,aes(x=x,y=y)) + geom_point() + scale_y_log10(breaks=c(.01,.1,1)) 

看起來像: enter image description here

最後,如果你想標籤也是0.1,1和1,使用labels說法:

ggplot(df,aes(x=x,y=y)) + geom_point() + 
      scale_y_log10(breaks=c(.01,.1,1),labels=c(.01,.1,1)) 

enter image description here

+0

我嘗試了完全相同的代碼,但只有0.1出現在中間。這裏是數據的最大值和最小值 Min。 :0.01504 中位數:0.03957 平均值:0.06148 最大。 :0.43815 – Amateur 2012-02-11 01:11:58

+0

我有另外一組數據,其中Min = 0.1,Max = 0.7,並且在比例尺末尾出現的只有0.1。 – Amateur 2012-02-11 01:17:46

+2

@TranHuynh如果您的數據範圍從0.1到0.7,並且您手動將間隔設置爲0.01,0.1和1,那麼爲什麼您可能期望看到除0.1之外的任何一個?如果你按照要求完成了並提供了一個可重複的例子,我們可以從一開始就告訴你也將'limits = c(0,1)'傳遞給'scale_y_log10'。我們不介意讀者。 – joran 2012-02-11 03:03:22

14

使用coord_trans()而不是規模()

df <- data.frame(x=1:100,y=10^-(2*runif(100))) 
ggplot(df,aes(x=x,y=y)) + geom_point() + coord_trans(y = "log10") 

enter image description here

相關問題