2017-05-30 88 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

相关问题