2015-09-04 74 views
2

我有一個數據集,我聚集和繪圖:在ggplot中使用scale_y_log10的stat_summary中的錯誤?

d <- # set d to the database below. 

agg <- aggregate(wt ~ t, data=d, FUN=mean) 

# example 1 
ggplot(agg, aes(x=t, y=wt)) + geom_line(size = 1.5) 

# example 1log 
ggplot(agg, aes(x=t, y=wt)) + geom_line(size = 1.5) + scale_y_log10() 

# example 2 
ggplot(d, aes(x=t, y=wt)) + stat_summary(fun.y="mean", geom="line", size = 1.5) 

# example 2log 
ggplot(d, aes(x=t, y=wt)) + stat_summary(fun.y="mean", geom="line", size = 1.5) + 
    scale_y_log10() 

例2:

Example 2

例2log:

Example 2log

的問題是,即使盡管示例1和2相同,但示例1log和2log是不同的並且例如e 2log甚至完全不符合實例2。

我做錯了什麼或這是一個錯誤?

我需要使用示例2log,因爲我想用不同的條件進行聚合,例如,

ggplot(data, aes(x=t, y=wt)) + 
    stat_summary(data=subset(data, dim == 6 & maxt == 32 & max_trials == 10000 & t > 2), fun.y="mean", geom="line", color="black", size = 1.5) + 
    stat_summary(data=subset(data, dim == 6 & maxt == 16 & max_trials == 1000 & t > 2), fun.y="mean", geom="line", color="black", size = 1.5) + scale_y_log10() 

這是我使用的數據集和再現的錯誤,通過write.table(d, "test.dat")作爲出口:

"wt" "t" 
"7" 12 3 
"9" 18 4 
"11" 28 6 
"13" 14 7 
"15" 81 9 
"21" 97 10 
"23" 3 11 
"25" 12 12 
"28" 46 13 
"35" 1296 15 
"37" 63 16 
"39" 43 17 
"41" 88 18 
"43" 395 19 
"45" 512 20 
"47" 154 21 
"49" 9 22 
"51" 83 23 
"53" 5 24 
"55" 1606 25 
"57" 3838 26 
"59" 1331 27 
"74" 23 3 
"76" 20 4 
"81" 79 5 
"83" 32 6 
"85" 14 7 
"88" 24 8 
"89" 9 9 
"93" 67 10 
"97" 44 11 
"98" 18 12 
"99" 101 13 
"100" 17 14 
"101" 19 16 
"102" 41 18 
"103" 9 19 
"105" 26 20 
"108" 76 21 
"109" 2 22 
"113" 883 23 
"116" 2054 24 
"137" 16 3 
"139" 26 4 
"140" 4 5 
"144" 15 6 
"145" 5 7 
"150" 31 8 
"155" 49 11 
"168" 5700 12 
"173" 12 3 
"176" 40 6 
"181" 89 7 
"182" 2 8 
"183" 4 9 
"184" 5 10 
"186" 35 11 
"194" 357 12 
"195" 13 13 
"208" 2544 14 
"209" 83 15 
"210" 168 16 
"211" 313 17 
"212" 7 18 
"213" 48 19 
"214" 18 20 
"215" 3 21 
"216" 35 22 
"230" 9 3 
"233" 23 4 
"235" 60 5 
"236" 8 6 
"237" 5 7 
"238" 23 8 
"239" 10 9 
"240" 28 10 
"241" 8 11 
"242" 31 12 
"244" 22 13 
"245" 12 14 
"246" 2 15 
"247" 9 16 
"261" 3475 17 
"266" 1091 18 
"267" 53 19 
"268" 13 20 
"269" 40 22 
"270" 264 26 
"271" 1726 27 
"292" 43 3 
"294" 22 4 
"301" 48 5 
"306" 81 6 
"307" 5 7 
"308" 25 8 
"309" 12 9 
"311" 12 10 
"315" 63 13 
"316" 2 14 
"317" 30 15 
+1

我想在你的第二個例子中,你正在策劃LOG10的平均'wt',而不是指'在LOG10規模wt'。'coord_trans'幫助頁面中的這句話可能有助於解釋發生了什麼事情:'轉換尺度和轉換座標系的不同之處在於尺度轉換髮生在統計之前,並在之後進行座標轉換。' – aosmith

+0

@aosmith,你是對的。你能回答這個問題嗎,這樣對別人有用嗎? –

回答

1

這與通過scale_y_*使用轉換時發生的轉換有關。一個有用的筆記是在coord_trans幫助頁面,它說:

改造規模和改造 座標系之間的區別在於發生大規模改造前 統計,事後座標變換。

因爲轉化要通過stat_summary計算統計數據之前發生的,你的情節2loglog10(wt)而非mean(wt)在LOG10規模均值的情節。在繪圖之前,您可以通過計算t的每個級別的平均值log10(wt)來驗證此情況。

agg2 <- aggregate(log(wt) ~ t, data=d, FUN=mean) 

ggplot(agg2, aes(x=t, y=`log(wt)`)) + 
    geom_line(size = 1.5) 

線條的形狀與2log中的相同。

enter image description here

0

非常有趣的問題。我認爲stat_summary和不同y比例的組合表現可疑。

我創建了一個簡單的例子:

library(ggplot2) 

data = data.frame(t=c(1,1,10,10,30,30), wt = c(1,1,20,180,1200,1200)) 


ggplot(data, aes(x=t, y=wt)) + 
    stat_summary(data=data, 
       fun.y="mean", geom="line", color="black", size = 1.5)+ 
    scale_y_log10() 


d <- aggregate(wt ~ t, data=data, FUN=mean) 

ggplot(d, aes(x=t, y=wt)) + geom_line(size = 1.5) + scale_y_log10() 

的情節我得到的是:

enter image description here enter image description here

另外,如果你運行上面的過程,而不scale_y_log10你會得到完全相同的地塊。

+0

感謝您的回答。我已經用一個完整的小例子更新了這個問題,它重現了這個問題。不幸的是,我認爲它的行爲比可疑的要糟糕。 –