2017-05-30 85 views
0

我使用stat_summary來顯示平均值,根據我的計算,「type1,G-」應該有〜10^7.3的平均值。這就是我沒有使用log10軸繪製它的價值。但是當我添加log10軸時,突然「type1,G-」顯示10^6.5的值。ggplot2:爲什麼設置爲log10軸時顯示錯誤的值?

發生了什麼事?

#Data 
Type = rep(c("type1", "type2"), each = 6) 
Gen = rep(rep(c("G-", "G+"), each = 3), 2) 
A = c(4.98E+05, 5.09E+05, 1.03E+05, 3.08E+05, 5.07E+03, 4.22E+04, 6.52E+05,  2.51E+04, 8.66E+05, 8.10E+04, 6.50E+06, 1.64E+06) 
B = c(6.76E+07, 3.25E+07, 1.11E+07, 2.34E+06, 4.10E+04, 1.20E+06, 7.50E+07, 1.65E+05, 9.52E+06, 5.92E+06, 3.11E+08, 1.93E+08) 
df = melt(data.frame(Type, Gen, A, B)) 

#Correct, non-log10 version ("type1 G-" has a value over 1e+07) 
ggplot(data = df, aes(x =Type,y = value)) + 
    stat_summary(fun.y="mean",geom="bar",position="dodge",aes(fill=Gen))+ 
    scale_x_discrete(limits=c("type1"))+ 
    coord_cartesian(ylim=c(10^7,10^7.5)) 


#Incorrect, log10 version ("type1 G-" has a value under 1e+07) 
ggplot(data = df, aes(x =Type,y = value)) + 
    stat_summary(fun.y="mean",geom="bar",position="dodge",aes(fill=Gen))+ 
    scale_y_log10() 
+0

它採取日誌轉換後的意思?我想這裏的正確和不正確是主觀的。 – Axeman

回答

0

想要coord_trans。作爲其文件說:

# The difference between transforming the scales and 
# transforming the coordinate system is that scale 
# transformation occurs BEFORE statistics, and coordinate 
# transformation afterwards. 

但是,你不能用這個barplot,因爲酒吧在0開始,(0)沒有定義日誌10。但是無論如何,鏤空通常不是一個好的可視化。

ggplot(data = df, aes(x =Type,y = value)) + 
    stat_summary(fun.y="mean",geom="point",position="identity",aes(color=Gen))+ 
    coord_trans(y = "log10", limy = c(1e5, 1e8)) + 
    scale_y_continuous(breaks = 10^(5:8)) 

resulting plot

很顯然,你應該繪製某種不確定性的信息。我會推薦一個boxplot。

+0

條形圖如何不僅僅是一個垂直線以下的點圖,因爲它提高了可視性......如果該點圖是「良好」的可視化,我將採用錯誤的可視化。 – myflow

相關問題