理解对数对数 ggplots 的问题

Problems understanding log-log ggplots

我正在处理一个非常大的数据集(大到 post 这里),我真的很难创建一个看起来正确的直方图。这是我对原始数据的最佳尝试:

g <- ggplot(df2, aes(x = n))
g <- g + geom_histogram(color = "white", fill = "firebrick3", bins = 47)
g <- g + scale_x_continuous(trans = 'log10', 
        breaks = trans_breaks('log10', function(x) 10^x), 
        labels = trans_format(math_format(10^.x)))
g <- g + scale_y_continuous(trans = 'log10',
        breaks = trans_breaks('log10', function(x) 10^x), 
        labels = trans_format(math_format(10^.x)), 
        oob = squish_infinite)
g <- g + annotation_logticks()
g <- g + labs(x = "n", y = "log(Count)")
g

这并没有产生情节;相反,它抛出了一个错误和两个警告:

为了尝试做一些其他人也可以尝试的事情,我 运行 计算每个 n 出现的次数的行的集合(有效地手工制作直方图)。这是该数据:

n <- c(2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 30, 40, 50, 60, 
70, 80, 90, 100, 200, 300, 400, 500, 600, 700, 800, 900, 
1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000, 
10000, 20000, 30000, 40000, 50000, 60000, 70000, 80000, 
90000, 100000, 20000)
counts <- c(885452, 468462, 222097, 166234, 103348, 85845, 
60798, 52651, 231830, 81138, 41333, 25274, 17192, 12465, 
9622, 7371, 6069, 27160, 9009, 4465, 2753, 1664, 1285, 918, 
716, 568, 2400, 707, 362, 180, 106, 90, 55, 55, 39, 124, 
25, 12, 8, 2, 1, 0, 2, 0, 3, 2)

这些是使用 [) 格式构建的;例如,对应于 n = 30 的计数计数 n 的所有出现 (30, 31, 32, 33, 34, 35, 36, 37, 38, 39) 次。

最终直方图应该:

我想我错过了一些基本的东西 - 有什么想法吗?

根据最新的建议更新内容,代码为:

g <- ggplot(bigram2, aes(x = n))
g <- g + geom_histogram(color = "white", fill = "firebrick3", bins = 47)
g <- g + scale_x_continuous(trans = 'log10',
        breaks = trans_breaks('log10', function(x) 10^x), 
        labels = trans_format('log10', math_format(10^.x)))
g <- g + scale_y_continuous(trans = 'log10',
        breaks = trans_breaks('log10', function(x) 10^x), 
        labels = trans_format('log10', math_format(10^.x)))
g <- g + annotation_logticks()
g <- g + labs(x = "n", y = "log(Count)")
g

结果图如下所示:

OP,你走对了。最终,问题归结为拼写错误:/。 我将解释您在尝试原始代码时收到的 3 条消息,然后向您展示一个包含应适用于您的数据集的虚拟数据的示例。

您的错误信息。

OP 引用了 运行 代码时收到的三个消息。让我们来解释一下(乱序):

  • 删除了包含缺失值 (geom_bar) 的 2 行。这不应该是一个错误,而是一个警告。它在这里不相关,因为它只是让你知道一些没有价值,所以没有什么可画的。您可以放心地忽略它。

  • 转换在连续 y 轴上引入了无限值。这也是一条警告消息,可以安全地忽略。当您有一些具有 0 计数的 bin 时,在进行对数转换时,预计您在连续 y 轴上有无穷大的值。这是因为 log10(0) 的计算结果为 -Inf。情节仍然可以制作,但这些箱子是最有可能被“移除”的箱子。在您的情况下,OP,您可能有一个直方图,其中删除了序列中的两个箱子……因为它们什么都不包含。不用担心。

  • x * 比例错误:二元运算符的非数字参数。弹出这个是因为您在 scale_*_continuous() 函数中对 trans_format() 的引用实际上有错字。该函数首先需要一个 trans= 参数(很像 trans_breaks()),但您只能通过 math_format() 指定格式。当 math_format() 应用于 trans_format() 中的 trans= 参数时...您会收到该错误。

修复错误消息

修复很简单,就是在trans_format()中指定"log10"。换句话说,使用这个:scale_*_continuous(... labels = trans_format("log10", math_format(10^.x)...),而不是这个 scale_*_continuous(... labels = trans_format(math_format(10^.x)...)

我将通过一个虚拟数据集来展示:

set.seed(1234)
d <- data.frame(n=sample(1:10000, size=1000000, replace=T))

这是没有对数转换的直方图:

p <- ggplot(d, aes(x=n)) + geom_histogram(bins=30, color='black', fill='steelblue')
p

以及对数-对数转换:

p +
  scale_x_continuous(
    trans='log10',
    breaks = trans_breaks('log10', function(x) 10^x), 
    labels = trans_format('log10', math_format(10^.x))) +
  scale_y_continuous(
    trans='log10',
    breaks = trans_breaks('log10', function(x) 10^x), 
    labels = trans_format('log10', math_format(10^.x))
    )