ggplot scale_y_log10()问题

问题描述:

我遇到了一个有趣的问题,使用ggplot进行缩放。我有一个数据集,我可以使用默认的线性比例进行绘图,但是当我使用scale_y_log10()时,数字就会消失。这里是一些示例代码和两张图片。请注意,线性刻度的最大值为〜700,而对数刻度的结果为10^8。我告诉你,整个数据集只有大约8000条目,所以有些东西是不对的。ggplot scale_y_log10()问题

我想这个问题与我的数据集的结构和装箱有关,因为我无法在像“钻石”这样的常见数据集上复制此错误。但是我不确定排除故障的最佳方法。

感谢, 扎克CP


编辑:bdamarest可以重现对钻石的数据集这样的规模问题:

example_1 = ggplot(diamonds, aes(x=clarity, fill=cut)) + 
    geom_bar() + scale_y_log10(); print(example_1) 

#data.melt is the name of my dataset  
> ggplot(data.melt, aes(name, fill= Library)) + geom_bar() 
> ggplot(data.melt, aes(name, fill= Library)) + geom_bar() + scale_y_log10() 
> length(data.melt$name) 
[1] 8003 

linear scalelog scale

这里是一些示例数据...我想我看到了这个问题。原始的融化数据集可能长达10〜8行。也许行号被用于统计?

> head(data.melt) 
     Library   name    group 
221938  AB Arthrofactin  glycopeptide 
235087  AB Putisolvin  cyclic peptide 
235090  AB Putisolvin  cyclic peptide 
222125  AB Arthrofactin  glycopeptide 
311468  AB  Triostin cyclic depsipeptide 
92249  AB   CDA   lipopeptide 


> dput(head(test2)) 
structure(list(Library = c("AB", "AB", "AB", "AB", "AB", "AB" 
), name = c("Arthrofactin", "Putisolvin", "Putisolvin", "Arthrofactin", 
"Triostin", "CDA"), group = c("glycopeptide", "cyclic peptide", 
"cyclic peptide", "glycopeptide", "cyclic depsipeptide", "lipopeptide" 
)), .Names = c("Library", "name", "group"), row.names = c(221938L, 
235087L, 235090L, 222125L, 311468L, 92249L), class = "data.frame") 

UPDATE:

行号都没有问题。这里使用相同的AES x轴绘制的相同的数据和填充颜色和缩放是完全正确的:

> ggplot(data.melt, aes(name, fill= name)) + geom_bar() 
> ggplot(data.melt, aes(name, fill= name)) + geom_bar() + scale_y_log10() 
> length(data.melt$name) 
[1] 8003 

enter image description hereenter image description here

geom_barscale_y_log10(或任何对数标度)不会很好地工作一起并没有给出预期的结果。

第一个基本问题是条形会变为0,并且在对数刻度上,0会变成负无穷大(这很难绘制)。这个婴儿床通常从1开始而不是0(因为$ \ log(1)= 0 $),如果有0个计数则不绘制任何图形,也不担心变形,因为如果需要对数刻度,不关心被1(不一定是真的,但是...)

我使用@dbemarest显示的diamonds示例。

要做到这一点一般是转换坐标,而不是规模(稍后更多的差异)。

ggplot(diamonds, aes(x=clarity, fill=cut)) + 
    geom_bar() + 
    coord_trans(ytrans="log10") 

但是,这给出了一个错误

Error in if (length(from) == 1 || abs(from[1] - from[2]) < 1e-06) return(mean(to)) : 
    missing value where TRUE/FALSE needed 

这源于负无穷大的问题。

当您使用比例变换时,将变换应用于数据,然后进行统计和排列,然后将比例标注为逆变换(大致)。你可以通过自己分析计算来看看发生了什么。

DF <- ddply(diamonds, .(clarity, cut), summarise, n=length(clarity)) 
DF$log10n <- log10(DF$n) 

这给

> head(DF) 
    clarity  cut n log10n 
1  I1  Fair 210 2.322219 
2  I1  Good 96 1.982271 
3  I1 Very Good 84 1.924279 
4  I1 Premium 205 2.311754 
5  I1  Ideal 146 2.164353 
6  SI2  Fair 466 2.668386 

如果我们以正常的方式绘制这一点,我们得到了预期的柱状图:

ggplot(DF, aes(x=clarity, y=n, fill=cut)) + 
    geom_bar(stat="identity") 

enter image description here

和缩放y轴给出与使用未预先汇总的数据相同的问题。

ggplot(DF, aes(x=clarity, y=n, fill=cut)) + 
    geom_bar(stat="identity") + 
    scale_y_log10() 

enter image description here

我们可以看到这个问题通过绘制计数的log10()值是如何发生的。

ggplot(DF, aes(x=clarity, y=log10n, fill=cut)) + 
    geom_bar(stat="identity") 

enter image description here

这看起来就像一个与scale_y_log10,但标签是0,5,10,... 10^0,10^5,10^10来代替。 ..

因此,使用scale_y_log10计数,将它们转换为日志,堆叠这些日志,然后以反日志形式显示比例。但是,堆积日志不是一种线性转换,所以你要求它做的没有任何意义。

最重要的是,对数刻度上的堆积条形图没有多大意义,因为它们不能从0开始(应该是一个小节的底部),并且比较小节的各个部分是不合理的因为它们的大小取决于它们在栈中的位置。反而认为是这样的:

ggplot(diamonds, aes(x=clarity, y=..count.., colour=cut)) + 
    geom_point(stat="bin") + 
    scale_y_log10() 

enter image description here

或者,如果你真的想要一个总为堆叠酒吧通常会给你的团体,你可以这样做:

ggplot(diamonds, aes(x=clarity, y=..count..)) + 
    geom_point(aes(colour=cut), stat="bin") + 
    geom_point(stat="bin", colour="black") + 
    scale_y_log10() 

enter image description here

+1

谢谢Brian,我很感谢你的详细解释。您也可以使用geom_bar(position =“dodge”)(答案由Winston Chang提供) – zach 2012-02-29 22:05:58

+0

为了更深入地了解这里发生的事情,堆积的条形图通常会给出一个与计数总和相等的高度。但是,sum(log(counts))相当于log(product(counts))。换句话说,你会看到酒吧的高度,就像你把这些数字放在一起。 – Brian 2016-10-19 18:56:25